• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Swing table coloring

Aeren

n00b
Joined
Aug 29, 2005
Messages
37
Hi everyone!

I would like to know if it's possible, to change the color(for- or background) of one cell.
Anyone ever tried that before?
 
Derive a from DefaultTableCellRenderer and override getTableCellRendererComponent().

Code:
class MyTableCellRenderer extends DefaultTableCellRenderer
{
  public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected,boolean hasFocus,int row, int col)
  {
    Component comp = super.getTableCellRendererComponent(table,
      value, isSelected, hasFocus, row, col);

    setBackground(/*color*/);

    return comp;
  }
}

Set the renderer using JTable.setDefaultRenderer().
 
bassman said:
Derive a from DefaultTableCellRenderer and override getTableCellRendererComponent().

Code:
class MyTableCellRenderer extends DefaultTableCellRenderer
{
  public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected,boolean hasFocus,int row, int col)
  {
    Component comp = super.getTableCellRendererComponent(table,
      value, isSelected, hasFocus, row, col);

    setBackground(/*color*/);

    return comp;
  }
}

Set the renderer using JTable.setDefaultRenderer().

What can i do if i already extend from JFrame?
This is my code so far.

Code:
import java.util.*;
import java.util.Vector.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.GridLayout;
import java.awt.Dimension;

public class View extends JFrame {
  private JFrame myFrame;
  private JTabbedPane tabbedPane;
  private JTable myTable;
  private JScrollPane scrollPane;

  public View( int size ) {
    super("Changes");
    try {
      UIManager.setLookAndFeel(
      UIManager.getSystemLookAndFeelClassName( ) );
    } catch ( Exception e ) { System.out.println( e.getMessage( ) ); }
    myTable = new JTable( size, 2 );
    myTable.setRowHeight( 20 );
    scrollPane = new JScrollPane( myTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
    scrollPane.setPreferredSize( new Dimension( 640, 480 ) );
  }

  public void addLine( String whichSide, int whichRow, String content ) {
    if( whichSide.equals( "source" ) ) {
      myTable.setValueAt( content,whichRow ,0 );
    } else {
      myTable.setValueAt( content,whichRow ,1 );
    }
  }

  public void showAll( ) {
    this.getContentPane( ).add( scrollPane );
    this.setSize( 640,480 );
    this.setLocationRelativeTo( null );
    this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    this.setVisible( true );
  }

}
The function addLine should be the one to set the color to green. How can i do that?

Thanks
Aeren
 
Having an external method (addLine) is not the way to do this. JTable colors each cell when it is rendered. By using an overridden DefaultTableCellRenderer, you hook directly in to the cell rendering in a nice, encapsulated, object-oriented way.
 
bassman said:
Having an external method (addLine) is not the way to do this. JTable colors each cell when it is rendered. By using an overridden DefaultTableCellRenderer, you hook directly in to the cell rendering in a nice, encapsulated, object-oriented way.
Ok, i have another problem then:
Unlike List, the size of JTable can't be changed after it's constructed(at least i didn't find a way to do it). The function addLine only writes in a cell, "Line" only indicates that i write in a line of information. I have to make my program in MVC model, and this is the View part. The information is in Model, that's why i'm having this function to transfer the information.
 
Back
Top