gpt4 book ai didi

java - jtable 重新加载,但是当单击 jcheck 框时会弹出旧数据

转载 作者:行者123 更新时间:2023-12-01 15:36:00 25 4
gpt4 key购买 nike

i) 我的表不包含任何列,最后一列有一个 jcheckbox。ii)我使用组合框来选择特定值。iii) 根据组合框的值,jbutton 将数据加载到表中。iv) 当我重新加载数据到表中时,新数据将显示在 jtable 中。v) 问题是,当我按下 Jcheckbox 时,旧数据显示在 jtable 中。 代码如下:

 public class aap2 extends JFrame {
@SuppressWarnings({ "unchecked", "rawtypes" })
static JComboBox year = new JComboBox(new Object[]
{"2012-13", "2013-14", "2014-15", "2015-16","2016-17","2017-
18","2018-19"});
@SuppressWarnings({ "unchecked", "rawtypes" })
static JComboBox division = new JComboBox(new Object[]
{"Name", "address","profession","others"});
JComboBox schemetype = new JComboBox(new Object[]
{});

JButton showschemes = new JButton("Show Schemes");

static ResultSet rs;
Connection con;
Statement st;
static JPanel panel = new JPanel();
static JPanel panel1 = new JPanel();
static JPanel panel2= new JPanel();
static JPanel panel3= new JPanel();
static JPanel panel4= new JPanel();
static JPanel panel5= new JPanel();
UIManager ui= new UIManager();
static int columns;
static int sel[];
JTable table;
DefaultTableModel dtm;
public aap2(){
division.setMaximumRowCount(5);
year.setMaximumRowCount(5);
year.setForeground(Color.blue);
year.setBackground(Color.white);
year.setSelectedItem("2009-10");

setBounds(00,40,1020,700);
Color c= new Color(160,200,100);
getContentPane().setBackground(c);
Color c3= new Color(0,50,50,2);
panel1.setBackground(c3);
panel2.setBackground(c3);
panel.setBackground(c);
panel.setBorder(new TitledBorder(new LineBorder(Color.white,1),""));
panel.setLayout(new GridBagLayout());
GridBagConstraints gc= new GridBagConstraints();

panel1.setLayout( new GridBagLayout());
GridBagConstraints gc1 = new GridBagConstraints();

gc1.gridx=0;
gc1.gridy=0;
panel1.add( new JLabel("Financial Year"),gc1);
gc1.insets= new Insets(1,10,1,10);
gc1.gridx=1;
gc1.gridy=0;
panel1.add(year,gc1);

gc1.gridx=4;
gc1.gridy=0;
panel1.add( new JLabel("Division"),gc1);
gc1.gridx=5;
gc1.gridy=0;
panel1.add(division,gc1);
gc.gridx=0;
gc.gridy=0;
panel.add(panel1,gc);


JPanel p2= new JPanel();
gc.gridx=0;
gc.gridy=4;
p2.setBackground(c3);
panel.add(p2,gc);

panel3.setLayout( new GridBagLayout());
GridBagConstraints gc3 = new GridBagConstraints();
gc3.insets= new Insets(30,10,1,10);
gc3.gridx=0;
gc3.gridy=2;
panel3.add(showschemes,gc3);
showschemes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){

showschemsActionPerformed(e);
}
});

gc.gridx=0;
gc.gridy=5;
panel3.setBackground(c3);
panel.add(panel3,gc);

add(panel, BorderLayout.NORTH);
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}

public void showschemsActionPerformed(ActionEvent e) {

showtable();

}
public void showtable() {
final Vector<String> columnNames = new Vector<String>();
final Vector<Vector<Object>> data = new Vector<Vector<Object>>();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");

// Establish a connection
con= DriverManager.getConnection
("jdbc:odbc:ysr");
System.out.println("Database connecteddddd");

// Create a statement
st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM ysr2011 where=
'"+division.getSelectedItem()+"' ");

ResultSetMetaData md = rs.getMetaData();

columns = md.getColumnCount();
System.out.println("col" +(columns+1));
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
columnNames.addElement("Save");
while (rs.next()) {
Vector<Object> row = new Vector<Object>(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
row.addElement(new Boolean(false));
data.addElement( row );
}
rs.close();
con.close();
st.close();
}
catch(Exception e1){}
dtm = new DefaultTableModel(data, columnNames) {
public Class getColumnClass(int col) {
if(col==columns){
return Boolean.class;
}else{
return String.class;
}
}

public boolean isCellEditable(int rowIndex, int colIndex) {
return (colIndex == columns);
}
};

dtm.fireTableDataChanged();
table= new JTable(dtm); ;

table.setFont(new Font(" Arial",Font.PLAIN,12));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoCreateRowSorter(true);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
table.setRowSelectionAllowed(false);
header.setFont(new Font(" Arial",Font.BOLD,12));
JScrollPane scrollPane = new JScrollPane(table);
JButton button= new JButton("Save");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < table.getRowCount(); row++) {
Boolean b = ((Boolean) table.getValueAt(row, columns));
if (b.booleanValue()) {
System.out.print("row " + row + " is " + b + ": ");
for (int col = 0; col < table.getColumnCount(); col++) {
System.out.print(table.getValueAt(row, col) + " ");
}
System.out.println();
}
}

}
});

JPanel buttonpanel= new JPanel();
buttonpanel.add(button);
add(scrollPane,BorderLayout.CENTER);
add(buttonpanel,BorderLayout.SOUTH);
Color c1= new Color(160,200,100);
table.setBackground(c1);
buttonpanel.setBackground(c1);
setBackground(c1);
setVisible(true);
}


public static void main(String args[]){
new aap2();
}

}

最佳答案

当您想要更改表中的数据时,请尝试创建一个新的 DefaultTableModel 对象(或任何其他实现 TableModel 接口(interface)的对象),并调用 table.setModel(yourNewModel);

关于java - jtable 重新加载,但是当单击 jcheck 框时会弹出旧数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831560/

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