This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Column in jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Column in jtable" Watch "Column in jtable" New topic
Author

Column in jtable

c ca
Greenhorn

Joined: Feb 23, 2004
Posts: 4
Hi
I have a problem with my JTable and I need your help.
Problem:
I have a JTable with 4 columns ( Col A, Col B, Col C and Col D).
Under the JTable I have 3 checkboxes(Col 1, Col 2, Col 3). The user can add a new column to the JTable by choosing one of these checkboxes.I want that when the user choose one of check boxes, the new column will be place before Col D.
Meaning that Col D will be always the last column in table.How can I do this???
my code:
TableColumnModel model = resultTable.getColumnModel();

JLabel checkBoxLabel = new QCLabel("Cols: ");
JCheckBox col1 = new QCCheckBox("Col 1");
JCheckBox col2 = new QCCheckBox("Col2");
JCheckBox col3 = new QCCheckBox("Col3");

if (true) {
TableColumn columnone = model.getColumn(ResultTableModel.COL_ONE );
time.addActionListener(new ColumnKeeper(column1, ResultTableModel.m_columns[ResultTableModel.COL_ONE]));
col1.setSelected(true);
}
if (true) {
TableColumn column2 = model.getColumn(ResultTableModel.COL_TWO);
col2.addActionListener(new ColumnKeeper(column2, ResultTableModel.m_columns[ResultTableModel.COL_TWO]));
col2.setSelected(true);
}
if (true) {
TableColumn column = model.getColumn(ResultTableModel.COL_THREE);
col3.addActionListener(new ColumnKeeper(column3, ResultTableModel.m_columns[ResultTableModel.COL_TREE]));
col3.setSelected(true);
}

public void actionPerformed(ActionEvent e) {

QCCheckBox col1 = (QCCheckBox) e.getSource();
QCCheckBox col2 = (QCCheckBox) e.getSource();
QCCheckBox col3 = (QCCheckBox) e.getSource();
TableColumnModel model = resultTable.getColumnModel();
if (col1.isSelected()) {
model.addColumn(m_column );

} else if (col2.isSelected()) {
model.addColumn(m_column);
} else if (col3.isSelected()) {
model.addColumn(m_column);
} else {
model.removeColumn(m_column);
}
resultTable.tableChanged(new TableModelEvent(_data));
resultTable.repaint();

}

Thanks
regards CCa
c ca
Greenhorn

Joined: Feb 23, 2004
Posts: 4
PLS..... Can any body help me ???
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Have you tried jtable's moveColumn (from,to) after adding the column?
You may need to provide a method to give required header values in place of default A, B etc.


The nice thing about Standards is that there are so many to choose from!
c ca
Greenhorn

Joined: Feb 23, 2004
Posts: 4
Is there possible to have particular place for one column in jtable.I mean that this particular column will be always in the end of JTable.
???
Regards
CCA
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Either use moveColumn() as Eddie suggested or create your own implementation of TableModel, e.g. by extending DefaultTableModel.
does that help?
D.
c ca
Greenhorn

Joined: Feb 23, 2004
Posts: 4
Hi D
I have created my own implementation of TableModel.But I don't no how to do that.You see I am new in Java and every thing is new for me.Can you help me litle bit more.
Thanks
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
To make one column always appear at the end you have to code the getColumnName, getValueAt, setValueAt and isCellEditable methods accordingly.
If you've never created your own TableModel before read this
Post your code, lets have a look at what you've done.
D.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch c ca
Please take a look at ournaming policy and adjust your displayed name accordingly.
If you are new to Java relax, JTable has one the most elaborated API in the whole Swing. Thus it needs to be tackled step to step.
Please read How to use Tables in the Java Tutorial. Read it twice at least, Write short examples to thrive your knowledge. Write examples before asking here. Read the API before writting examples. Many doubts can be cleared in this way.
Please ident your code using UBB Code and be very specific in your questions.
In the excellent book Swing by Vorobiev and Robinson you can find an example of Column addition and removal
[ March 01, 2004: Message edited by: Jose Botella ]
[ March 01, 2004: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Column in jtable