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 / General / July 2005

Tip: Looking for answers? Try searching our database.

Jcomboboxes in jtable select the same value..

Thread view: 
6e - 29 Jul 2005 16:06 GMT
Hi,

Ive created a JTable that has one column that is full of JCombobox
objects.

The weird thing is that when I select one value in.. say row 3.., that
value is used not only in row 3, but also row 4,5, and 6 and all the
way to the bottom-1 of the table.

I created a custom celleditor and cellrenderer, code I included below
for your perusal...

--------main app code-------------
String[] values = new String[]{"  ", "Heading", "Body"};

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).setCellEditor(new
ComboBoxEditor(values));

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).setCellRenderer(new
ComboBoxRenderer(values));

------------combobox editor---------------
public class ComboBoxEditor extends DefaultCellEditor {
   public ComboBoxEditor(String[] items) {
       super(new JComboBox(items));
   }
}

--------------Combobox renderer----------
ublic class ComboBoxRenderer extends JComboBox implements
TableCellRenderer {
   public ComboBoxRenderer(String[] items) {
       super(items);
   }

   public Component getTableCellRendererComponent(JTable table, Object
value,
           boolean isSelected, boolean hasFocus, int row, int column)
{
       if (isSelected) {
           setForeground(table.getSelectionForeground());
           super.setBackground(table.getSelectionBackground());
       } else {
           setForeground(table.getForeground());
           setBackground(table.getBackground());
       }

       // Select the current value
       setSelectedItem(value);
       return this;
   }
}
jan V - 29 Jul 2005 16:46 GMT
> The weird thing is that when I select one value in.. say row 3.., that
> value is used not only in row 3, but also row 4,5, and 6 and all the
> way to the bottom-1 of the table.

Sounds like your combo boxes are sharing underlying data models...

> I created a custom celleditor and cellrenderer, code I included below
> for your perusal...
>
> --------main app code-------------
> String[] values = new String[]{"  ", "Heading", "Body"};

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).se
tCellEditor(new
> ComboBoxEditor(values));

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).se
tCellRenderer(new
> ComboBoxRenderer(values));

You may get happier results if your renderers don't share the values
array...
6e - 29 Jul 2005 18:14 GMT
I appreciate the idea, I explored it previously by
new ComboBoxEditor( new String[]{"  ", "Heading", "Body"}; ... but it
didn't seem to work...

unless I just don't understand what you're trying to tell me...  If
this is the case, please explain further..  or let me know if you have
any other ideas?..

Thanks!


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.