Reorder columns in JTable
Hi.
I want to reorder the columns in a JTable. I want the user to be free to resize and reorder the columns just as he wants, save it, and reuse these settings the next time. I have no problems getting the current configuration. But I cannot handle resetting these. What I tried is
mytable.getColumnModel().getColumn(i).setModelIndex(newIndex);
mytable.getColumnModel().getColumn(i).setWidth(newWidth);The data is reordered correctly. But the header is still the old one. And the width is also the old. What do I do wrong here?
(I also thought about TableColumnModel.moveColumn(int,int), but I don't want to remember all movements, just the result...)
Thx.
Kay Dohmann
ADF Swing's table binding doesn't support dynamically adjusting the column model automatically based on the hidden control hint.
The ADF Swing design time does hide the hidden attributes to they don't show up in the generated panel/form.
I'd recommend looking at the techniques illustrated in the ADF JClient Component Demo on this page full of demo code:
http://www.oracle.com/technology/sample_code/products/jdev/index.html
The readme file for that demo is here:
http://www.oracle.com/technology/sample_code/products/jdev/1012/jclient_guide/adf_jclientcomponentdemo_readme.html
The table demo has a checkbox that illustrates hiding a column as well as another that illustrates changing a column heading dynamically.
Similar Messages
-
How to reorder columns in JTable between sessions without serialization?
I have a situation where the column widths and names of a JTable are saved to DB(there is no serialization). Upon starting a new session, the JTable needs to be displayed with reordered column.
For example, let us say the default JTable has columns as A, B, C, D ...
The user rearranges the columns to B, A, D, C....
How do I tell JTable model to not use the default but the new values? Is serialization the only way?
Here is some sample code I use to set the widths. But what I really want to do is reorder columns by reading their names.
Thanks for your help
for(int i=0;i<count;i++) {
int width=(int)(ttsDefaults.getColumnWidth(i)*
DesktopEnvironment.getInstance().getDesktopRootFrame().getWidth());
sumWidth+=width;
treeTable.getColumnModel().getColumn(i).setPreferredWidth(width);
//treeTable.getColumnModel().getColumn(i).setWidth(width);
}Hi Jules,
You need a user with XDBADMIN rights to access the WebDav. Once you have access, you can open the URL as webfolder (in IE).
Regards,
Christian -
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,
Sarwadayanandabv 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
SuyogPlease 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 AdvanceHi,
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.. -
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...
-
I wont to hide a elected column in JTable.
how it to doTableColumnModel 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. -
Show/hide a column in JTable
I want to show/hide columns in JTable
How is this possible
Note that columns that are hidden may be showed later
thanks in advance
RenjithI tried out for this:TableColumn col = jTable.getColumnModel().getColumn( colnum );
if( col != null )
col.setPreferredWidth( 0 );
col.setMaxWidth( 0 );
}But the column seems to have minimum width which you can't even change with col.setMinWidth( 0 ); -
Dear all,
I want to hide a column in JTable object, I already tried resize it to 0 but the effect is not good, I also cannot remove the column, since I need this column for other process.
So, any other good idea besides resizes and remove?
Thank you very much!I have tried that too, but the column isn't hide, the size is not actually come to 0,...That's because JTable has a default minimum width of 15.... if you want to hide the column, you must set the minwidth as well. For example:
myTable.getColumnModel().getColumn(column_to_hide).setMinWidth(0);
myTable.getColumnModel().getColumn(column_to_hide).setPreferredWidth(0);;o)
V.V. -
To setup scrollbars for individual columns in JTable
Hi,
I need to setup individual scrollbars for the
columns in JTable.
Can anybody help?
Thanks in advance.
Rgds,
sitaHi,
I need to setup individual scrollbars for the
columns in JTable.If i understand you correctly, you want to have a scrollbar for every column. If so, you will need a new instance of JTable and JScrollPane for every column, but that's a lot of work if you want to do it fancy.
The good news, is you can reuse the table model.
You could do something like that.
for (int i= 0; i < model.getColumnCount(); i++) {
JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
//container
someContainer.add(new ScrollPane(table), null/* or some layout constrains*/);
TableColumn tc = new TableColumn(i, 120);
tc.setHeaderValue(model.getColumnName(i));
table.addColumn(tc);
Can anybody help?
Thanks in advance.
Rgds,
sita -
I have see a lots of examples of whow to create fix column into JTable.
But all the examples have the same problems.
When I wont to scroll over the fix column the vertical scroll bar of the no fix column dont move, so the no fix column dose not move and the fix columns and no fix columns aren?t alined.
If sameone have same example, send it please !!!!
Thanks !!!
PabloIn a recent project I've had the same problem.
After a long research at the Swing forum at java.sun.org and google I found out that
1. Currently there is no proper Open Source solution
2. All the postings at sun are good concepts, but far away from solutions
3. The only (for me )acceptable commercial solution is the JCTable from Quest (formerly KGroup)
Since I want to have full control over the source code of the table, I decided to collect all the tips from this forums and write my own table.
Here is a first try:
http://jroller.com/resources/kriede/CoolTable.java
The main idea is to have two tables, one for the locked columns (= fixed columns = frozen columns) and one for the scrollable columns. With all the tips from this forum it was more or less a puzzle to make it work pretty.
Maybe you are looking for
-
What is new in Cinema Tools 4?
Can anyone please tell me what is different in CT4 as compared with the previous version, CT3.0.2 (this is what I have installed on my system)? I need to know it for a presentation, not for practical work with film, therefore information does not hav
-
Data Not coming Properly for 0HR_PA_OS_1 Data Source?
Hi Experts, I am trying to load data to 0PAOS_C01 using 0HR_PA_OS_1 data source. For field 0POS_OCCVAC key figure i am not getting any data from Data Source. I checked in RSA3 for 0HR_PA_OS_1 data source and in that it is not populating data for fil
-
HELP - URGENT - N8 no respone at all !!!
My N8 no respone at all after I launch default camera app, the screen hang by showing camera icon at the middle. N8 no respone at all, even press menu button, or long press the on/off button on the top. Before this happens, my N8 opening dfault faceb
-
I have an odd problem Whenever I am in the office, some files will not sync through the desktop app. However, when I leave and access any other network, they sync. Are there any specific ports or something that CC's file sync uses that I should be aw
-
Grey screen fix ??
I'm trying to fix the Grey screen issue on my Mac book pro. I have started it up using command"R". I tried reinstalling Mac OSX but there is not enough space required. There is only 49.4 mb available. I then went into disk utility and started veri