_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
}
private JPanel makeCenterPanel() {
JPanel centerpan = new JPanel();
centerpan.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 1));
String[] headings = { "Name", "Color", "Composition", "Mass", "Radius",
"Orbit"};
String[][] data = {
{ "Mars", "Red", "Dust", "1.5e10", "2.7e6", "Elliptical"},
{ "Pluto", "Blue", "Rock", "2.3e11", "2.9e7", "Circular"},
{ "Luna", "Green", "Cheese", "1.3e5", "2.3e12", "Square"},
{ "Venus", "White", "Gas", "4.3e5", "2.3e12",
"A funny irregular shape whose name is longer than the table width"},
{ "Jupiter", "Black", "Marshmallow", "4.3e6", "2.3e12",
"Zigzag"},
{ "Neptune", "Purple", "Gas", "1.2e6", "2.4e2", "Elliptical"},
{ "Saturn", "Yellow", "Gas", "1.1e7", "1.4e6", "Circular"}};
/*
* The following inner class overrides the processKeyEvent() method of
* JTable, so that we can display the selected rows and columns.
*/
_table = new JTable(data, headings) {
/*
* Gets called when the user presses a key in the JTable.
*/
public void processKeyEvent(KeyEvent e_) {
super.processKeyEvent(e_);
if (e_.getKeyCode() != KeyEvent.VK_ENTER) return;
int[] rows = getSelectedRows();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < rows.length; i++) {
buf.append(rows[ i]);
buf.append(' ');
}
_selectedRows.setText(buf.toString());
int[] columns = getSelectedColumns();
buf = new StringBuffer();
for (int i = 0; i < columns.length; i++) {
buf.append(columns[ i]);
buf.append(' ');
}
_selectedColumns.setText(buf.toString());
}
};
_table.setPreferredScrollableViewportSize(new Dimension(30, 5));
//_table.setValueAt("Yellow", 5, 2);
//_table.setValueAt("Red", 7, 4);
//_table.setValueAt("Magenta", 1, 5);
JScrollPane scrollPane = new JScrollPane(_table);
TitledBorder border = new TitledBorder(new LineBorder(Color.cyan));
border.setTitle("The Heavenly Bodies");
scrollPane.setViewportBorder(border);
// scrollPane.setSize(25, 6);
centerpan.add(scrollPane);
return centerpan;
}