Moving a Column in JTable

This section describes, how to move a column in JTable component.

Moving a Column in JTable


This section describes, how to move a column in JTable component. Moving is a very simple method  that  moves the data from  one position  to the other. If you want to move a column from one position to another specified position in JTable via using the moveColumn() method,  you need the index of column that will count from '0' which denotes the first column of  JTable. The  moveColumn method  takes the index of columns.

Description of program:

This program helps you in moving a column into  JTable. This program creates a table through the JTable having  '4' rows and '3' columns with the column header and yellow background color in it. This program uses the move method  to move a column at specified position with the help of moveColumn() method. In the first  table, the previous column is Name, secondary  is Course and the third one is Subject. After moving, Course reaches on the first position, Name goes to the second position and Subject does not left it's position  So you can see how the first column  moves from first position to second position.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class MocveColumn{
  JTable table;
  public static void main(String[] args) {
  new MocveColumn();

  public MocveColumn(){
  JFrame frame = new JFrame("Moveing a column in a JTble");
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","MCA","Computer"},
  String col[] {"Name","Course","Subject"};  
  DefaultTableModel model = new DefaultTableModel(data, col);
  table = new JTable(model);
  //first column move into second position
  move(table, 10);
  JTableHeader header = table.getTableHeader();
  JScrollPane pane = new JScrollPane(table);

  public void move(JTable table, int mIndex, int sIndex){

Download this example.

Output of program:

Initial Table:

After moving a column:


Share on Google+Share on Google+

Moving a Column in JTable

Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Moving a Column in JTable   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 18, 2012

import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.sql.*; public class Frame3 extends JFrame implements KeyListener { JTable table; JLabel l1 ; String rec[][]=null; JTextField t1 ; public Frame3() {Container c= getContentPane(); c.setLayout(null); setSize(1000,2*500); l1= new JLabel ("Methods"); l1.setBounds(50,50,100,25); t1=new JTextField(10); t1.setBounds(100,50,100,25); //table.setVisible(false); add(l1);add(t1); t1.addKeyListener(this); setVisible(true); } public static void main(String qq[]) { new Frame3(); } public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) {System.out.println(t1.getText()); if(t1.getText()=="") return; int j=Integer.parseInt(t1.getText()); int i=0; rec= new String[j][6]; for(i=0;i<j;i++) {rec[i][0]=""; rec[i][1]="hi "; rec[i][2]=""; rec[i][3]=""; rec[i][4]=""; rec[i][5]=""; } String head[] = {"Method Name","Alias","Input Param","Output Param","Input Mapping","Output Mapping"}; table=new JTable(rec,head); table.setVisible(false); //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.getColumnModel().getColumn(0).setPreferredWidth(27); table.getColumnModel().getColumn(1).setPreferredWidth(120); table.getColumnModel().getColumn(2).setPreferredWidth(100); JScrollPane p=new JScrollPane(table);p.setBounds(250,250,500,500); add(p); table.setVisible(true); } public void keyTyped(KeyEvent e) { } }