[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

JTable Enhancements.

Hello Every Body!,

I am doing programming in Swing - Applet.  In this

  1. I need to know how to i maintain the real time data in JTable.

  2. How to i do coding for gui enhancement like font, color -background, foreground for particular cell.
  3. how to i do sorting with multiple columns.

  4. for this which tablemodel i have to inherit.

 Can any one help.

Regards..

s.pushparaj


   


0
santham
Asked:
santham
1 Solution
 
kylarCommented:
1.What do you mean by real time data? if you update the model of a table then call revalidate() then it will update with the new data.

2. GUI Enhancement would be done by playing with TableCellRenderers. if you want to change the background, foreground etc.. you could either get the default renderer and change it, or implement your own class that has the foreground and background set to whatever colours you want.
3. this is extremely difficult. you would have to make your own class of tablesorter, which goes through and sorts by one column, and then the second, creates a new TableModel then attaches the new Model to your JTable, and finally calls revalidate() and/or repaint().
4. Well, this is probably going to be an extention of DefaultTableModel, or possibly AbstractTableModel, depending on how much functionality you want and need to customize. it doesn't really matter which one you inherit from, you could write your own as long as it implements the TableModel interface.

Hope this helps,
Kylar
0
 
anilbathulaCommented:
For background and foreground colors use TableCellRenderers
   
  1. Set background and foreground color same for all the cells then use getDefaultCellRenderer(Class columnClass) gives the TableCellRenderers Class then cast it to DefaultTableCellRenderer . For DefaultTableCellRenderers have setBackground(Color) and setForeground(Color) using these method set the colors.
 
2. For particular cell with particular color then use getCellRenderer(int row,int column) then use cast it to DefaultTableCellRenderer and use the same methods.

Some sorting examples: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
0
 
vladi21Commented:
You can look excelent examples with sources at:

http://www2.gol.com/users/tame/swing/examples/SwingExamples.html 

Also look:

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

http://manning.spindoczine.com/sbe/files/uts2/Chapter18html/Chapter18.htm 
http://users.vnet.net/wwake/swing/faq.html#table
http://www.codeguru.com/java/Swing/JTable/index.shtml
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/ 
   
---
How update JTable after I modify the data?
TableModel model=table.getModel();
if (model instanceof AbstractTableModel){
((AbstractTableModel)model).fireTableDataChanged();
}

or implemant your TableModel explicitly.

DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);

--
JTable highlight selected cells
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10219262 

----
I have JTable in a GUI and a button to save the data in the table to a database. The JTable's view and model are separate. When I do a save, the data in the last cell which I edited is lost.

Only if I change the focus to a different cell or if I press ENTER key, it fires an event to update the cell (i.e. it calls the "setValue" method in the data model). Without this, how can I fire an event to update the data in the cell.

if (isEditing()) myDefaultCellEditor.stopCellEditing();
or
if (isEditing()) getDefaultEditor(String.class).stopCellEditing();

---
How can I clear data entered into a JTable?
http://developer.java.sun.com/developer/qow/archive/23/index.html
http://developer.java.sun.com/developer/qow/archive/archive.html

http://java.sun.com/products/jdk/1.3/docs/guide/swing/JTableChanges.html
0
 
santhamAuthor Commented:
Answer accepted
0
 
santhamAuthor Commented:
Thanks valdi,

very good explanation for sorting and cell renderering.    



thanks...


s.p.raj
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now