gpt4 book ai didi

java - 每次按钮事件发生时从 JTable 中删除以前的内容

转载 作者:行者123 更新时间:2023-12-02 05:49:29 25 4
gpt4 key购买 nike

我很难从 JTable 中删除数据。我尝试过 JTable .repaint()、setRowCount(0) 以及 model.removeRow()。我创建了 print 语句来查看使用 setRowCount 时的行数,行数确实变为 0,但我的表没有更新。这是我需要实现的该项目的最后一部分。我不确定为什么我的表没有更新,因为我正在使用 removeRow 和 setRowCount 更改模型。如果你们能引导我走向正确的方向,我将不胜感激。

public class TemplateGui extends JFrame  {
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField;
private static String [] sortedRoles_Flags,finalFlagsArr,finalHSArr;
private static String finalFlags="",finalHS="",columnToConvert="Result";
private Vector<String> SF,HS,column;
private JTable hotelSecurityTable,securityFlagsTable;
private DefaultTableModel hsTableModel,sfTableModel;


public TemplateGui(){

super("Galaxy Template Generator V1.0");
//column name
column = new Vector(Arrays.asList(columnToConvert));

getContentPane().setForeground(new Color(0, 0, 0));
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(51, 51, 51));

//radio buttons
JRadioButton rdbtnNewRadioButton = new JRadioButton("Central User ");
rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnNewRadioButton.setBackground(Color.WHITE);
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(222, 75, 127, 36);
getContentPane().add(rdbtnNewRadioButton);

final JRadioButton rdbtnPropertyUser = new JRadioButton("Property User");
rdbtnPropertyUser.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnPropertyUser.setBackground(Color.WHITE);
buttonGroup.add(rdbtnPropertyUser);
rdbtnPropertyUser.setBounds(222, 38, 127, 34);
getContentPane().add(rdbtnPropertyUser);

textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 35, 53, 34);
getContentPane().add(textField);
textField.setColumns(10);

JLabel lblHotelSecurity = new JLabel("Hotel Security (H S)");
lblHotelSecurity.setHorizontalAlignment(SwingConstants.CENTER);
lblHotelSecurity.setFont(new Font("Tahoma", Font.BOLD, 13));
lblHotelSecurity.setBounds(10, 144, 189, 23);
lblHotelSecurity.setBackground(new Color(204, 204, 204));
lblHotelSecurity.setOpaque(true);
getContentPane().add(lblHotelSecurity);

JLabel label = new JLabel("Security Flags (S F)");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Tahoma", Font.BOLD, 13));
label.setBounds(222, 144, 372, 23);
label.setBackground(new Color(204, 204, 204));
label.setOpaque(true);
getContentPane().add(label);

JLabel lblEnterTemplateCode = new JLabel("ENTER TEMPLATE CODE");
lblEnterTemplateCode.setForeground(new Color(255, 255, 255));
lblEnterTemplateCode.setFont(new Font("Tahoma", Font.BOLD, 14));
lblEnterTemplateCode.setBounds(10, 9, 175, 23);
getContentPane().add(lblEnterTemplateCode);

JLabel lblSelectUserRole = new JLabel("SELECT USER ROLE LEVEL");
lblSelectUserRole.setForeground(new Color(255, 255, 255));
lblSelectUserRole.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSelectUserRole.setBounds(222, 13, 195, 14);
getContentPane().add(lblSelectUserRole);

//Submit button action
Button button = new Button("Generate Template");
button.setFont(new Font("Dialog", Font.BOLD, 12));
button.setBackground(new Color(102, 255, 102));
button.setForeground(Color.BLACK);
button.setBounds(467, 83, 127, 41);
getContentPane().add(button);


button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

Query excell = new Query();
//get template text
String template = textField.getText().toUpperCase();
System.out.println(template);

if(rdbtnPropertyUser.isSelected()){
try {
//property user was selected
excell.runProcess(1);
System.out.println("you selected Property user");
} catch (IOException e) {

e.printStackTrace();
}
}
else{
try {
//Central User was selected
excell.runProcess(2);
System.out.println("you selected central user");
} catch (IOException e) {

e.printStackTrace();
}
}

System.out.println("NOW WERE HERE");
//get static variables from Excel Query
for(int i = 0; i< Query.sortedGF.length; i++)
{
if(Query.sortedGF[i].contains(template)){
sortedRoles_Flags =Query.sortedGF[i].split(" ");
System.out.println("THIS RAN"+" :"+i);
break;
}

}

//add data to table
int j=1;
int sizeOfFlags = Query.securityFlags.length;


//Add HS to FinalHS Variable only if Yes
for(int i=0;i< sortedRoles_Flags.length+1-sizeOfFlags;i++)
{
if(sortedRoles_Flags[i].matches("Y|y|Y\\?|\\?Y|y\\?|\\?y"))
{
System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
finalHS += Query.hotelSecurity[i]+"+";
System.out.println("Hotel security:"+" "+finalHS);
}
}

//add Security Flags to Final Flags
for(int i=(sortedRoles_Flags.length-sizeOfFlags+1);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+"\t\t: "+ sortedRoles_Flags[i]+" + ";
j++;
}

//Leave open just incase they would prefer a text file for template in which case we just write it
System.out.println(finalFlags);

System.out.println(finalHS);

//Convert to String Arrays in order to add to our JTable
finalFlagsArr= finalFlags.split("\\+");
finalHSArr = finalHS.split("\\+");

for(int i=0; i <finalHSArr.length;i++)
finalHSArr[i].replaceAll(" ","");

hsTableModel.setRowCount(0);
for (String row : finalHSArr) {
hsTableModel.addRow(new Object[]{row});
}
sfTableModel.setRowCount(0);
for (String row : finalFlagsArr) {
sfTableModel.addRow(new Object[]{row});
}

}
});
//scroll panes for flags
JScrollPane scrollPaneHS = new JScrollPane();
scrollPaneHS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneHS.setBounds(10, 170, 189, 185);
getContentPane().add(scrollPaneHS);

JScrollPane scrollPaneSF = new JScrollPane();
scrollPaneSF.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneSF.setBounds(222, 170, 372, 187);
getContentPane().add(scrollPaneSF);

//tables for updates
hsTableModel = new DefaultTableModel(HS,column);
hotelSecurityTable = new JTable(hsTableModel);
hotelSecurityTable.setBackground(new Color(255, 255, 255));
scrollPaneHS.setViewportView(hotelSecurityTable);

sfTableModel = new DefaultTableModel(SF,column);
securityFlagsTable = new JTable(sfTableModel);
scrollPaneSF.setViewportView(securityFlagsTable);


}


}

更新我的最新尝试。这似乎有效,但第二次就不行了。

if(hsTableModel.getRowCount() !=0 && sfTableModel.getRowCount() !=0){
for(int i=0; i<hsTableModel.getRowCount();i++)
hsTableModel.removeRow(i);

for(int i=0; i<sfTableModel.getRowCount();i++)
sfTableModel.removeRow(i);

sfTableModel.fireTableRowsDeleted(0, sfTableModel.getRowCount()-1);
hsTableModel.fireTableRowsDeleted(0, hsTableModel.getRowCount()-1);
}

最佳答案

when using setRowCount and the rows do become 0 however my table does not update.

由于您使用的是 DefaultTableModel,因此 setRowCount(0) 是用于清除所有条目的模型的正确方法。应该自动重新绘制自己。

创建一个简单的SSCCE这说明了你的问题。 SSCCE 应包含一个包含数据的 JTable,然后是一个用于调用 setRowCount() 方法的 JButton。

向自己证明基本概念有效,然后确定为什么您的实际代码不起作用。如果表没有重新绘制,则通常意味着您正在对尚未添加到 GUI 上可见的 JTable 的 TableModel 调用 setRowCount() 方法。

另外,不要:

  1. 使用按钮。您应该使用 JButton
  2. 使用空布局。 Swing 被设计为与布局管理器一起使用。

关于java - 每次按钮事件发生时从 JTable 中删除以前的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23700333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com