A Table with Row Headers
// RowHeaderTable.java
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class RowHeaderTable extends JFrame {
public RowHeaderTable( ) {
super(”Row Header Test”);
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel( ) {
String data[] = {”", “a”, “b”, “c”, “d”, “e”};
String headers[] = {”Row #”, “Column 1″, “Column 2″, “Column 3″,
“Column 4″, “Column 5″};
public int getColumnCount( ) { return data.length; }
public int getRowCount( ) { return 1000; }
public String getColumnName(int col) { return headers[col]; }
// Synthesize some entries using the data values and the row number.
public Object getValueAt(int row, int col) {
return data[col] + row;
}
};
// Create a column model for the main table. This model ignores the first
// column added and sets a minimum width of 150 pixels for all others.
TableColumnModel cm = new DefaultTableColumnModel( ) {
boolean first = true;
public void addColumn(TableColumn tc) {
// Drop the first column, which will be the row header.
if (first) { first = false; return; }
tc.setMinWidth(150); // Just for looks, really…
super.addColumn(tc);
}
};
// Create a column model that will serve as our row header table. This model
// picks a maximum width and stores only the first column.
TableColumnModel rowHeaderModel = new DefaultTableColumnModel( ) {
boolean first = true;
public void addColumn(TableColumn tc) {
if (first) {
tc.setMaxWidth(tc.getPreferredWidth( ));
super.addColumn(tc);
first = false;
}
// Drop the rest of the columns; this is the header column only.
}
};
JTable jt = new JTable(tm, cm);
// Set up the header column and hook it up to everything.
JTable headerColumn = new JTable(tm, rowHeaderModel);
jt.createDefaultColumnsFromModel( );
headerColumn.createDefaultColumnsFromModel( );
// Make sure that selections between the main table and the header stay in sync
// (by sharing the same model).
jt.setSelectionModel(headerColumn.getSelectionModel( ));
// Make the header column look pretty.
// headerColumn.setBorder(BorderFactory.createEtchedBorder( ));
headerColumn.setBackground(Color.lightGray);
headerColumn.setColumnSelectionAllowed(false);
headerColumn.setCellSelectionEnabled(false);
// Put it in a viewport that we can control.
JViewport jv = new JViewport( );
jv.setView(headerColumn);
jv.setPreferredSize(headerColumn.getMaximumSize( ));
// Without shutting off autoResizeMode, our tables won’t scroll correctly
// (horizontally, anyway).
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// We have to manually attach the row headers, but after that, the scroll
// pane keeps them in sync.
JScrollPane jsp = new JScrollPane(jt);
jsp.setRowHeader(jv);
jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER,
headerColumn.getTableHeader( ));
getContentPane( ).add(jsp, BorderLayout.CENTER);
}
public static void main(String args[]) {
RowHeaderTable rht = new RowHeaderTable( );
rht.setVisible(true);
}
}
Related Websites -
Applying borders The border lines around each cell are very important because they separate the data in each cell. By default (without a table style) there’s a 1-point border around each side of each cell, but you can make some or all borders thicker, a different line style (dashed, for example), a...... -
Track Your Money with Excel Many people utilize the software program known as Excel to set up their budget as well as to manage it. This is a really easy way for you to get started and it is definitely going to be able to put you on the proper track for creating a financial...... - 2 soccer games in a row, how to keep in top shape?plz answer very important? hello asked: 2 soccer games in a row, how to keep in top shape?plz answer very important? 2 soccer games in a row, how to keep in top shape? this winter on saturday's i will probaly be playing at around 4pm then again in another league at 6 pm, how......
-
Guide to Buying a Dobro Dobros, which are resonator guitars, are very interesting musical instruments that seem to come and go in terms of popularity. Choosing the right Dobro when you first begin playing is a really important decision, because quality is absolutely everything when it comes to playing an instrument as unique and intricate...... -
Drawing a table I’ve saved the most fun method for last. Drawing a table enables you to use your mouse pointer like a pencil to create every row and column in the table in exactly the positions you want. You can even create unequal numbers of rows and columns. This method is a......


JXL17
Thursday, 22nd October 2009 at 15:06
Lamson’s Grocery store where Gilbert works. ,
Red83
Friday, 23rd October 2009 at 12:52
Jill Pangallo has generously offered to put together digital images on a CD to project during the evening. ,