Solved

JTable Enhancements.

Posted on 2000-05-16
5
499 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
ID: 2815341
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
ID: 2815977
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
ID: 2816393
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
ID: 2982930
Answer accepted
0
 

Author Comment

by:santham
ID: 2982931
Thanks valdi,

very good explanation for sorting and cell renderering.    



thanks...


s.p.raj
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

856 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