Move the last visible column so it becomes the first visible column in JTable in Java

Description

The following code shows how to move the last visible column so it becomes the first visible column in JTable.

Example


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
//from w w w .j av  a 2  s .  co  m
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addColumn("Col2");
    model.addRow(new Object[] { "v1", "v2" });
    model.addRow(new Object[] { "v1" });
    model.addRow(new Object[] { "v1", "v2", "v3" });
    model.insertRow(0, new Object[] { "java2s.com" });
    

    table.moveColumn(table.getColumnCount() - 1, 0);
    
    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);

  }
}

The code above generates the following result.

Move the last visible column so it becomes the first visible column in JTable in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer