hide some JTable column names
Hello.
It's possible to hide some column names in a JTable.
Let's say i ahev this:
String[] columnNames ={
"Name",
"Surname",
"Adress",
"Email"
};
Object values[][]=new Object [100][4];
JTable table =new Jtable(values, columnNames);
but in the GUI i just wanna see the columns Name, Surname and Adress.
Is this possible and how can i achive this if it is possible?
Thank you.
noe Rocha.
[699 byte] By [
noe.rochaa] at [2007-11-26 16:58:21]

# 1
String[] columnNames = {"","Surname","Adress",""};
# 2
Extend DefaultTableModel and override getColumnName(int column).
# 3
Yes, it is possible, but I can't remember how to do it off the top of my head.Why are you adding email to the table if you don't want to see it?EDIT:See the reply by PhHein above me for how.
# 4
doing this i can save all the values i want for future workarounds, instead of keep the values in some cache, etc..
if for example i have 10 atributes for a user and i only need to show 5 of them, having some columns hided prevents me from having to ask the database the rest of the atributes.
I don't know if i have explained this ok.
# 5
> doing this i can save all the values i want for
> future workarounds, instead of keep the values in
> some cache, etc..
>
> if for example i have 10 atributes for a user and i
> only need to show 5 of them, having some columns
> hided prevents me from having to ask the database the
> rest of the atributes.
> I don't know if i have explained this ok.
I understand now. Just some comments:
1. You have it stored somewhere already right? Please tell me the table model doesn't access the database directly.
2. Do you use it somewhere else at this time? If you aren't using this data now then don't get it from the database. If you need to show it in the future, then change the code later.
3. If you need to use it later somewhere else, then you need to store it in some type of "User" object, rather than in an array in a table.
# 6
Hi.
You mean, like this?:
public class MyDefaultTableModel extends DefaultTableModel
{
....
public String getClomunName(int value)
{
...
//But what should i do here ?
}
}
Thank you.
# 7
Do you want to remove a complete column from the table?
If yes you can use this:
TableColumn col = table.getColumnModel().getColumn(0);
table.getColumnModel().removeColumn(col);
And to place it back:
table.getColumnModel().addColumn(col);
table.getColumnModel().moveColumn(table.getColumnCount()-1, 0);
# 8
Hi.
This is what i'm trying to do:
http://img147.imageshack.us/img147/1643/guikf1.jpg
I have a table with 3 columns, but i need to populate the fields below too everytime i click on a row.
If i have some table columns hided, i have all the values i need, and settings the values below is very easy and fast.
This info is acessible by a webservice, everytime i click on a tab on the JTabbedPane, the table gets filled.
I guess i would like to save the info in future too, lets say someone clicks the tabs back and forward, i could have some cache system that was able to fill the table without needing to call a webservice everytime.
# 9
I don't to remove the columns, i want them to be there, but not visible, removing the columns i guess it would also remove the values contained in that columns.
# 10
> I don't to remove the columns, i want them to be
> there, but not visible, removing the columns i guess
> it would also remove the values contained in that
> columns.
What?
What is your table model doing? Just remove the column from the table if you don't display it and don't use it.
In fact, don't even put it there in the first place.
# 11
When you remove the column it doesn't change the table model.So you can still get the values from the model.
# 12
> I understand now. Just some comments:
>
> 1. You have it stored somewhere already right?
> Please tell me the table model doesn't access the
> database directly.
> . Do you use it somewhere else at this time? If you
> aren't using this data now then don't get it from the
> database. If you need to show it in the future, then
> change the code later.
> 3. If you need to use it later somewhere else, then
> you need to store it in some type of "User" object,
> rather than in an array in a table.
I'm filling like this:
try {
RListaUsersResposta resposta = getUsersList();
User2[] users = resposta.getUsers();
int rows = users.length;
String names[] = {"Username", "Administrador", "Nome"};
Object values[][] = new Object [rows][3];
for(int i=0;i<rows;i++)
{
values[i][0] = (String)users[i].getUsername();
values[i][1] = users[i].getUserAdmin();
values[i][2] = users[i].getUserNome();
}
this.UTILIZADORES.setModel(new javax.swing.table.DefaultTableModel(values, names)
{
boolean[] canEdit = new boolean [] {
false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
this.UTILIZADORES.setSelectionMode(0);
}
catch (Exception e)
{
}
Message was edited by:
noe.rocha>
# 13
Use code tags.Second, since you aren't adding the email address to the table, you don't have a problem anymore. Right?
# 14
> Use code tags.
>
> Second, since you aren't adding the email address to
> the table, you don't have a problem anymore. Right?
Did you saw the image i posted?
I would like to have a column name for every atribute, but only show 3 or 4, the rest is hided.
I will try the tip from Rodney_McKay, to see i can catch the value even if i don't see some columns.
# 15
> Did you saw the image i posted?No, I don't follow links off this forum for further descriptions.> I would like to have a column name for every> atribute, but only show 3 or 4, the rest is hided.What is the value in that? Do you ever use this data?
zadoka at 2007-7-21 16:54:36 >

# 16
Yes, i use the rest of the other data later.That's why i though it was a good idea keeping that extra data that doesn't get visible on the DefaultTableModel.
# 17
> Yes, i use the rest of the other data later.
> That's why i though it was a good idea keeping that
> extra data that doesn't get visible on the
> DefaultTableModel.
Ok a better idea, would be to store the data in some other data structure that could then be used to display in a table or somewhere else.
You should not be collecting the data in the table model.
zadoka at 2007-7-21 16:54:36 >

# 18
Ok.Gonna follow you sugestion after all.I though that i had a nice idea, guess it's not that good enough.Thank you.
# 19
> Ok.> Gonna follow you sugestion after all.> I though that i had a nice idea, guess it's not that> good enough.> Thank you.In general you want to keep from mixing your gui with your data retrieval.Research: MVC Pattern
zadoka at 2007-7-21 16:54:36 >

# 20
Instead of hidden columns, why not put a data class in the first column, with supporting data from the data class in the other columns.
Assume you have a data class called Person. The Person class has a getName(), getSurname(), getEmail(), etc. The toString() would call getName().
So, a row in your table model would hold a person, person.getSurname(), person.getAddress(), and person.getEmail(). When a row is clicked to populate the supporting data below, you already have it all in the first column's object (an instance of Person).
No hidden columns needed.
# 21
> Instead of hidden columns, why not put a data class
> in the first column, with supporting data from the
> data class in the other columns.
>
> Assume you have a data class called Person. The
> Person class has a getName(), getSurname(),
> getEmail(), etc. The toString() would call
> getName().
>
> So, a row in your table model would hold a person,
> person.getSurname(), person.getAddress(), and
> person.getEmail(). When a row is clicked to populate
> the supporting data below, you already have it all in
> the first column's object (an instance of Person).
>
> No hidden columns needed.
Thanks.
I have this working already.
You know, one day a guy wakes up and think, "hey what if i did this ...?", sometimes it happens, sometimes it's not doable.
# 22
> Ok a better idea, would be to store the data in some other data
> structure that could then be used to display in a table or somewhere else
The reason the View is separated from the Model is so you can customize the view without affecting the model. A JTable is one of the better examples of this since it allows you to easily remove columns from the view without affecting the model. And I believe in JDK6 there is even support for filtered models which again allow you to further control the view.
For example maybe you have a Product ID that is the key to the database, but the user view would only need to see the Product Description. When you update the database with a change you would need the ID, so its easier to keep the data together in the TableModel instead of creating a separate data structure.
So, I believe the correct approach as has already been suggested by Rodney is to remove the TableColumns from the TableColumnModel.
And, for what its worth, here is a posting the allows the user to customize the view at runtime that shows how flexible this approach can be:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=743041&start=7
# 23
Hum...nice code.Using this, how can i acess the values of the rows that are invivible?Thank you.
# 24
> Using this, how can i acess the values of the rows that are invivible?The columns are hidden, not the rows. The order of the columns on the TableModel never changes (even if you reorder or hide the columns in the view). You use:table.getModel().getValueAt(...);