Get your CodeRanch badge!*
The moose likes Swing / AWT / SWT and the fly likes Flashing a row in JTable ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Flashing a row in JTable ..." Watch "Flashing a row in JTable ..." New topic
Author

Flashing a row in JTable ...

Tracy Woo
Ranch Hand

Joined: Jul 23, 2002
Posts: 113
Hi,
I display some rows in a JTable. As per business logic, I keep on adding/removing rows to/from the model on certain events.
The problem that I have is, I want to flash the most recently added row (it could be any where in the table, not necessarily on the top or bottom). I am totally clueless about how to do this. If anybody has any idea please let me know.
thank you very much for your time,
Tracy.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
you can do two things:
- move the row to visibility, that is into the viewport of the scrollpane:
scrollpane.getViewport().scrollRectToVisible(table.getCellRect(theRow, theCol));
(or something like this)
- select the row. if you don't like an ordinary selection (I mean, the same colour as if the user selects a row) you can change the colour of the selection background while your highlighting this special row.
I did that for search results: I just changed the selection to multiple selection and changed the colour. if the user would click into the table (mouse event) the selection behaviour would be switched back to normal. this spares me from writing some special table cell renderer.
if you need more assistance to implement this behaviour post the code you have problems with.
cheers
Lennie B
Greenhorn

Joined: Aug 20, 2002
Posts: 3
hello Tracy,
I have an idea, but solution depends on where JTable rows came from.
my JTable represent records from database table with unique ID in first column.
insert/update database operation (SQL Server stored procedure) returns me ID of
the inserted/updated record. I can find this record in my JTable by this ID.
i'll explain in details if it can be useful
Best regards


hi Tracy<br />that's how I do it. maybe it's a way too complicating, but it works.<br />my JTable uses certain TableModel that extends AbstractTableModel.<br />in Model class I define two Vectors - first contents data for JTable, and<br />second contents IDs of matching records in database table (in the <br />same order).<p>public class someTable<br />{<br /> private myTableModel tblModel;<br /> private JTable tbl;<br /> ...<br /> <br /> public someTable()<br /> {<p> //first I'm creating model for my table and fill the rows with data <br /> tblModel = new myTableModel();<br /> tblModel.query();<p> //then I'm creating a table itself and select the first row<br /> tbl = new JTable(tblModel);<br /> <br /> tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);<br /> try {<br /> tbl.setRowSelectionInterval(0, 0);<br /> }<br /> catch(IllegalArgumentException e) {<br /> }<p> }<br /> //and doing anything else<br /> ...<p>}<p>class myTableModel extends AbstractTableModel <br />{<br /> <br />//in tableData I put rows of JTable ( each as String[] )<br />//in recordIndex I put IDs ( int )<p> private Vector tableData;<br /> private Vector recordIndex;<br /> <br /> public myTableModel() {<br /> tableData = new Vector();<br /> recordIndex = new Vector();<br /> }<br /> <br /> public void query() <br /> {<br /> Connection dbCon = new Connection(...); <br /> try {<br /> Statement st = dbCon.createStatement();<br /> String strSQL = "SELECT ID, Name, ... FROM SOMETABLE WHERE ... ORDER BY...";<br /> ResultSet rs = st.executeQuery(strSQL);<p> tableData.clear();<br /> recordIndex.clear();<p> while ( rs.next() ) {<br /> String[] record = new String[number_of_fields_I_want_to_show];<br /> recordIndex.addElement( new Integer(rs.getInt(1)) );<br /> record[0] = rs.getString(2);<br /> ...<br /> tableData.addElement( record );<br /> }<br /> st.close();<br /> }<br /> catch (SQLException e) { <br /> }<br /> }<p>//this method links ID of the record and number of the row in JTable<br /> public int getRecNum(int n) {<br /> Integer i = new Integer(n);<br /> return(recordIndex.indexOf(i));<br /> }<br />}<p><br />//as I change data I use this code<br />//class myTable deals with table in the database, and this method returns <br />// an ID of the record that I just inserted or updated<p>int newRec = myTable.InsertUpdateRecord();<p>//query() method reloads tblModel and refreshes JTable<p>tblModel.query();<br />tblModel.fire()
Tracy Woo
Ranch Hand

Joined: Jul 23, 2002
Posts: 113
Thanks a lot for the help, Chantal. I will try this out. Just want to confirm this: Since I want to flash the row, I need to maintain a separate thread that will keep selecting and unselecting the row. Right?
Lennie: Yes, I know which row I want to flash. Please do let me know what is your approach.
Lennie B
Greenhorn

Joined: Aug 20, 2002
Posts: 3
hi Tracy
that's how I do it. maybe it's a way too complicating, but it works.
my JTable uses certain TableModel that extends AbstractTableModel.
in Model class I define two Vectors - first contents data for JTable, and
second contents IDs of matching records in database table (in the
same order).
public class someTable
{
private myTableModel tblModel;
private JTable tbl;
...

public someTable()
{
//first I'm creating model for my table and fill the rows with data
tblModel = new myTableModel();
tblModel.query();
//then I'm creating a table itself and select the first row
tbl = new JTable(tblModel);

tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
try {
tbl.setRowSelectionInterval(0, 0);
}
catch(IllegalArgumentException e){
}
}
//and doing anything else
...
}
class myTableModel extends AbstractTableModel
{

//in tableData I put rows of JTable ( each as String[] )
//in recordIndex I put IDs ( int )
private Vector tableData;
private Vector recordIndex;

public myTableModel(){
tableData = new Vector();
recordIndex = new Vector();
}

public void query()
{
Connection dbCon = new Connection(...);
try {
Statement st = dbCon.createStatement();
String strSQL = "SELECT ID, Name, ... FROM SOMETABLE WHERE ... ORDER BY...";
ResultSet rs = st.executeQuery(strSQL);
tableData.clear();
recordIndex.clear();
while ( rs.next() ) {
String[] record = new String[number_of_fields_I_want_to_show];
recordIndex.addElement( new Integer(rs.getInt(1)) );
record[0] = rs.getString(2);
...
tableData.addElement( record );
}
st.close();
}
catch (SQLException e) {
}
}
//this method links ID of the record and number of the row in JTable
public int getRecNum(int n) {
Integer i = new Integer(n);
return(recordIndex.indexOf(i));
}
}

//as I change data I use this code
//class myTable deals with table in the database, and this method returns
// an ID of the record that I just inserted or updated
int newRec = myTable.InsertUpdateRecord();
//query() method reloads tblModel and refreshes JTable
tblModel.query();
tblModel.fire();
//and here I set selection
int selection = tblModel.getRecNum(newRec);
try {
tbl.setRowSelectionInterval(selection, selection);
}
catch(IllegalArgumentException e) {
}
I hope it can be useful
Tracy Woo
Ranch Hand

Joined: Jul 23, 2002
Posts: 113
Thank you, Lennie. I'm trying to do something similar.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
Hi Tracy,
sorry, I thought it would be enough to just select the row.
and yes, it might work with a seperate thread. but then you have to use SwingUtilities.invokeAndWait.
Threading is a delicate issue in Swing. Painting is done in the event dispatching thread and only there. so if you do multi threading inside swing you have to make sure to invoke any paint methods inside the event dispatching thread. SwingUtilities.invokeAndWait and SwingUtilities.invokeLater give you the ability to do this.
try something like:

BUT: what could happen is that your program will get stuck in this thread: SwingUtilities.invokeAndWait does what its name says: it invokes the run() method of its runnable and waits. so as there is a while(true) loop inside it, it might hang everything else. you could try invokeLater() though that might lead to an unregular flashing.
well, this is all hypothetical, just give it a try, maybe it works.
if it does not work:
- either think about being satisfied with a simple change of the selection background (I personally do not like flashy guis)
- or you might have to use an animated gif that you put into the cell as an icon. this should not be that difficult as the tablecellrenderer is a JLabel. to implement an animated gif as background is probably more difficult.
hope this helps
cheers
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
try to use wait(500) instead of sleep(500) and see what this changes. but then it is not guaranteed that the thread will be rewaken after 500 msecs, I think.
Tracy Woo
Ranch Hand

Joined: Jul 23, 2002
Posts: 113
Thanks a lot for your help. I implemented your suggestions today and got it working. This is what I did:

It's working great.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flashing a row in JTable ...
 
Similar Threads
JTable
JTable(!!!)
jtable
Jtable
JTable