0

i) My Table contains no of columns and a jcheckbox i the last column.
ii) I am using a combobox to select certain value.
iii) based on the value of combo box the jbutton loads the data into the table.
iv) when i reload the data into the table, the new data gets displayed in jtable.
v) The problem is that, When I press the Jcheckbox, the old data is getting displayed in jtable.
code is as below:

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

Edited by peter_budo: Keep It Clear - Do use code tags when posting any code

3
Contributors
2
Replies
8
Views
5 Years
Discussion Span
Last Post by itexamstube
0

there are lots of good workaround for JTable and Database, I'd suggest to search for ResultsetTableModel or TableFromDatabase

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.