Results 1 to 9 of 9
  1. #1
    michalss is offline Member
    Join Date
    Mar 2011
    Posts
    17
    Rep Power
    0

    Default jTable Cell collor

    Hi all,

    I very new to Java and i would like to get help regarding jTable.
    I have 3 colloms and i'm filling them in the loop with data. My problem is i need to select different color in the last colom depence on the value.

    This is what i'm using:
    Java Code:
    int i = 1;
        
            for (String line : Link) {
    
                if (this.CheckLF(line) == true) {
                       ((DefaultTableModel)tResult.getModel()).addRow(new Object[]{i,line,"ALIVE"});
    
                } else {
                        ((DefaultTableModel)tResult.getModel()).addRow(new Object[]{i,line,"DEAD",false});
                }
             i++;
            }
    Can anyone write an example please ?


    thx
    Last edited by michalss; 03-12-2011 at 11:24 AM.

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by michalss View Post
    My problem is i need to select different color in the last colom depence on the value.
    You mean the column with "ALIVE" or "DEAD" ? As an example, if the value is ALIVE the foreground colorwill be green and if its DEAD, the color will be red?

    You need a custom cell renderer, take a look at
    How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Can anyone write an example please ?
    Java Code:
    	public static void main(String[] args)  {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				JFrame frame = new JFrame();
    				JPanel panel = new JPanel();
    				DefaultTableModel model = new DefaultTableModel(new String[]{"id","line","status","?"},0);
    				JTable table = new JTable(model);
    				((DefaultTableModel)table.getModel()).addRow(new Object[]{0,"foo","ALIVE"});
    				((DefaultTableModel)table.getModel()).addRow(new Object[]{1,"bar","DEAD",false});
    				//register the renderer on the column with identifier = "status"
    				table.getColumn("status").setCellRenderer(new TableCellRenderer() {
    					private JLabel label = new JLabel();
    					public Component getTableCellRendererComponent(JTable table, Object value,
    							boolean isSelected, boolean hasFocus, int row, int column) {
    						label.setText(value.toString());
    						if(value.toString().equals("ALIVE")){
    							label.setForeground(Color.GREEN);
    						}else{
    							label.setForeground(Color.RED);
    						}
    						return label;
    					}
    				});
    				
    				panel.add(new JScrollPane(table));
    				frame.add(panel);
    				frame.pack();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setLocationRelativeTo(null);
    				frame.setVisible(true);
    			}
    		});
    	}

  3. #3
    michalss is offline Member
    Join Date
    Mar 2011
    Posts
    17
    Rep Power
    0

    Default

    Thnank you but i'm using netBeans Component so i dont need to declarate jTable again and thats my problem. I found many example of that but i have no idea how to do it if i have all comnetnets on the form allready. So this 2 lines i cannot use really.

    DefaultTableModel model = new DefaultTableModel(new String[]{"id","line","status","?"},0);
    JTable table = new JTable(model);


    yes your deduction is correct. if alive = green and dead = red.

    Thx a lot
    Last edited by michalss; 03-12-2011 at 11:23 AM.

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    ?? Copy and paste the lines
    Java Code:
    				table.getColumn("status").setCellRenderer(new TableCellRenderer() {
    					private JLabel label = new JLabel();
    					public Component getTableCellRendererComponent(JTable table, Object value,
    							boolean isSelected, boolean hasFocus, int row, int column) {
    						label.setText(value.toString());
    						if(value.toString().equals("ALIVE")){
    							label.setForeground(Color.GREEN);
    						}else{
    							label.setForeground(Color.RED);
    						}
    						return label;
    					}
    				});
    into your code and rename table to tResult and "status" to the caption of your third column thats all. We cant help you if you dont post your code or describe it. And read the link which i have posted before !

  5. #5
    michalss is offline Member
    Join Date
    Mar 2011
    Posts
    17
    Rep Power
    0

    Default

    Thats why i did :) , but gettign error

    internal error; cannot instantiate () at <anonymous TableCellRenderer> to ()
    --


    UPDATE: Sorry i had to add correct imports :)
    Last edited by michalss; 03-12-2011 at 11:36 AM.

  6. #6
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    sorry i`m out, maybe another one can help you with these less informations.

    good luck !

  7. #7
    michalss is offline Member
    Join Date
    Mar 2011
    Posts
    17
    Rep Power
    0

    Default

    Ok now im getting exeption during the loop. Where to pu this your code pls ?

    for (String line : Link) {

    if (this.CheckLF(line) == true) {
    ((DefaultTableModel)tResult.getModel()).addRow(new Object[]{i,line,"ALIVE"});


    } else {
    ((DefaultTableModel)tResult.getModel()).addRow(new Object[]{i,line,"DEAD",false});
    }

    i++;

    }

  8. #8
    michalss is offline Member
    Join Date
    Mar 2011
    Posts
    17
    Rep Power
    0

    Default

    it is sorted THX a buch mate. Status is case sensitive :)

  9. #9
    robilonu is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default

    Hi I have the same problem please eRaa give your mail, this is my code , my application is for a restaurant, i have a jtable where i am display the number of tables 1,2,3,4,5,6... if my table is busy i want to change the color of the row maybe red and green when it is free

    private void actualizar_tmesas(){

    DefaultTableModel modelomesa= new DefaultTableModel();
    modelomesa.addColumn("Numero" );


    try{
    ResultSet mesastodas = consultas.mostrar_mesas();
    while (mesastodas.next()){
    Object[] datosmesas = new Object [1];
    for (int row = 0; row < 1; row++){
    datosmesas[row]=mesastodas.getObject(row+1);
    System.out.println(datosmesas[row]);

    //Verify table if estadome = 1 (red) is busy and 0 (green) not
    aux_idme=((Integer)mesastodas.getObject(row+1)).in tValue();
    estadome= consultas.estado_mesa(aux_idme);
    System.out.println("Este es el auxiliar"+aux_idme+"este es elestado de la mesa"+estadome);

    }
    modelomesa.addRow(datosmesas);

    System.out.println(mesastodas.getString(2));

    }
    T_Mesas.setModel(modelomesa);

    }
    catch (Exception e){
    e.printStackTrace();
    }

    }

    PLEASE HELP ME

Similar Threads

  1. jtable cell to string
    By Sno in forum Advanced Java
    Replies: 3
    Last Post: 04-30-2010, 07:53 PM
  2. jtable cell to string
    By Sno in forum New To Java
    Replies: 3
    Last Post: 04-30-2010, 05:08 PM
  3. Setting jTable cell?
    By greatmajestics in forum New To Java
    Replies: 5
    Last Post: 04-22-2010, 05:33 PM
  4. jtable cell renderer
    By ankitmcgill in forum New To Java
    Replies: 2
    Last Post: 05-22-2009, 01:08 AM
  5. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •