Java examples on Java Swing
- How do I change JFrame image icon?
- How do I add key listener event handler to JTextField?
- How do I handle mouse button click event?
- How do I use JFormattedTextField to format user input?
- How do I display an image in JButton?
- How do I add an action listener to JComboBox?
- How do I get or set the state of JCheckBox?
- How do I handle a window closing event?
- How do I format JTextField's text to uppercase?
- How do I handle JFrame window events?
- How do I add items and remove items from JComboBox?
- How do I format JLabel using HTML?
- How do I create undecorated frame?
- How do I create JComboBox?
- How do I right justified JTextField contents?
- How do I close a JFrame application?
- How do I create JLabel with an image icon?
- How do I handle mouse wheel event?
- How do I render boolean value as checkbox in JTable component?
- How do I make a centered JFrame?
- How do I programmatically select JTable's rows?
- How do I handle mouse event in swing?
- How do I get items of JComboBox?
- How do I use SwingWorker to perform background tasks?
- How do I set and get the selected item in JComboBox?
- How do I set the look and feel for swing application?
- How do I get the items of a JList components?
- How do I create a ButtonGroup for radio buttons?
- How do I get the system look and feel?
- How do I customize JButton icons?
- How do I change the cursor shape?
- How do I disable JFrame close button?
- How do I set a tool tip for swing component?
- How do I customize JCheckBox icons?
- How do I get the selected cells in JTable component?
- How do I change color of selected node in JTree?
- How do I place swing component using absolute coordinates?
- How do I create JTree with different icons for each node?
- How do I append text to JTextArea?
- How do I get the available swing look and feel?
- How do I arrange the swing component using GridLayout?
- How do I arrange the swing component using FlowLayout?
- How do I handle mouse motion event?
- How do I replace text in the JTextArea?
- How do I disable or enable JTable cell selection?
- How do I select all text in JTextArea?
- How do I create JCheckBox component?
- How do I create a JList component?
- How do I create JSpinner component with date value?
- How do I change JTabbedPane tab placement position?
- How do I add background image in JTextPane?
- How do I create Border for swing component?
- How do I insert a text in a specified position in JTextArea?
- How do I get the selected nodes of a JTree?
- How do I right-align numbers data in JTable component?
- How do I disable / enable application tool tips?
- How do I create a JSpinner component?
- How do I expand or collapse all JTree nodes?
- How do I create a message dialog box?
- How do I detect tab selection changes in JTabbedPane?
- How do I create an uneditable JTextArea?
- How do I create JSpinner component with hour value?
- How do I arrange the swing component using BoxLayout?
- How do I scroll or wrap JTabbedPane tab layout?
- How do I create a multi lines tool tips?
- How do I change JTree default icons?
- How do I add icon to JTabbedPane tabs?
- How do I listening for changes to the selected item in JComboBox?
- How do I add selection listener to JTree?
- How do I assign keyboard shortcut to JTabbedPane tabs?
- How do I disable or enable tabs in JTabbedPane?
- How do I create a JSlider with custom labels?
- How do I set Swing Timer initial delay?
- How do I create a simple JTabbedPane?
- How do I get the JFrame of a component?
- How do I create a scrollable JTable component?
- How do I create a compound border?
- How do I determine if the menu of JComboBox is displayed?
- How do I associate JLabel component with a JTextField?
- How do I set the tab size in JTextArea?
- How do I create a table model for JTable component?
- How do I disable keyboard events in JTextArea?
- How do I wrap the text lines in JTextArea?
- How do I remove tabs from JTabbedPane?
- How do I set the colors of JTabbedPane tabs?
- How do I use a JSlider component?
- How do I create JRadioButton and define some action?
- How do I read text file into JTextArea?
- How do I remove JTree default icons?
- How do I programmatically select JTable's columns?
- How do I set the font and color of JTextArea?
- How do I create a JRadioButton component?
- How do I set and get the contents of JTextArea?
- How do I use JColorChooser component?
- How do I set or change JTable column width?
- How do I use Swing Timer class?
- How do I use a JTree component?
- How do I move focus from JTextArea using TAB key?
- How do I create a JSpinner with a SpinnerListModel?
- How do I set the cell width and height of a JList component?
- How do I set the JTable's selection mode?
- How do I add a title to a border?
- How do I create a vertical JSlider?
- How do I remove a node from JTree?
- How do I create a JTextArea?
- How do I add disabled icon for JTabbedPane tabs?
- How do I create a JSlider that snap to the closest tick mark?
- How do I create JLabel component?
- How do I allow row or column selection in JTable?
- How do I create a simple JTable component?
- How do I change the number of visible items in JComboBox?
- How do I reverse JSlider's value-range?
- How do I assign tool tips for JTabbedPane tabs?
- How do I flash the window taskbar in Swing?
- How do I change JFrame state programmatically?
- How do I create a single line JTextArea?
- How do I get the selected color from JColorChooser?
- How do I set the initial color of a JColorChooser?
- How to define JRadioButton label position?
How do I programmatically select JTable's columns?
This program shows you how to select some columns in the JTable programmatically. Below we demonstrates how to select columns using the setColumnSelectionInterval() method, remove or clear column selection using removeColumnSelectionInterval() method and use the addColumnSelectionInterval() to add more columns to the previously selected columns.
package org.kodejava.example.swing;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JTableColumnSelectProgramatically extends JPanel {
public JTableColumnSelectProgramatically() {
initializePanel();
}
private void initializePanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(475, 150));
final JTable table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
JScrollPane pane = new JScrollPane(table);
JLabel label1 = new JLabel("From col: ");
final JTextField field1 = new JTextField(3);
JLabel label2 = new JLabel("To col: ");
final JTextField field2 = new JTextField(3);
JButton select = new JButton("Select");
JButton clear = new JButton("Clear");
JButton add = new JButton("Add another one");
//
// Enables column selection mode and disable row
// selection mode.
//
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
//
// Select columns based on the input.
//
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int index1 = 0;
int index2 = 0;
try {
index1 = Integer.valueOf(field1.getText());
index2 = Integer.valueOf(field2.getText());
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (index1 < 0 || index2 < 0 ||
index1 >= table.getColumnCount() ||
index2 >= table.getColumnCount()) {
JOptionPane.showMessageDialog(table, "Selection out of range!");
} else {
table.setColumnSelectionInterval(index1, index2);
}
}
});
//
// Clears column selection
//
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
table.removeColumnSelectionInterval(0, table.getColumnCount() - 1);
field1.setText("");
field2.setText("");
}
});
//
// Add one more column after the last selected column.
//
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int index2 = 0;
try {
index2 = Integer.valueOf(field2.getText());
} catch (NumberFormatException e) {
e.printStackTrace();
}
index2 = index2 + 1;
table.addColumnSelectionInterval(index2, index2);
field2.setText(String.valueOf(index2));
}
});
JPanel command = new JPanel(new FlowLayout());
command.add(label1);
command.add(field1);
command.add(label2);
command.add(field2);
command.add(select);
command.add(clear);
command.add(add);
add(pane, BorderLayout.CENTER);
add(command, BorderLayout.SOUTH);
}
public static void showFrame() {
JPanel panel = new JTableColumnSelectProgramatically();
panel.setOpaque(true);
JFrame frame = new JFrame("JTable Column Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JTableColumnSelectProgramatically.showFrame();
}
});
}
public class MyTableModel extends AbstractTableModel {
private String[] columns = {"ID", "NAME", "AGE", "A STUDENT?"};
private Object[][] data = {
{1, "Alice", 20, Boolean.FALSE},
{2, "Bob", 10, Boolean.TRUE},
{3, "Carol", 15, Boolean.TRUE},
{4, "Mallory", 25, Boolean.FALSE}
};
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return columns.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public String getColumnName(int column) {
return columns[column];
}
//
// This method is used by the JTable to define the default
// renderer or editor for each cell. For example if you have
// a boolean data it will be rendered as a check box. A
// number value is right aligned.
//
@Override
public Class<?> getColumnClass(int columnIndex) {
return data[0][columnIndex].getClass();
}
}
}
Below is the screen capture of the program.