Hello,

I'm trying to help my son who is student in computer sciences in the Paris University. They were given a Java drill based on JTable.
The code he had written was pretty good, and after a few corrections the windows displayed correctly. However there is something that neither him nor myself were able to achieve. Let's see his exercise subject :

1) Create a JTable and fill each cell with boy or girl first names.
2) define 2 buttons. One for girl names and one for boy names.
3) when clicking on either button, names must be written in RED for boys and Yellow for girls.

We were not able colouring names. We tried many things,particularly by creating a TableCellRendered. But this did not work because there were no click on cells. Or maybe because we don't understand very well how a rendered works.

Below is our code and please, look at particularly the coloringCells method.

Thanks a lot for your help or any advice you may provide.

Gerard
Code:
package pierre;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
	 
	public class TestJTable  extends JFrame implements java.awt.event.ActionListener 
	{
		private static final long serialVersionUID = 1L;
	        int nbCells 	= 4 ;    // number of cells per line and column
	        protected String[][] valeur 	= new String[nbCells][nbCells];
	        protected JPanel JFrameContentPane = new JPanel();
	        protected JPanel panel = null;
	        protected static DefaultTableModel dtm ;
	        protected JButton button1, button2, button3 ; 
	        protected JLabel message =null;
	        private static JTable jt = null;
	        String []  Girls = {"Janet", "Monica", "Helena", "Justine", "Alexa", "Brenda", "Jenny", "Kristin"};  
	        String []  Boys = {"Peter", "Paul", "Jack", "Brad", "George", "Alan", "James", "Cary"};  
	        public TestJTable() 
	        {
	            super("Test Jtable");
	            panel = new JPanel();
	            JFrameContentPane.setLayout(null);
	            setSize(309, 344);
	    		setContentPane(JFrameContentPane);
	            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	            panel = new javax.swing.JPanel();
	    		panel.setName("myPanel");
	    		panel.setLayout(new java.awt.GridLayout());
	    		panel.setBounds(10, 44, 272, 148);
	    		panel.setBackground(Color.white);
	    		// define the grid now
	    		dtm = new DefaultTableModel(nbCells,nbCells);
		        jt = new JTable(dtm);
		    	panel.add(jt);
		    	jt.setGridColor( Color.BLACK) ;  
		    	JFrameContentPane.add(panel);
	            button1 = new JButton();
	            button1.setBounds(220, 230, 70, 25);
	            button1.setText("Finish");
	            button2 = new JButton();
	            button2.setBounds(20, 230, 70, 25);
	            button2.setText("Girls");
	            button3 = new JButton();
	            button3.setBounds(120, 230, 70, 25);
	            button3.setText("Boys");
	            JFrameContentPane.add(button1);
	            JFrameContentPane.add(button2);
	            JFrameContentPane.add(button3);
	            button2.addActionListener(this);
	            button1.addActionListener(this);
	            message  = new javax.swing.JLabel();
		        message.setBounds(10, 210, 242, 12);
		        message.setForeground(java.awt.Color.red);
		        message.setHorizontalTextPosition(JLabel.CENTER);
		        message.setFont(new java.awt.Font("Times New Roman", 1, 12));
		        message.setText("Click either button to color boy or girl names");
		        JFrameContentPane.add(message); 
	            this.setLocationRelativeTo(null);
	           
	            // Now we set the right size for rows. 
	            dtm.addTableModelListener(new TableModelListener() 
	            {
					  @Override public void tableChanged(final TableModelEvent e) 
					  {
					    // TO DO: replace 100 with the actual preferred height.
						  java.awt.EventQueue.invokeLater(new Runnable() 
						  {
							  @Override public void run() 
						      {
								java.awt.Dimension dm = panel.getSize();  
								double  h= 	dm.getHeight()/nbCells ;
								int y = (int)Math.round(h);
						        jt.setRowHeight(e.getFirstRow(), y);
						      }
						  });
					  }
					});
	            
	            fillCells();   // populate our table
	           this.setVisible(true);
			} 
	        
	       private void  fillCells()
	       {
	    	   int index=0;
	    	   for (int i = 0; i< 4; i++)  
	    	   {
	    		  int j=0;
	    		  while (j<4)
	    		  {
	    			  jt.setValueAt(Girls[index], i, j++);
	    			  jt.setValueAt(Boys[index++], i, j++);
	    		  }
	    		    
	    	   }
	       }   
	    	@Override
	        public void actionPerformed(ActionEvent e) 
	        {
	          if (e.getSource() == button1) 	{ System.exit(0);}
	          if (e.getSource() == button2) 	{ coloringGirls();}
	          if (e.getSource() == button3) 	{ coloringBoys();}
	        }
	       
	        private void coloringGirls() {coloringCells(Boys, Color.YELLOW);} 
	        private void coloringBoys() {coloringCells(Boys, Color.RED);} 
	        
	        private void coloringCells(String[] tab, Color c)
	        {
	        	Vector v = new Vector();
	        	for (int i =  0; i<tab.length; i++) v.add(i,tab[i]); // fils the vector
	        	 
	        	for (int row=0; row<nbCells; row++)
	        	{	
	        		for (int col=0;col<nbCells; col++)
		        	{
	        			String wk = (String) jt.getValueAt(row, col);
	        			int indx = v.indexOf(wk);
	        			if( indx==-1) continue;  
	        			// THIS CELL MUST BE COLORED NOW, BUT I DON'T KNOW HOW TO DO
	        			// I TRIED WITH A NEW RENDERER  BUT IT DOES NOT WORK
		        	}	
	        	}
	        }
	         
			public static void main(String args[]) 
			{
	             new TestJTable();
			}
	}