Skip to main content

Problem whith JTable

1 reply [Last post]
momo
Offline
Joined: 2004-05-17

Hallo everybody,
I have a serious problem with the cell renderer.
I have a simpleJTable :

<br />
package forum_jtable;</p>
<p>// Class SimpleJTable //<br />
import java.util.*;<br />
import java.awt.*;<br />
import java.awt.event.*;<br />
import javax.swing.*;<br />
import javax.swing.table.*;</p>
<p>public class SimpleJTable extends JFrame{</p>
<p>  public SimpleJTable() {</p>
<p>    Object[] columnNames = {  "Name",  "Age"  };<br />
    Object[][] rowData   = {<br />
          { "Gil   ", "17" },<br />
          { "Alin  ", "13" },<br />
          { "Michel", "20" },<br />
          { "Jose  ", "24" },<br />
          { "Arnaud", "31" },<br />
          { "Didier", "40" },<br />
          { "Hanks ", "19" }<br />
                           };</p>
<p>   CustomTableModel ctm = new CustomTableModel(rowData, columnNames);<br />
   ColorRenderer CR     = new ColorRenderer();<br />
   // Cr�ation d'un objet JTable<br />
   JTable t = new JTable(ctm);</p>
<p>   /***************************************************************************/<br />
   /**/  t.setDefaultRenderer(String.class, new StringRenderer());    /**/<br />
   /**/  t.setDefaultRenderer(String.class, CR);                                /**/<br />
   /**/  CR.setRowColor(0,Color.cyan);                                           /**/<br />
   /*************************************************************************/</p>
<p>   JScrollPane sp = new JScrollPane(t);<br />
   JPanel p = new JPanel(new BorderLayout());<br />
   p.add(sp);<br />
   this.setContentPane(p);<br />
  }</p>
<p>  public static void main(String[] args)<br />
   {<br />
    SimpleJTable st = new SimpleJTable();<br />
    st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
    st.setSize(400,250);<br />
    st.setTitle("SimpleJTable");<br />
    st.show();<br />
   }<br />
}</p>
<p>and the 2sd Class is : CustomTableModel<br />
package forum_jtable;</p>
<p>// Class CustomTableModel //<br />
import java.awt.*;<br />
import javax.swing.*;<br />
import javax.swing.table.*;</p>
<p>class CustomTableModel extends AbstractTableModel<br />
   {<br />
      private Object[][] rowData;    //Lignes (Enregistrements)<br />
      private Object[] columnNames;  // Colonnes</p>
<p>   public CustomTableModel(Object[][] rowData, Object[] columnNames)<br />
      {<br />
         this.rowData = rowData;<br />
         this.columnNames = columnNames;<br />
      }<br />
      public int getColumnCount()  {<br />
        return columnNames.length;   }<br />
      public int getRowCount()     {<br />
        return rowData.length;       }<br />
      public String getColumnName(int idx)  {<br />
        return (String)columnNames[idx];}<br />
      public Object getValueAt(int row, int col){<br />
        return rowData[row][col];   }<br />
      public Class getColumnClass(int col){<br />
        return getValueAt(0, col).getClass();}<br />
      public void setValueAt(Object value, int row, int col){<br />
         rowData[row][col] = value;<br />
         fireTableCellUpdated(row, col);}<br />
   }<br />
   class StringRenderer extends JLabel implements TableCellRenderer<br />
     {<br />
        StringRenderer()<br />
        {<br />
           super();<br />
           this.setHorizontalAlignment(SwingConstants.CENTER);<br />
           this.setOpaque(true);<br />
        }</p>
<p>        public Component getTableCellRendererComponent(JTable t, Object v,<br />
           boolean isSelected, boolean hasFocus, int row, int column)<br />
        {<br />
           String s = v.toString();<br />
           this.setText(s);<br />
           return this;<br />
        }<br />
     }</p>
<p>and the 3rd Class is : ColorRenderer</p>
<p>package forum_jtable;</p>
<p>// Class ColorRenderer //<br />
import java.awt.*;<br />
import java.util.*;<br />
import javax.swing.*;<br />
import javax.swing.table.*;</p>
<p>public class ColorRenderer extends DefaultTableCellRenderer<br />
{<br />
   Map colors;<br />
   public ColorRenderer(){<br />
          colors = new HashMap();<br />
                          }<br />
   public Component getTableCellRendererComponent(JTable table, Object value,<br />
                      boolean isSelected, boolean hasFocus, int row, int column)<br />
             {<br />
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);<br />
             if (hasFocus)<br />
                 setBackground( Color.gray );<br />
             else<br />
                 setBackground( getCellColor(row, column) );<br />
             return this;<br />
             }</p>
<p>  public Color getCellColor(int row, int column)<br />
               {<br />
                //  Get cell color<br />
                String key = getKey(row, column);<br />
                Object o = colors.get( key );</p>
<p>                if (o != null)<br />
                    return (Color)o;</p>
<p>                //  Get row color<br />
                key = getKey(row, -1);<br />
                o = colors.get( key );<br />
                if (o != null)<br />
                    return (Color)o;</p>
<p>                //  Get column color<br />
                key = getKey(-1, column);<br />
                o = colors.get( key );<br />
                if (o != null)<br />
                    return (Color)o;</p>
<p>                //  Use default<br />
                return null;<br />
               }</p>
<p>               public void setCellColor(int row, int column, Color color)<br />
               {<br />
                       String key = getKey(row, column);<br />
                       colors.put(key, color);<br />
               }</p>
<p>               public void setColumnColor(int column, Color color)<br />
               {<br />
                       setCellColor(-1, column, color);<br />
               }</p>
<p>               public void setRowColor(int row, Color color)<br />
               {<br />
                       setCellColor(row, -1, color);<br />
                }</p>
<p>               private String getKey(int row, int column)<br />
               {<br />
                       return row + ":" + column;<br />
               }<br />
       }<br />

The question . How can make value Cell in the midle (CENTER of Cell) and the first Row other Color
I think the problem is here

<br />
   /***************************************************************************/<br />
   /**/  t.setDefaultRenderer(String.class, new StringRenderer());    /**/<br />
   /**/  t.setDefaultRenderer(String.class, CR);                                /**/<br />
   /**/  CR.setRowColor(0,Color.cyan);                                           /**/<br />
   /*************************************************************************/<br />

I would be very greatful if somebody can help me ..

code tags aded by: admin

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
popxw
Offline
Joined: 2006-02-17

Hi,

You can try this:

CR.setHorizontalAlignment(JLabel.CENTER);

popxw