Java jtable会重新加载,但当单击jcheck框时,会重新加载旧数据popsup
i) 我的表不包含任何列,最后一列包含一个jcheckbox i。 ii)我使用组合框选择特定值。 iii)根据组合框的值,jbutton将数据加载到表中。 iv)当i将数据重新加载到表中时,新数据将显示在jtable中。 v) 问题是,当我按下Jcheckbox时,旧数据将显示在jtable中。 代码如下:Java jtable会重新加载,但当单击jcheck框时,会重新加载旧数据popsup,java,swing,jtable,Java,Swing,Jtable,i) 我的表不包含任何列,最后一列包含一个jcheckbox i。 ii)我使用组合框选择特定值。 iii)根据组合框的值,jbutton将数据加载到表中。 iv)当i将数据重新加载到表中时,新数据将显示在jtable中。 v) 问题是,当我按下Jcheckbox时,旧数据将显示在jtable中。 代码如下: public class aap2 extends JFrame { @SuppressWarnings({ "unchecked", "rawtypes" })
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();
}
}
public类aap2扩展了JFrame{
@SuppressWarnings({“unchecked”,“rawtypes”})
静态JComboBox年=新JComboBox(新对象[]
{"2012-13", "2013-14", "2014-15", "2015-16","2016-17","2017-
18","2018-19"});
@SuppressWarnings({“unchecked”,“rawtypes”})
静态JComboBox分区=新JComboBox(新对象[]
{“姓名”、“地址”、“职业”、“其他”});
JComboBox schemetype=新JComboBox(新对象[]
{});
JButton showschemes=新JButton(“Show Schemes”);
静态结果集;
连接con;
报表st;
静态JPanel面板=新JPanel();
静态JPanel panel1=新的JPanel();
静态JPanel panel2=新的JPanel();
静态JPanel panel3=新的JPanel();
静态JPanel panel4=新的JPanel();
静态JPanel panel5=新的JPanel();
UIManager ui=新UIManager();
静态int列;
静态整数选择[];
JTable表;
数字地面模型;
公共aap2(){
分区设置最大行数(5);
年份。设置最大行数(5);
年份.背景(颜色.蓝色);
年份.挫折背景(颜色.白色);
2009-10年度;
立根数(00,401020700);
颜色c=新颜色(160200100);
getContentPane().setBackground(c);
颜色c3=新颜色(0,50,50,2);
1.立根点(c3);
2.立根点(c3);
面板.立根点(c);
面板顺序(新标题边框(新线条边框(颜色:白色,1),“”);
panel.setLayout(新的GridBagLayout());
GridBagConstraints gc=新的GridBagConstraints();
panel1.setLayout(新的GridBagLayout());
GridBagConstraints gc1=新的GridBagConstraints();
gc1.gridx=0;
gc1.gridy=0;
小组1.添加(新的JLabel(“财政年度”),一般条款1;
一般条款1.插图=新插图(1,10,1,10);
gc1.gridx=1;
gc1.gridy=0;
小组1.增加(年份,一般条款1);
gc1.gridx=4;
gc1.gridy=0;
小组1.添加(新的JLabel(“分部”),一般条款1;
gc1.gridx=5;
gc1.gridy=0;
小组1.增补(一般条款1分部);
gc.gridx=0;
gc.gridy=0;
面板。添加(面板1,gc);
JPanel p2=新的JPanel();
gc.gridx=0;
gc.gridy=4;
p2.挫折背景(c3);
添加面板(p2,gc);
panel3.setLayout(新的GridBagLayout());
GridBagConstraints gc3=新的GridBagConstraints();
一般条款3.插图=新插图(30,10,1,10);
gc3.gridx=0;
gc3.gridy=2;
面板3.添加(展示方案,一般条款3);
showschemes.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e){
显示已执行的方案(e);
}
});
gc.gridx=0;
gc.gridy=5;
3.立根点(c3);
面板。添加(面板3,gc);
添加(面板,边界布局。北);
未装饰的设置(真实);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_对话框);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
}
已执行的公共无效showschemsActionPerformed(ActionEvent e){
可显示();
}
公共空间显示表(){
最终向量列名称=新向量();
最终矢量数据=新矢量();
试一试{
forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
System.out.println(“驱动程序加载”);
//建立联系
con=DriverManager.getConnection
(“jdbc:odbc:ysr”);
System.out.println(“数据库连接DDD”);
//创建一个语句
st=con.createStatement();
结果集rs=st.executeQuery(“从ysr2011中选择*,其中=
“+division.getSelectedItem()+”);
ResultSetMetaData md=rs.getMetaData();
columns=md.getColumnCount();
System.out.println(“列”+(列+1));
对于(int i=1;i当您想要更改表中的数据时,尝试创建一个新的DefaultTableModel
对象(或实现TableModel
接口的任何其他对象),并调用table.setModel(yourNewModel);
向我们展示您真实的源代码。这段代码不可编译,我们也不知道它包含的错误是真是假。我们需要一个SSCCE。请学习java命名约定并坚持使用正确格式的代码。也就是说,使用制表符或空格进行缩进并保持一致。“为什么firetabledatachanged()不起作用。”要找到答案,请发布一个。