2012-01-12 66 views
0

i)我的表不包含列,而jcheckbox包含最后一列。 ii)我正在使用组合框来选择特定值。 iii)基于组合框的值,jbutton将数据加载到表中。 iv)当我将重新加载将数据放入表格后,新数据就会显示在jtable中。 v)问题是,当我按下Jcheckbox时,旧数据会以jtable显示。 代码如下:jtable重新加载,但在jcheck框中单击时弹出旧数据弹出

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(); 
     } 

    } 
+2

展我们您的真实源代码。这个不会编译,我们也不知道它包含的错误是真实的还是假的。我们需要一个SSCCE。 – 2012-01-12 08:29:55

+0

请学习java命名约定并坚持使用它们 – kleopatra 2012-01-12 15:23:32

+0

发布格式正确的代码。这是使用制表符或空格进行缩进并保持一致。 – camickr 2012-01-12 16:24:11

回答

1

当你要更改的数据在你的表,尝试创建一个新的DefaultTableModel对象(或实现TableModel接口的任何其它对象),并调用table.setModel(yourNewModel);

+0

为什么firetabledatachanged()不起作用。 – user1101703 2012-01-12 08:49:20

+0

*“为什么firetabledatachanged()不起作用。”*要找到答案,请发布*** [SSCCE](http://sscce.org/)。*** – 2012-01-12 09:23:16