JTable question

hi everyone in the forum,

another problem i can not solve.

I am using a table to show some values on the screen. Now i was trying to implement something like a search option but i do not know if the table has any method or functionality which allows it instead of doing this:

1 get each value of the table with table.getValueAt(i,0)

2 compare it with the desired value

And another problem: when i have found the request how can be focussed the row of the table ?

Many thanks in advance

[528 byte] By [patucosa] at [2007-10-3 3:46:08]
# 1
No, search is not provided by default - you will need to check cells as you describe.From the API docs again, ListSelectionModel getSelectionModel() Returns the ListSelectionModel that is used to maintain row selection state.
itchyscratchya at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the help. i was expecting that, the neccesity of implement a search functionality.But how can i focus a line when i found the value?
patucosa at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

the code;

table.getSelectionModel().setSelectionInterval(i, i);

is how you select the row. See example below for context.

you'll be using a for loop through the rows i suspect;

// assume some collection - rows - which holds all the data

// also assume a JTable table with some table model set

for (int i= 0; i < rows.size(); i++)

{

if ((getValueAt(i,j).toString().toLowerCase()).contains(s.toLowerCase()))

{

table.getSelectionModel().setSelectionInterval(i, i);

return;

}

}

johnmcparlalda at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 4
That is exactly what i didn磘 knowThanks a lot :)
patucosa at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 5

it is something i do not know if it is possible.

After testing the previous perfect suggestion to search and highlight the searched data, and due to the fact the table contains a lot of date, i do not know if it is possible to move the scroll of the table to show it automatically.

Any idea?

Thanks in advance

patucosa at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 6
Again a quick browse through the API should reveal the answers.From the table, get the location of the row.Feed that location to the viewport of the scroll pane, requesting that it move to make that point visible.I'll leave you to find the exact methods this time ;o)
itchyscratchya at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanks a lot for the help.It is always a good challenge to look for the methods, but i think i am doing something bad. I am using this methods, but i have a problem with the fact of moving the scroll.scrollInterface.setViewport( table.getSelectedRow() );
patucosa at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 8

scrollInterface.setViewport( table.getSelectedRow() );

That won't even compile - you should have found that out already, surely?

You're not doing what I suggested: you're getting the index of the row rather than it's location. And you're trying to set the viewport of the scroll pane (to an int?!) instead of asking the viewport to display a certain region.

This is what you're after.

Rectangle r = table.getCellRect(table.getSelectedRow(), 0, false);

scrollPane.getViewport().scrollRectToVisible(r);

itchyscratchya at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 9

thanks a lot for the help.

Now after being concious i have no idea how to work with the viewport, if you dont mind, could you recommend me any tutorial or something similar to learn how to use them?

i read :

http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html

and i realized of the neccesity of an area to display but no idea how to get it.

Tanks again

patucosa at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...
# 10
i realized of the neccesity of an area to display but no idea how to get itI'm not sure what you mean.The page you link to should explain it all - which bit don't you follow?
itchyscratchya at 2007-7-14 21:42:45 > top of Java-index,Desktop,Core GUI APIs...