3 Replies Latest reply on Nov 16, 2004 3:42 AM by camickr

    excel styled table?

    843804
      I want to make a table similar to the look of what excel looks. Meaning the first column is grey and shows numbers 1 .... n and the first row is also grey with 1 .... m.

      AFAIK this kind of control is called a grid, but there is no component called JGrid. The LayoutManager is not GridLayout is not what i want, because i would have to write the whole functionality of the table to it. Any suggestions on how to do this?
        • 1. Re: excel styled table?
          843804
          Seems like posts in this forum are immutable ...

          The last sentence should be:
          The LayoutManager "GridLayout" is not what i want, because i would have to write the whole functionality of the table myself. Any suggestions on how to do this?
          • 2. Re: excel styled table?
            843804
            You need to use the JTableHeader. It has nothing to do with layout. Look at javax.swing.table.JTableHeader. Also, look at the table tutorial.

            -Kevin
            • 3. Re: excel styled table?
              camickr
              import java.awt.*;
              import javax.swing.*;
              
              public class TableRowHeader extends JList
              {
                   private JTable table;
              
                   public TableRowHeader(JTable table)
                   {
                        this.table = table;
              
                        setAutoscrolls( false );
                        setCellRenderer(new RowHeaderRenderer());
                        setFixedCellHeight(table.getRowHeight());
                        setFixedCellWidth(50);
                        setFocusable( false );
                        setModel( new TableListModel() );
                        setOpaque( false );
                        setSelectionModel( table.getSelectionModel() );
                   }
              
                   /*
                    *  Use the table to implement the ListModel
                    */
                   class TableListModel extends AbstractListModel
                   {
                        public int getSize()
                        {
                             return table.getRowCount();
                        }
              
                        public Object getElementAt(int index)
                        {
                             return String.valueOf(index + 1);
                        }
                   }
              
                   /*
                    *  Use the table row header properties to render each cell
                    */
                   class RowHeaderRenderer extends DefaultListCellRenderer
                   {
                        RowHeaderRenderer()
                        {
                             setHorizontalAlignment(CENTER);
                             setOpaque(true);
                             setBorder(UIManager.getBorder("TableHeader.cellBorder"));
                             setFont(table.getTableHeader().getFont());
                             setBackground(table.getTableHeader().getBackground());
                             setForeground(table.getTableHeader().getForeground());
                        }
              
                        public Component getListCellRendererComponent(
                             JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                        {
                             if (isSelected)
                             {
                                  setBackground( table.getSelectionBackground() );
                             }
                             else
                             {
                                  setBackground( table.getTableHeader().getBackground() );
                             }
              
                             setText( (value == null) ? "" : value.toString() );
              
                             return this;
                        }
                   }
              
                   public static void main(String[] args)
                   {
                        JTable table = new JTable( 5, 10 );
                        JScrollPane scrollPane = new JScrollPane( table );
                        scrollPane.setRowHeaderView( new TableRowHeader( table) );
              
                        JFrame frame = new JFrame("Row Header Test");
                        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
                        frame.pack();
                        frame.setLocationRelativeTo( null );
                        frame.setVisible(true);
                   }
              }