/*
* TableSelectionDemo.java
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableSelectionDemo extends JFrame implements ActionListener {
private JCheckBox cbCol;
private JCheckBox cbRow;
private JScrollPane jScrollPane1;
private JTable table;
private JToolBar toolbar;
public TableSelectionDemo() {
super("TableSelectionDemo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
toolbar = new JToolBar();
cbRow = new JCheckBox();
cbCol = new JCheckBox();
jScrollPane1 = new JScrollPane();
table = new JTable();
toolbar.setRollover(true);
cbRow.setSelected(true);
cbRow.setText("row selection allowed");
cbRow.setFocusable(false);
cbRow.setHorizontalTextPosition(SwingConstants.CENTER);
cbRow.setVerticalTextPosition(SwingConstants.BOTTOM);
cbRow.addActionListener(this);
toolbar.add(cbRow);
cbCol.setText("column selection allowed");
cbCol.setFocusable(false);
cbCol.setHorizontalTextPosition(SwingConstants.CENTER);
cbCol.setVerticalTextPosition(SwingConstants.BOTTOM);
cbCol.addActionListener(this);
toolbar.add(cbCol);
getContentPane().add(toolbar, BorderLayout.PAGE_START);
table.setModel(new DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}));
table.changeSelection(0, 0, false, false);
jScrollPane1.setViewportView(table);
getContentPane().add(jScrollPane1, BorderLayout.CENTER);
}
public void actionPerformed(final ActionEvent evt) {
table.setRowSelectionAllowed(cbRow.isSelected());
table.setColumnSelectionAllowed(cbCol.isSelected());
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
public void run() {
new TableSelectionDemo().setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
}