Solved

JTable Enhancements.

Posted on 2000-05-16
5
492 Views
Last Modified: 2013-11-23
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
Comment
Question by:santham
5 Comments
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
 

Expert Comment

by:anilbathula
Comment Utility
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
 
LVL 7

Accepted Solution

by:
vladi21 earned 75 total points
Comment Utility
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
 

Author Comment

by:santham
Comment Utility
Answer accepted
0
 

Author Comment

by:santham
Comment Utility
Thanks valdi,

very good explanation for sorting and cell renderering.    



thanks...


s.p.raj
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now