Java by API/javax.swing/JTable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
- 1 JTable: addColumnSelectionInterval(int index0, int index1)
- 2 JTable.AUTO_RESIZE_ALL_COLUMNS
- 3 JTable.AUTO_RESIZE_LAST_COLUMN
- 4 JTable.AUTO_RESIZE_NEXT_COLUMN
- 5 JTable.AUTO_RESIZE_OFF
- 6 JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
- 7 JTable: changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
- 8 JTable: clearSelection()
- 9 JTable: configureEnclosingScrollPane() "Headerless Table"
- 10 JTable: createDefaultColumnsFromModel()
- 11 JTable: getCellSelectionEnabled()
- 12 JTable: getColumnCount()
- 13 JTable: getColumnModel()
- 14 JTable: getColumnSelectionAllowed()
- 15 JTable: getIntercellSpacing()
- 16 JTable: getRowCount()
- 17 JTable: getRowSelectionAllowed()
- 18 JTable: getSelectedColumns()
- 19 JTable: getSelectedRows()
- 20 JTable: getSelectionModel()
- 21 JTable: getTableHeader()
- 22 JTable: getValueAt(int row, int column)
- 23 JTable: isCellSelected(int row, int column)
- 24 JTable: moveColumn(int column, int targetColumn)
- 25 JTable: print()
- 26 JTable: print(PrintMode p, MessageFormat hFormat, MessageFormat fFormat)
- 27 JTable: removeColumnSelectionInterval(int index0, int index1)
- 28 JTable: removeColumn(TableColumn aColumn)
- 29 JTable: removeRowSelectionInterval(int index0, int index1)
- 30 JTable: selectAll()
- 31 JTable: setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel)
- 32 JTable: setAutoResizeMode(int m)
- 33 JTable: setCellSelectionEnabled(boolean cellSelectionEnabled)
- 34 JTable: setColumnSelectionAllowed(boolean columnSelectionAllowed)
- 35 JTable: setDefaultEditor(Class columnClass, TableCellEditor editor)
- 36 JTable: setDefaultRenderer(Class columnClass, TableCellRenderer renderer)
- 37 JTable: setFocusable(boolean focusable)
- 38 JTable: setGridColor(Color gridColor)
- 39 JTable: setIntercellSpacing(Dimension intercellSpacing)
- 40 JTable: setPreferredScrollableViewportSize(Dimension size)
- 41 JTable: setRowHeight(int rowHeight)
- 42 JTable: setRowSelectionAllowed(boolean rowSelectionAllowed)
- 43 JTable: setRowSelectionInterval(int index0, int index1)
- 44 JTable: setRowSorter(RowSorter<? extends TableModel> arg0)
- 45 JTable: setSelectionMode(int m)
- 46 JTable: setShowGrid(boolean showGrid)
- 47 JTable: setShowHorizontalLines(boolean showHorizontalLines)
- 48 JTable: setShowVerticalLines(boolean showVerticalLines)
- 49 JTable: setTableHeader(JTableHeader tableHeader)
- 50 JTable: setValueAt(Object aValue, int row, int column)
- 51 new JTable(Object name, Object[][] data)
- 52 new JTable(TableModel dm)
- 53 new JTable(Vector rowData, Vector columnNames)
- 54 TableHeader.cellBorder
- 55 Table.scrollPaneBorder
JTable: addColumnSelectionInterval(int index0, int index1)
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
// Select an additional range of columns - columns 1 to 2
table.addColumnSelectionInterval(1, 2);
}
}
JTable.AUTO_RESIZE_ALL_COLUMNS
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF,
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
final JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column",
"Resize Off", "Resize Subsequent Columns" };
final JComboBox resizeModeComboBox = new JComboBox(modes);
int defaultMode = 4;
table.setAutoResizeMode(modeKey[defaultMode]);
resizeModeComboBox.setSelectedIndex(defaultMode);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int index = resizeModeComboBox.getSelectedIndex();
table.setAutoResizeMode(modeKey[index]);
}
};
resizeModeComboBox.addItemListener(itemListener);
JFrame frame = new JFrame("Resizing Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(resizeModeComboBox, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable.AUTO_RESIZE_LAST_COLUMN
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF,
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
final JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column",
"Resize Off", "Resize Subsequent Columns" };
final JComboBox resizeModeComboBox = new JComboBox(modes);
int defaultMode = 4;
table.setAutoResizeMode(modeKey[defaultMode]);
resizeModeComboBox.setSelectedIndex(defaultMode);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int index = resizeModeComboBox.getSelectedIndex();
table.setAutoResizeMode(modeKey[index]);
}
};
resizeModeComboBox.addItemListener(itemListener);
JFrame frame = new JFrame("Resizing Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(resizeModeComboBox, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable.AUTO_RESIZE_NEXT_COLUMN
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF,
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
final JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column",
"Resize Off", "Resize Subsequent Columns" };
final JComboBox resizeModeComboBox = new JComboBox(modes);
int defaultMode = 4;
table.setAutoResizeMode(modeKey[defaultMode]);
resizeModeComboBox.setSelectedIndex(defaultMode);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int index = resizeModeComboBox.getSelectedIndex();
table.setAutoResizeMode(modeKey[index]);
}
};
resizeModeComboBox.addItemListener(itemListener);
JFrame frame = new JFrame("Resizing Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(resizeModeComboBox, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable.AUTO_RESIZE_OFF
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass {
public static void main(final String args[]) {
final Object rows[][] = { { "one", "1" }, { "two", "2" },
{ "three", "3" } };
final String headers[] = { "English", "Digit" };
TableModel fixedColumnModel = new AbstractTableModel() {
public int getColumnCount() {
return 2;
}
public String getColumnName(int column) {
return headers[column];
}
public int getRowCount() {
return 3;
}
public Object getValueAt(int row, int column) {
return rows[row][column];
}
};
JFrame frame = new JFrame("Scrollless Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(fixedColumnModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF,
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
final JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column",
"Resize Off", "Resize Subsequent Columns" };
final JComboBox resizeModeComboBox = new JComboBox(modes);
int defaultMode = 4;
table.setAutoResizeMode(modeKey[defaultMode]);
resizeModeComboBox.setSelectedIndex(defaultMode);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int index = resizeModeComboBox.getSelectedIndex();
table.setAutoResizeMode(modeKey[index]);
}
};
resizeModeComboBox.addItemListener(itemListener);
JFrame frame = new JFrame("Resizing Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(resizeModeComboBox, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable: changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
int row = 2;
int col = 1;
boolean toggle = false;
boolean extend = false;
table.changeSelection(row, col, toggle, extend);
}
}
JTable: clearSelection()
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
table.clearSelection();
}
}
JTable: configureEnclosingScrollPane() "Headerless Table"
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.UIManager;
public class MainClass {
public static void main(String args[]) {
Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
Object headers[] = { "Upper", "Lower" };
JTable table = new JTable(rows, headers);
table.setTableHeader(null);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
class HeaderlessTable extends JTable {
public HeaderlessTable(Object rowData[][], Object columnNames[]) {
super(rowData, columnNames);
}
protected void configureEnclosingScrollPane() {
Container container = getParent();
if (container instanceof JViewport) {
Container viewParent = container.getParent();
if (viewParent instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)viewParent;
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this) {
return;
}
scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
}
}
}
}
JTable: createDefaultColumnsFromModel()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class MainClass extends JFrame {
public MainClass() {
super("Abstract Model JTable Test");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
DefaultTableModel dtm = new DefaultTableModel(new String[][] { { "1", "2", "3" },
{ "4", "5", "6" } }, new String[] { "Names", "In", "Order" });
SortingColumnModel scm = new SortingColumnModel();
JTable jt = new JTable(dtm, scm);
jt.createDefaultColumnsFromModel();
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass ce = new MainClass();
ce.setVisible(true);
}
}
class SortingColumnModel extends DefaultTableColumnModel {
public void addColumn(TableColumn tc) {
super.addColumn(tc);
int newIndex = sortedIndexOf(tc);
if (newIndex != tc.getModelIndex()) {
moveColumn(tc.getModelIndex(), newIndex);
}
}
protected int sortedIndexOf(TableColumn tc) {
int stop = getColumnCount();
String name = tc.getHeaderValue().toString();
for (int i = 0; i < stop; i++) {
if (name.rupareTo(getColumn(i).getHeaderValue().toString()) <= 0) {
return i;
}
}
return stop;
}
}
JTable: getCellSelectionEnabled()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Selection Model Test");
setSize(450, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel() {
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "0";
}
};
final JTable jt = new JTable(tm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("Selected Column Indices:");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getSelectionModel();
JLabel rowCounter = new JLabel("Selected Row Indices:");
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
rowPanel.add(new JLabel("Selected rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
MainClass se = new MainClass();
se.setVisible(true);
}
public class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
debugger = target;
model = lsm;
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
.getMaxSelectionIndex());
if (selection.length == 0) {
buf.append("none");
} else {
for (int i = 0; i < selection.length - 1; i++) {
buf.append(selection[i]);
buf.append(", ");
}
buf.append(selection[selection.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectedIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
}
JTable: getColumnCount()
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
String[] columnNames = { "col1", "col2" };
JTable table = new JTable(cellData, columnNames);
int rows = table.getRowCount();
int cols = table.getColumnCount();
System.out.println(rows);
System.out.println(cols);
}
}
JTable: getColumnModel()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
JFrame frame = new JFrame("Tooltip Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(rows, headers);
JLabel headerRenderer = new DefaultTableCellRenderer();
String columnName = table.getModel().getColumnName(0);
headerRenderer.setText(columnName);
headerRenderer.setToolTipText("Wave");
TableColumnModel columnModel = table.getColumnModel();
TableColumn column = columnModel.getColumn(0);
column.setHeaderRenderer((TableCellRenderer) headerRenderer);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable: getColumnSelectionAllowed()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Selection Model Test");
setSize(450, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel() {
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "0";
}
};
final JTable jt = new JTable(tm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("Selected Column Indices:");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getSelectionModel();
JLabel rowCounter = new JLabel("Selected Row Indices:");
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
rowPanel.add(new JLabel("Selected rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
MainClass se = new MainClass();
se.setVisible(true);
}
public class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
debugger = target;
model = lsm;
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
.getMaxSelectionIndex());
if (selection.length == 0) {
buf.append("none");
} else {
for (int i = 0; i < selection.length - 1; i++) {
buf.append(selection[i]);
buf.append(", ");
}
buf.append(selection[selection.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectedIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
}
JTable: getIntercellSpacing()
import java.awt.Dimension;
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
Dimension d = table.getIntercellSpacing();
// d.width == 1, d.height == 1
}
}
JTable: getRowCount()
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
String[] columnNames = { "col1", "col2" };
JTable table = new JTable(cellData, columnNames);
int rows = table.getRowCount();
int cols = table.getColumnCount();
System.out.println(rows);
System.out.println(cols);
}
}
JTable: getRowSelectionAllowed()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Selection Model Test");
setSize(450, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel() {
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "0";
}
};
final JTable jt = new JTable(tm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("Selected Column Indices:");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getSelectionModel();
JLabel rowCounter = new JLabel("Selected Row Indices:");
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
rowPanel.add(new JLabel("Selected rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
MainClass se = new MainClass();
se.setVisible(true);
}
public class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
debugger = target;
model = lsm;
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
.getMaxSelectionIndex());
if (selection.length == 0) {
buf.append("none");
} else {
for (int i = 0; i < selection.length - 1; i++) {
buf.append(selection[i]);
buf.append(", ");
}
buf.append(selection[selection.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectedIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
}
JTable: getSelectedColumns()
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
if (table.getColumnSelectionAllowed() && !table.getRowSelectionAllowed()) {
int[] vColIndices = table.getSelectedColumns();
}
}
}
JTable: getSelectedRows()
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
if (!table.getColumnSelectionAllowed() && table.getRowSelectionAllowed()) {
//
int[] rowIndices = table.getSelectedRows();
}
}
}
JTable: getSelectionModel()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Selection Model Test");
setSize(450, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel() {
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "0";
}
};
final JTable jt = new JTable(tm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("Selected Column Indices:");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getSelectionModel();
JLabel rowCounter = new JLabel("Selected Row Indices:");
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
rowPanel.add(new JLabel("Selected rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
MainClass se = new MainClass();
se.setVisible(true);
}
public class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
debugger = target;
model = lsm;
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
.getMaxSelectionIndex());
if (selection.length == 0) {
buf.append("none");
} else {
for (int i = 0; i < selection.length - 1; i++) {
buf.append(selection[i]);
buf.append(", ");
}
buf.append(selection[selection.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectedIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
}
JTable: getTableHeader()
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
JTableHeader header = table.getTableHeader();
JPanel container = new JPanel(new BorderLayout());
// Add header in NORTH slot
container.add(header, BorderLayout.NORTH);
// Add table itself to CENTER slot
container.add(table, BorderLayout.CENTER);
}
}
JTable: getValueAt(int row, int column)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 3;
int cols = 3;
JTable table = new JTable(rows, cols);
int rowIndex = 1;
int vColIndex = 2;
Object o = table.getValueAt(rowIndex, vColIndex);
}
}
JTable: isCellSelected(int row, int column)
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
if (table.getCellSelectionEnabled()) {
// In the other modes, the set of selected cells can be retrieved using
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// Get the min and max ranges of selected cells
int rowIndexStart = table.getSelectedRow();
int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
int colIndexStart = table.getSelectedColumn();
int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
// Check each cell in the range
for (int r = rowIndexStart; r <= rowIndexEnd; r++) {
for (int c = colIndexStart; c <= colIndexEnd; c++) {
if (table.isCellSelected(r, c)) {
System.out.println("cell is selected");
}
}
}
}
}
}
JTable: moveColumn(int column, int targetColumn)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.moveColumn(table.getColumnCount() - 1, 0);
}
}
JTable: print()
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Main {
public static void main(String[] args) throws Exception {
String[] columns = { "Name", "Age" };
Object[][] content = { { "R", new Integer(24) }, { "A", new Integer(25) },
{ "J", new Integer(30) }, { "A", new Integer(32) }, { "S", new Integer(27) } };
JTable table = new JTable(content, columns);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel(new GridLayout(2, 0));
jPanel.setOpaque(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
jPanel.add(new JScrollPane(table));
/* Add the panel to the JFrame */
frame.add(jPanel);
/* Display the JFrame window */
frame.pack();
frame.setVisible(true);
table.print();
}
}
JTable: print(PrintMode p, MessageFormat hFormat, MessageFormat fFormat)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
JFrame frame = new JFrame("Table Printing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Print");
ActionListener printAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
MessageFormat headerFormat = new MessageFormat("Page {0}");
MessageFormat footerFormat = new MessageFormat("- {0} -");
table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
} catch (PrinterException pe) {
System.err.println("Error printing: " + pe.getMessage());
}
}
};
button.addActionListener(printAction);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable: removeColumnSelectionInterval(int index0, int index1)
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
table.removeColumnSelectionInterval(0, 1);
}
}
JTable: removeColumn(TableColumn aColumn)
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
public static void main(String[] argv) throws Exception {
DefaultTableModel model = new MyDefaultTableModel();
JTable table = new JTable(model);
table.setModel(model);
model.addColumn("Col1");
model.addColumn("Col2");
model.addColumn("Col3");
model.addRow(new Object[] { "v1" });
table.removeColumn(table.getColumnModel().getColumn(0));
}
}
class MyDefaultTableModel extends DefaultTableModel {
public Vector getColumnIdentifiers() {
return columnIdentifiers;
}
}
JTable: removeRowSelectionInterval(int index0, int index1)
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.removeRowSelectionInterval(0, 1);
}
}
JTable: selectAll()
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
//
table.selectAll();
}
}
JTable: setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel)
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
public static void main(String[] argv) throws Exception {
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
// Disable autoCreateColumnsFromModel
table.setAutoCreateColumnsFromModel(false);
}
}
JTable: setAutoResizeMode(int m)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass {
public static void main(final String args[]) {
final Object rows[][] = { { "one", "1" }, { "two", "2" },
{ "three", "3" } };
final String headers[] = { "English", "Digit" };
TableModel fixedColumnModel = new AbstractTableModel() {
public int getColumnCount() {
return 2;
}
public String getColumnName(int column) {
return headers[column];
}
public int getRowCount() {
return 3;
}
public Object getValueAt(int row, int column) {
return rows[row][column];
}
};
JFrame frame = new JFrame("Scrollless Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(fixedColumnModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable: setCellSelectionEnabled(boolean cellSelectionEnabled)
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Selection Model Test");
setSize(450, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel() {
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "0";
}
};
final JTable jt = new JTable(tm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("Selected Column Indices:");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getSelectionModel();
JLabel rowCounter = new JLabel("Selected Row Indices:");
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
rowPanel.add(new JLabel("Selected rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
MainClass se = new MainClass();
se.setVisible(true);
}
public class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
debugger = target;
model = lsm;
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
.getMaxSelectionIndex());
if (selection.length == 0) {
buf.append("none");
} else {
for (int i = 0; i < selection.length - 1; i++) {
buf.append(selection[i]);
buf.append(", ");
}
buf.append(selection[selection.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectedIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
}
JTable: setColumnSelectionAllowed(boolean columnSelectionAllowed)
import java.awt.BorderLayout;
import java.io.File;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass extends JFrame {
String titles[] = new String[] { "Directory?", "File Name", "Read?", "Write?", "Size",
"Last Modified" };
public MainClass() {
super("Simple JTable Test");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
File pwd = new File(".");
Object[][] stats = getFileStats(pwd);
JTable jt = new JTable(stats, titles);
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setColumnSelectionAllowed(true);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public Object[][] getFileStats(File dir) {
String files[] = dir.list();
Object[][] results = new Object[files.length][titles.length];
for (int i = 0; i < files.length; i++) {
File tmp = new File(files[i]);
results[i][0] = new Boolean(tmp.isDirectory());
results[i][1] = tmp.getName();
results[i][2] = new Boolean(tmp.canRead());
results[i][3] = new Boolean(tmp.canWrite());
results[i][4] = new Long(tmp.length());
results[i][5] = new Date(tmp.lastModified());
}
return results;
}
public static void main(String args[]) {
MainClass tf = new MainClass();
tf.setVisible(true);
}
}
JTable: setDefaultEditor(Class columnClass, TableCellEditor editor)
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
public class MainClass extends JFrame {
ColorName colors[] = { new ColorName("Red"), new ColorName("Green"), new ColorName("Blue"),
new ColorName("Black"), new ColorName("White") };
public MainClass() {
super("Table With DefaultCellEditor Example");
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(new AbstractTableModel() {
ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0],
colors[1], colors[2], colors[3], colors[4] };
public int getColumnCount() {
return 3;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int r, int c) {
switch (c) {
case 0:
return (r + 1) + ".";
case 1:
return "Some pithy quote #" + r;
case 2:
return data[r];
}
return "Bad Column";
}
public Class getColumnClass(int c) {
if (c == 2)
return ColorName.class;
return String.class;
}
public boolean isCellEditable(int r, int c) {
return c == 2;
}
public void setValueAt(Object value, int r, int c) {
data[r] = (ColorName) value;
}
});
table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors)));
table.setDefaultRenderer(ColorName.class, new DefaultTableCellRenderer());
table.setRowHeight(20);
getContentPane().add(new JScrollPane(table));
}
public static void main(String args[]) {
MainClass ex = new MainClass();
ex.setVisible(true);
}
public class ColorName {
String cname;
public ColorName(String name) {
cname = name;
}
public String toString() {
return cname;
}
}
}
JTable: setDefaultRenderer(Class columnClass, TableCellRenderer renderer)
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
public class MainClass extends JFrame {
ColorName colors[] = { new ColorName("Red"), new ColorName("Green"), new ColorName("Blue"),
new ColorName("Black"), new ColorName("White") };
public MainClass() {
super("Table With DefaultCellEditor Example");
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(new AbstractTableModel() {
ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0],
colors[1], colors[2], colors[3], colors[4] };
public int getColumnCount() {
return 3;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int r, int c) {
switch (c) {
case 0:
return (r + 1) + ".";
case 1:
return "Some pithy quote #" + r;
case 2:
return data[r];
}
return "Bad Column";
}
public Class getColumnClass(int c) {
if (c == 2)
return ColorName.class;
return String.class;
}
public boolean isCellEditable(int r, int c) {
return c == 2;
}
public void setValueAt(Object value, int r, int c) {
data[r] = (ColorName) value;
}
});
table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors)));
table.setDefaultRenderer(ColorName.class, new DefaultTableCellRenderer());
table.setRowHeight(20);
getContentPane().add(new JScrollPane(table));
}
public static void main(String args[]) {
MainClass ex = new MainClass();
ex.setVisible(true);
}
public class ColorName {
String cname;
public ColorName(String name) {
cname = name;
}
public String toString() {
return cname;
}
}
}
JTable: setFocusable(boolean focusable)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setFocusable(false);
table.setCellSelectionEnabled(false);
}
}
JTable: setGridColor(Color gridColor)
import java.awt.Color;
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.setGridColor(Color.red);
}
}
JTable: setIntercellSpacing(Dimension intercellSpacing)
import java.awt.Dimension;
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
int gapWidth = 10;
int gapHeight = 4;
table.setIntercellSpacing(new Dimension(gapWidth, gapHeight));
}
}
JTable: setPreferredScrollableViewportSize(Dimension size)
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
String[] headings = { "Name", "Customer ID", "Order #", "Status" };
Object[][] data = {
{ "A", new Integer(3), "0", new Date() },
{ "B", new Integer(6), "4", new Date() },
{ "C", new Integer(9), "9", new Date() },
{ "D", new Integer(7), "1", new Date() },
{ "E", new Integer(4), "1", new Date() },
{ "F", new Integer(8), "2", new Date() },
{ "G", new Integer(6), "1", new Date() }
};
JTable jtabOrders = new JTable(data, headings);
Main() {
JFrame jfrm = new JFrame("JTable Demo");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(460, 180);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane jscrlp = new JScrollPane(jtabOrders);
jtabOrders.setPreferredScrollableViewportSize(new Dimension(420, 60));
jtabOrders.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jfrm.setVisible(true);
jtabOrders.setColumnSelectionAllowed(false);
jtabOrders.setRowSelectionAllowed(true);
}
public static void main(String args[]) {
new Main();
}
}
JTable: setRowHeight(int rowHeight)
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
public class MainClass extends JFrame {
ColorName colors[] = { new ColorName("Red"), new ColorName("Green"), new ColorName("Blue"),
new ColorName("Black"), new ColorName("White") };
public MainClass() {
super("Table With DefaultCellEditor Example");
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(new AbstractTableModel() {
ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0],
colors[1], colors[2], colors[3], colors[4] };
public int getColumnCount() {
return 3;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int r, int c) {
switch (c) {
case 0:
return (r + 1) + ".";
case 1:
return "Some pithy quote #" + r;
case 2:
return data[r];
}
return "Bad Column";
}
public Class getColumnClass(int c) {
if (c == 2)
return ColorName.class;
return String.class;
}
public boolean isCellEditable(int r, int c) {
return c == 2;
}
public void setValueAt(Object value, int r, int c) {
data[r] = (ColorName) value;
}
});
table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors)));
table.setDefaultRenderer(ColorName.class, new DefaultTableCellRenderer());
table.setRowHeight(20);
getContentPane().add(new JScrollPane(table));
}
public static void main(String args[]) {
MainClass ex = new MainClass();
ex.setVisible(true);
}
public class ColorName {
String cname;
public ColorName(String name) {
cname = name;
}
public String toString() {
return cname;
}
}
}
JTable: setRowSelectionAllowed(boolean rowSelectionAllowed)
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Selection Model Test");
setSize(450, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableModel tm = new AbstractTableModel() {
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int r, int c) {
return "0";
}
};
final JTable jt = new JTable(tm);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
buttonPanel = new JPanel();
final JCheckBox cellBox, columnBox, rowBox;
cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
cellBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setCellSelectionEnabled(cellBox.isSelected());
columnBox.setSelected(jt.getColumnSelectionAllowed());
rowBox.setSelected(jt.getRowSelectionAllowed());
}
});
columnBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setColumnSelectionAllowed(columnBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
rowBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jt.setRowSelectionAllowed(rowBox.isSelected());
cellBox.setSelected(jt.getCellSelectionEnabled());
}
});
buttonPanel.add(new JLabel("Selections allowed:"));
buttonPanel.add(cellBox);
buttonPanel.add(columnBox);
buttonPanel.add(rowBox);
columnPanel = new JPanel();
ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
JLabel columnCounter = new JLabel("Selected Column Indices:");
csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
columnPanel.add(new JLabel("Selected columns:"));
columnPanel.add(columnCounter);
rowPanel = new JPanel();
ListSelectionModel rsm = jt.getSelectionModel();
JLabel rowCounter = new JLabel("Selected Row Indices:");
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
rowPanel.add(new JLabel("Selected rows:"));
rowPanel.add(rowCounter);
controlPanel = new JPanel(new GridLayout(0, 1));
controlPanel.add(buttonPanel);
controlPanel.add(columnPanel);
controlPanel.add(rowPanel);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
public static void main(String args[]) {
MainClass se = new MainClass();
se.setVisible(true);
}
public class SelectionDebugger implements ListSelectionListener {
JLabel debugger;
ListSelectionModel model;
public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
debugger = target;
model = lsm;
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
StringBuffer buf = new StringBuffer();
int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
.getMaxSelectionIndex());
if (selection.length == 0) {
buf.append("none");
} else {
for (int i = 0; i < selection.length - 1; i++) {
buf.append(selection[i]);
buf.append(", ");
}
buf.append(selection[selection.length - 1]);
}
debugger.setText(buf.toString());
}
}
protected int[] getSelectedIndices(int start, int stop) {
if ((start == -1) || (stop == -1)) {
return new int[0];
}
int guesses[] = new int[stop - start + 1];
int index = 0;
for (int i = start; i <= stop; i++) {
if (model.isSelectedIndex(i)) {
guesses[index++] = i;
}
}
int realthing[] = new int[index];
System.arraycopy(guesses, 0, realthing, 0, index);
return realthing;
}
}
}
JTable: setRowSelectionInterval(int index0, int index1)
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.setRowSelectionInterval(0, 0);
}
}
JTable: setRowSorter(RowSorter<? extends TableModel> arg0)
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class Main extends JFrame {
public Main() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] columns = { "Item", "Price" };
Object[][] rows = { { "Potatoes", 10.98 }, { "Magazine", 7.99 },
{ "Can of soup", 0.89 }, { "DVD movie", 39.99 } };
TableModel model = new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
if (column >= 0 && column <= getColumnCount())
return getValueAt(0, column).getClass();
else
return Object.class;
}
};
JTable table = new JTable(model);
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
getContentPane().add(new JScrollPane(table));
setSize(200, 150);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
JTable: setSelectionMode(int m)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass {
public static void main(final String args[]) {
final Object rows[][] = { { "one", "1" }, { "two", "2" },
{ "three", "3" } };
final String headers[] = { "English", "Digit" };
TableModel fixedColumnModel = new AbstractTableModel() {
public int getColumnCount() {
return 2;
}
public String getColumnName(int column) {
return headers[column];
}
public int getRowCount() {
return 3;
}
public Object getValueAt(int row, int column) {
return rows[row][column];
}
};
JFrame frame = new JFrame("Scrollless Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(fixedColumnModel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable: setShowGrid(boolean showGrid)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.setShowGrid(true);
}
}
JTable: setShowHorizontalLines(boolean showHorizontalLines)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.setShowGrid(false);
table.setShowHorizontalLines(true);
}
}
JTable: setShowVerticalLines(boolean showVerticalLines)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.setShowGrid(false);
table.setShowVerticalLines(true);
}
}
JTable: setTableHeader(JTableHeader tableHeader)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
Object columnNames[] = { "1", "2", "3" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.setTableHeader(null);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTable: setValueAt(Object aValue, int row, int column)
import javax.swing.JTable;
public class Main {
public static void main(String[] argv) throws Exception {
int rows = 3;
int cols = 3;
JTable table = new JTable(rows, cols);
table.setValueAt("New Value", 0, 0);
}
}
new JTable(Object name, Object[][] data)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JTable Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = { { "A", "Name 1", "38.94" },
{ "B", "Name 2", "7.70" },
{ "C", "Name 3", "112.65" } };
Object columns[] = { "Symbol", "Name", "Price" };
JTable table = new JTable(rows, columns);
JScrollPane scrollPane = new JScrollPane(table);
f.add(scrollPane, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
new JTable(TableModel dm)
import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
JComboBox comboBox = new JComboBox(rows[0]);
comboBox.setMaximumRowCount(4);
TableCellEditor editor = new DefaultCellEditor(comboBox);
JFrame frame = new JFrame("JTable Anatomy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(new DefaultTableModel(rows, headers));
table.getColumnModel().getColumn(1).setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
new JTable(Vector rowData, Vector columnNames)
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector<String> rowOne = new Vector<String>();
rowOne.addElement("Row1-Column1");
rowOne.addElement("Row1-Column2");
rowOne.addElement("Row1-Column3");
Vector<String> rowTwo = new Vector<String>();
rowTwo.addElement("Row2-Column1");
rowTwo.addElement("Row2-Column2");
rowTwo.addElement("Row2-Column3");
Vector<Vector> rowData = new Vector<Vector>();
rowData.addElement(rowOne);
rowData.addElement(rowTwo);
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Column One");
columnNames.addElement("Column Two");
columnNames.addElement("Column Three");
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
TableHeader.cellBorder
import java.awt.BorderLayout;
import java.awt.ruponent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
JFrame frame = new JFrame("Label Header");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");
JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER);
headerLabel1.setBorder(headerBorder);
JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER);
headerLabel2.setBorder(headerBorder);
TableCellRenderer renderer = new JComponentTableCellRenderer();
TableColumnModel columnModel = table.getColumnModel();
TableColumn column0 = columnModel.getColumn(0);
TableColumn column1 = columnModel.getColumn(1);
column0.setHeaderRenderer(renderer);
column0.setHeaderValue(headerLabel1);
column1.setHeaderRenderer(renderer);
column1.setHeaderValue(headerLabel2);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
class JComponentTableCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return (JComponent)value;
}
}
Table.scrollPaneBorder
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.UIManager;
public class MainClass {
public static void main(String args[]) {
Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
Object headers[] = { "Upper", "Lower" };
JTable table = new JTable(rows, headers);
table.setTableHeader(null);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
class HeaderlessTable extends JTable {
public HeaderlessTable(Object rowData[][], Object columnNames[]) {
super(rowData, columnNames);
}
protected void configureEnclosingScrollPane() {
Container container = getParent();
if (container instanceof JViewport) {
Container viewParent = container.getParent();
if (viewParent instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)viewParent;
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this) {
return;
}
scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
}
}
}
}