This discussion is archived
7 Replies Latest reply: Sep 5, 2012 1:11 AM by Jörg RSS

Rearrange JTable columns together

852227 Newbie
Currently Being Moderated
hi

I would like to have a JTable with 10 columns and 3 columns are of similar type. I want the 3 columns to be displayed together always. So whenever one of the 3 columns is rearranged (or moved) to a different position I want all the 3 to be moved together. How can this be achieved?

Thanks in advance.
  • 1. Re: Rearrange JTable columns together
    898886 Newbie
    Currently Being Moderated
    Hi

    Try this.
    http://www.java2s.com/Code/Java/Swing-Components/GroupableGroupHeaderExample.htm


    Regards
    Rupali
  • 2. Re: Rearrange JTable columns together
    Jörg Explorer
    Currently Being Moderated
    I don't see in what way this link is a solution to the OP's question. Grouping column headers have nothing to do with moving columns.

    I would suggest to attach a TableColumnModelListener to the table and then move the desired (remaining) columns with JTable.moveColumn(...).
  • 3. Re: Rearrange JTable columns together
    852227 Newbie
    Currently Being Moderated
    Thank you Jorg. I will try it.

    Rupali, I checked that link earlier itself and it just has grouping the column together. Anyway, thanks.

    Edited by: 849224 on Aug 28, 2012 9:19 PM
  • 4. Re: Rearrange JTable columns together
    852227 Newbie
    Currently Being Moderated
    Hi

    I tried TableColumnModelListener's columnMoved(). This method is called even if you click on the Table Header or sort columns. I wanted to know only if user manually drags/moves a column to a different position. Please let me know if there is any other option for the same.

    Thanks in advance.
  • 5. Re: Rearrange JTable columns together
    Jörg Explorer
    Currently Being Moderated
    Well, from the API''s description I would not assume that columnMoved(...) is fired, when a header is ckicked.
    Could you provide an SSCCE to show this?
  • 6. Re: Rearrange JTable columns together
    852227 Newbie
    Currently Being Moderated
    Please find below the sample code to check the same.


    /**
    *
    */

    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Vector;

    import javax.swing.JFrame;
    import javax.swing.JPopupMenu;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.TableColumnModelEvent;
    import javax.swing.event.TableColumnModelListener;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;


    public class SimpleJTable extends JFrame {

    private static final String COLUMN_SNO = "SNo";

    private static final String COLUMN_Name = "Name";

    private static final String COLUMN_Age = "Age";

    private static final String COLUMN_City = "City";

    private static final String COLUMN_PhoneNo = "PhoneNo";


    private int INDEX_SNO = 0;

    private int INDEX_NAME = INDEX_SNO + 1;

    private int INDEX_AGE = INDEX_NAME + 1;

    private int INDEX_CITY = INDEX_AGE + 1;

    private int INDEX_PHONENO = INDEX_CITY + 1;

    /**
    *
    */
    public SimpleJTable() {
    super();
    setSize( new Dimension( 800, 500 ) );
    addWindowListener( new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

    System.exit( 0 );
    }
    } );
    initComponents();
    init();
    }

    private void initComponents() {
    final Vector columnNames = new Vector();
    columnNames.add( COLUMN_SNO );
    columnNames.add( COLUMN_Name );
    columnNames.add( COLUMN_Age );
    columnNames.add( COLUMN_City );
    columnNames.add( COLUMN_PhoneNo );

    fTableModel = new DefaultTableModel( columnNames, 0 ) {
    public Class getColumnClass(int columnIndex) {
    return String.class;
    }

    public int getColumnCount() {
    return columnNames.size();
    }
    };
    fTable = new JTable( fTableModel );
    fTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

    // Set renderer.
    DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) fTable
    .getDefaultRenderer( String.class );
    fTable.setDefaultRenderer( String.class, defaultRenderer );
    }

    private DefaultTableModel fTableModel;

    private JTable fTable;

    private JScrollPane scrollPane;

    private JPopupMenu fPopupMenu = new JPopupMenu();

    private void init() {
    setTitle( "SimpleJTable" );
    int selectionColumn = 0;
    fTable.requestFocus();

    fTable.changeSelection( 0, selectionColumn, false, true );


    fTable.getColumnModel().addColumnModelListener(
    new TableColumnModelListener() {

    public void columnAdded(TableColumnModelEvent e) {
    }

    public void columnRemoved(TableColumnModelEvent e) {
    }

    public void columnMoved(TableColumnModelEvent e) {
    System.out.println( " Column moved" );

    }

    public void columnMarginChanged(ChangeEvent e) {
    }

    public void columnSelectionChanged(ListSelectionEvent e) {
    }

    } );



    // Adding row
    int rowCount = fTableModel.getRowCount();
    fTableModel.fireTableDataChanged();

    addEmptyRow( rowCount + 1 );

    fTable.setRowSelectionInterval( rowCount, rowCount );

    MouseListener popupListener = new PopupListener( fPopupMenu );
    fTable.addMouseListener( popupListener );
    fTable.add( fPopupMenu );
    scrollPane = new JScrollPane( fTable );
    add( scrollPane );
    setVisible( true );

    }

    private void addEmptyRow(int rowIndex) {

    fTableModel.addRow( getEmptyRow( rowIndex ) );
    }

    // POP up listener class

    private Vector getEmptyRow(int rowIndex) {
    Vector vector = new Vector();
    vector.add( "1" );
    vector.add( "Rishi" );
    vector.add( "20" );
    vector.add( "Delhi" );
    vector.add( "8899456789" );
    return vector;
    }

    private class PopupListener extends MouseAdapter {

    private JPopupMenu popup;

    PopupListener(JPopupMenu popupMenu) {
    popup = popupMenu;
    }

    @Override
    public void mousePressed(MouseEvent e) {
    maybeShowPopup( e );
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    if ( fTable.getSelectedRow() != -1 ) {
    maybeShowPopup( e );
    }
    }

    private void maybeShowPopup(MouseEvent e) {
    if ( e.isPopupTrigger() ) {
    popup.show( e.getComponent(), e.getX(), e.getY() );
    }
    }
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    SwingUtilities.invokeLater( new Runnable() {

    public void run() {
    new SimpleJTable();
    }
    } );
    }
    }
  • 7. Re: Rearrange JTable columns together
    Jörg Explorer
    Currently Being Moderated
    Please read the second (fixed) post of the forum which tells you how to format code (and reduce eye strain).
    And "short" in SSCCE means to strip the code from any line not necessary to demonstrate the problem.
    Have a second try.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points