Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

excel styled table?

843804Nov 15 2004 — edited Nov 15 2004
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 Nov 15 2004
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 Nov 15 2004
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 Nov 15 2004
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);
	}
}
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 13 2004
Added on Nov 15 2004
3 comments
326 views