Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Customer Cell Editor for JTable

Thread view: 
Alexander Brown - 09 Mar 2004 15:35 GMT
Hello,

I am using a JTable and a custom editor to edit Color objects.

This is the cell editor class I have written:

public class CellEditor extends AbstractCellEditor implements
TableCellEditor
{
private Color colour;
private JLabel jValue = new JLabel("");
public Component getTableCellEditorComponent(JTable table,Object
value,boolean isSelected,int row,int column)
{
   //cache the original colour
   colour = (Color)value;
   Color tmp;
   //get the chosen colour
   tmp =
JColorChooser.showDialog(Engine.getInstance().getParentWindow(),"Colour for
plot",(Color)value);
   //set the current colour to the chosen colour, if valid
   if(tmp!=null)
       colour = tmp;
   jValue.setBackground(colour);
   jValue.setOpaque(true);
   return jValue;
}
public Object getCellEditorValue()
{
   return colour;
}
}

When i have edited the cell in question the change is reflected in the
table.
However if i, for example, sort the table the last change is lost.  If i
change 3 cells using the above editor, the first two will persist.  The last
change will be lost.
For sorting i use TableSorter which i got from the sun website - but as i
understand it it should not affect things like i am experiencing.
I can provide the full code for TableSorter, but i think the problem may be
in my editor...
Any help would be greatly appreciated,
Alex
Stephan Wehner - 09 Mar 2004 21:20 GMT
What is the TableCellRenderer you are using?

Stephan

_________________________
Stephan Wehner
Editor, Traffic Life: Passionate Tales and Exit Strategies
An anthology about our car culture and alternatives with
short stories, poems, cartoons and lots of other art
www.trafficlife.com

> Hello,
>
[quoted text clipped - 41 lines]
> Any help would be greatly appreciated,
> Alex
Alexander Brown - 11 Mar 2004 12:13 GMT
> What is the TableCellRenderer you are using?
>
> Stephan

Obviously this is not yet comprehensive, but this is the Cell renderer that
i use

public Component getTableCellRendererComponent(JTable table,Object
value,boolean isSelected,boolean hasFocus,int row,int column) {
   JComponent jValue = null;
   if(value instanceof Color)
   {
       jValue = new JLabel("");
       jValue.setBackground((Color)value);
       jValue.setOpaque(true);
   }
   else if (value instanceof JComponent)
   {
       jValue = (JComponent)value;
   }
   return (Component)jValue;
}
Stephan Wehner - 17 Mar 2004 02:05 GMT
My impression is you need to keep track of which colours
you want to associate with which cells (by row and column),
and set the background colour of the component returned in your  
TableCellRenderer's getTableCellRendererComponent(..) method.
Your association should know about the sort order as well.

See you

Stephan

_________________________
Stephan Wehner
Editor, Traffic Life: Passionate Tales and Exit Strategies
An anthology about our car culture and alternatives with
short stories, poems, cartoons and lots of other art
www.trafficlife.com

> > What is the TableCellRenderer you are using?
> >
[quoted text clipped - 18 lines]
>  return (Component)jValue;
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.