• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Flashing a row in JTable ...

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tracy Woo
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Lennie. I'm trying to do something similar.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your help. I implemented your suggestions today and got it working. This is what I did:

It's working great.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic