• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Column in jtable

 
c ca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PLS..... Can any body help me ???
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
c ca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic