Moving columns in JTable

hi all
i'm having a slight problem after i've moved a column within my JTable, i need to reallocate the column index numbers. i'm using a sorter for the data, so when i move a column and select another column (or that one) it sorts a different column to the one selected based on the original (pre-move) indexes ( i do hope that makes some sense).
i have a mouseDragged listener which works fine when moving a column, i just now need to add the index changes for all columns to it but i keep coming up with very very bad code that doesn't work.
any suggestions would be most appreciated.
thanks heaps...
Takis

i've actually been fiddling with those and that is actually where my problem lies.
i use the following:
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = tableView.convertColumnIndexToModel(viewColumn);
the sorter then works on "column".
problem is, that when the column is moved, the viewColumn changes, but the column value remains the same. i've also tried sorting on the view column but then it gets even more confused.
i just can't seem to figure it out...
Takis

Similar Messages

  • How to disable moving Columns in JTable

    Hi,
    How can I make sure that the columns in my JTable cannot be moved?
    Any ideas?
    thanks

    How about:myTable.getTableHeader().setReorderingAllowed(false);

  • Moving Columns in Jtable. Columns not in sync with header

    Hi,
    I got several tables within a scrollPane. So I use the add method on the scrollPane to add them. In order to see the headers I add the header separately :
    add(table.getTableHeader());
    add(table);
    The problem is the following :
    When I am moving my Column around, the header moves around, however the columns don't move ! So The header does not correspond anymore to my column !
    Do someone have some ideas how to get ride of that problem ?
    Thanks
    Emmanuel

    One thought: Add the tables to another container, such as a JPanel and then add the panel to the JScrollPane

  • How to forbid Column-Moving in a JTable ?

    Hi,
    anyone knoyw how to forbid the Column-Moving in a JTable ??
    I have a JTable that works fine, but it is still allowed to move the columns. And i want to forbid that.
    This: JTableHeader header = new JTableHeader();
    header.setResizingAllowed(false);
    header.setReorderingAllowed(false);
    did not work :-\
    Maybe someone has some ideas and could help me?
    Thx and have a nice day
    Avalon

    >
    Where's your thread, GoldieDork? You do nothing
    besides trolling.no I don't, I ask Java questions but no one answers them apart from the database question where dave and jverd and rage were all nice to me and I was happy that we could all be friends cos they helped me but then they started cussing me again so thats why I no longer talk to those guys.
    I dont know where my thread is now, Sun probably deleted it

  • How to forbid to moving columns in a JTable

    Hi together,
    i have one simple question. How can i forbid, that columns can be moved in my Jtable?
    If anyone can help or may has a useful link, i would be glad.
    thanks
    BackUP

    Hi,yourTable.getTableHeader() .setReorderingAllowed(false);

  • Problem while inserting new Column in JTable

    Hi,
    I am facing Problem while inserting new Column in JTable.
    I have a JTable with one inherited ColumnModel class.
    When I am adding the column and moving towards the corresponding location, it moves successfully.
    but if I am adding another column or making any changes to table column structure the table retains the defualtcolumn structure.
    please help me to solve this..
    Regards
    Ranjith.........

    Maybe this example will help:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=579527

  • Hiding a column in jtable made from DefaultTableModel.

    I have made my jtable from DefaultTableModel.
    I want to keep one column in the jtable as hidden storing some data containing neccessary information like the "path of the file"
    which need not be shown to the user.
    Please tell me how I can hide one column in the Jtable.
    please provide siome link or code for the same.
    Tia,
    Sarwa

    dayanandabv wrote:
    [http://search.sun.com/search/onesearch/index.jsp?qt=hide+column%2B+JTable&rfsubcat=&col=developer-forums]
    My thought exactly.
    db

  • How to set different renderers to different cells in same column of JTable?

    Hello Friends,
    I need your help again...
    Does any body knows, how to set different renderer's for different cells of same column in JTable..??
    For ex.
    Col1 Col2 Col3 Col4
    A       A       A      A
    A       A       A      B
    A       A       A      C
    A       A      A       D
    Where A B C D would be different Renderers.  I want set exactly same ,,, ie. one column with different renderer at different cell positions..
    Right now i m setting renderer using statement bellow :
    table.getColumnModel().getColumn(int).setCellRenderer(rederer_Instance);But with this, effect in the last renderer is applicable whole column....
    Can any body help me out ?????????
    please refer this thread for similar kind of discussion...
    http://forums.sun.com/thread.jspa?forumID=57&threadID=571445Thanks
    Suyog

    Please refer to the first reply of [this thread|http://forums.sun.com/thread.jspa?forumID=57&threadID=571445] for the answer. If you have a specific problem implementing it, post you code with a specific question.

  • Add and remove columns from JTable

    Help me please!
    A try to remove column from JTable. It's removed, but when I try to add column in table, then I get all old (removed early) columns + new column....
    I completely confused with it.....
    Here is my code for remove column:
    class DelC implements ActionListener
              public void actionPerformed (ActionEvent e )
                   int [] HowManyColDelete = table.getSelectedColumns();
                   if (HowManyColDelete.length !=0)
                        TableColumnModel tableCModel = table.getColumnModel();
                        for (int i = HowManyColDelete.length-1; i>-1; i--)
                             table.getColumnModel().removeColumn (tableCModel.getColumn (HowManyColDelete [ i ]));
                   else
                          JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Column is not selected!");
         }

    It's little ex for me, I just try understand clearly how it's work (table models i mean). Here is code. All action with tables take place through menu items.
    My brain is boiled, I've try a lot of variants of code, but did't get right result :((
    It's code represent problem, which I've describe above. If you'll try remove column and then add it again, it will be ma-a-a-any colunms...
    I understand, that my code just hide columns, not delete from table model....
    But now I have not any decision of my problem...
    Thanks a lot for any help. :)
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    import javax.swing.table.DefaultTableModel;
    class JTableF extends JFrame
         Object [] [] data = new Object [0] [2];
         JTable table;
         DefaultTableModel model;
         String [] columnNames = {"1", "2"};
         TableColumnModel cm;
         JTableF()
              super("Table features");
              setDefaultLookAndFeelDecorated( true );
              setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
              JMenuBar MBar = new JMenuBar();
              JMenu [] menus =  {new JMenu("A"), new JMenu("B")};
              JMenuItem [] menu1 =  {new JMenuItem("Add row"), new JMenuItem("Delete row", 'D'),  new JMenuItem("Add column"), new JMenuItem("Delete column")};
              menu1 [ 0 ].addActionListener(new AddL());
              menu1 [ 1 ].addActionListener(new DelL());
              menu1 [ 2 ].addActionListener(new AddC());
              menu1 [ 3 ].addActionListener(new DelC());
              for (int i=0; i<menu1.length; i++)
                   menus [ 0 ].add( menu1 [ i ]);
              for (int i=0; i<menus.length; i++)
                   MBar.add(menus );
              JPanel panel = new JPanel ();
              model = new DefaultTableModel( data, columnNames );
              table = new JTable (model);
              cm = table.getColumnModel();
              panel.add (new JScrollPane(table));
              JButton b = new JButton ("Add row button");
              b.addActionListener(new AddL());
              panel.add (b);
              setJMenuBar (MBar);
              getContentPane().add(panel);
              pack();
              setLocationRelativeTo (null);
              setVisible (true);
         class DelC implements ActionListener
              public void actionPerformed (ActionEvent e )
                   int [] HowManyColDelete = table.getSelectedColumns();
                   if (HowManyColDelete.length !=0)
                        TableColumnModel tableCModel = table.getColumnModel();
                        for (int i = HowManyColDelete.length-1; i>-1; i--)
                             int vizibleCol = table.convertColumnIndexToView(HowManyColDelete [ i ]);
                             tableCModel.removeColumn (tableCModel.getColumn (vizibleCol));
                        //cm = tableCModel;
                   else
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Column is not selected!");
         class AddC implements ActionListener
              public void actionPerformed (ActionEvent e)
                   //table.setColumnModel(cm);
                   Object NewColumnName = new String();
                   NewColumnName = JOptionPane.showInputDialog ("Input new column name", "Here");
                   int i = model.getRowCount();
                   int j = model.getColumnCount();
                   Object [] newData = new Object [ i ];
                   model.addColumn ( NewColumnName, newData);
         class AddL implements ActionListener
              public void actionPerformed (ActionEvent e)
                   int i = model.getColumnCount();
                   Object [] Row = new Object [ i ];
                   model.addRow ( Row );
         class DelL implements ActionListener
              public void actionPerformed (ActionEvent e)
                   int [] HowManyRowsDelete = table.getSelectedRows();
                   if (HowManyRowsDelete.length !=0)
                        for (int k = HowManyRowsDelete.length-1; k>-1; k--)
                             model.removeRow (HowManyRowsDelete[k]);
                   else
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Row is not selected!");
         public static void main (String [] args)
              javax.swing.SwingUtilities.invokeLater(new Runnable()
                   public void run()
                        JTableF inst = new JTableF();

  • Keyboard Access For a Column in JTable with default editor as JComboBox

    I want to get Key board access for a column in JTable.
    The user should be able to select from a drop down list for the column with default
    editor set as JComboBox.
    Presently,it works fine with mouse,also I am able to focus it with Keyboard using
    ALT+Up keys,but how to make drop down list appear.
    Plz help,it's urgent.
    Thanks in Advance

    Hi,
    In addition to setting DO_SUM = 'X' you need to specify function in H_FTYPE field. It should be set to 'AVG' in your case.
    ls_fielcat-do_sum = 'X'.
    ls_fieldcat-h_ftype = 'AVG.

  • Serial number column in Jtable

    Apologies for this distraction.
    I read at this forum about a year ago how to add a s/n column in Jtable such that all the nos are automatically updated/adjusted when a row is added/deleted. the count starts from the top down serially and unaffected by sorting.
    pls help!

    thanks a million BaltimoreJohn and especially camickr whose solutions to problems have been most helpful. applying the referenced post, i worked around the problem by resetting the values of the column having the serial number, based on table rowCount every time there is an operation on it(deleting, addition etc)

  • Problem with Hiding Columns in JTable

    Hi.
    Who knows how to hide column in JTable.
    I try to get column:
    MyTab.getColumnModel().getColumn(i)But there is no methods like SetVisible(false)
    What to DO???

    http://www.google.com/search?hl=en&q=%2Bhide+%2Bcolumn+%2Bjtable&btnG=Google+Search

  • Programmatically Sorting columns in JTable

    Hi,
    How can I programmatically sort a particular column in JTable without having to click on column header.
    Thanks.

    to sort the desired column w/o click on header
    int columnIndexToSort = 1;
    RowSorter.SortKey sortKey = new RowSorter.SortKey(columnIndexToSort,SortOrder.ASCENDING);
    ArrayList alist = new ArrayList(1);
    alist.add(sortKey);
    ((DefaultRowSorter)table.getRowSorter()).setSortable(columnIndexToSort , true);
    ((DefaultRowSorter)table.getRowSorter()).setSortKeys(alist);
    ((DefaultRowSorter)table.getRowSorter()).sort();expecting dukestars..

  • Freezing columns in JTable....  -  Need Suggestions!!!!!!!!!

    Hai Friends,
    i need suggestions for the following,
    How to freeze the columns in swing JTable.?????????????
    I need to freeze 3 columns in JTable... how it can be done?
    eg: its like the option used in excel for freezing the panes. like that, i need to freeze the column in JTable. so that, it wont be scrolled while scrolling the records, it should also be done in colmns of jtable for horizontal scroll n row for vertical scroll. is this can be done in JTable. Can u pls help me out in this...?
    Regards,
    Ciya.

    Did you actually try the code which was pointed out to you when you [url http://forum.java.sun.com/thread.jspa?threadID=701198]asked this question before? It does just what you want...

  • Hidden Columns in JTable

    I wont to hide a elected column in JTable.
    how it to do

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn( modelIndex );
    tcm.removeColumn( column );
    Next time, search the forum first. There are at least a dozen posts asking this question.

Maybe you are looking for

  • Setting adapter specific message attributes

    hi xi guroos. i just got one doubt in a file to file scenario. if i set adapter specifec message attributes in sender file CC. and if  i enabled file name checkbox. some xi prof saying that the file at recievr directory will get created with the same

  • Rss form with many item fileds how to insert at once all?

    RSS Form have single channel with many items.How to get them all form form so that can be added to a file. For now what i have done is a form having (RSS) input types as channel(title,link,desc) with many items together(title,link,desc) here i submit

  • IPhone 6 Plus Keeps Crashing  PLEASE HELP!

    I am a huge Apple fan, so I ask this question wishing and hoping my iPhone 6 Plus would just perform the way I know it should be. Since restoring from my old iPhone 5S backup onto my 6 Plus 128GB (AT&T) running iOS 8.0, my 6 Plus is laggy (animations

  • Finding a printer using IPAddress

    Hi, Can anyone suggest me how to find a printer in the network using its IPAddress and then print a document to it (probably a PDF). I cannot use the printer name, but I know the IPAddress and that its present in the network. Thank you,

  • Running GB 1.1 on Intel-based cpu

    I'd like to know if there will be any problem running GB 1.1 on an Intel-based Mac. I need to do this for my school music classroom, as all the computers there use that version on eMacs. All help will be appreciated.