Forum Stats

  • 3,750,838 Users
  • 2,250,259 Discussions
  • 7,867,174 Comments

Discussions

excel styled table?

843804
843804 Member Posts: 45,751
edited Nov 15, 2004 10:42PM in Swing
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?

Comments

  • 843804
    843804 Member Posts: 45,751
    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?
  • 843804
    843804 Member Posts: 45,751
    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
  • camickr
    camickr Member Posts: 24,931
    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);
    	}
    }
This discussion has been closed.