• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

JCombo inside a JTable

for a particular column i want to put a JCombBox inside the cells.  it depends on the data for if that column cell will have a combobox.  how do i implement the combobox in the jtable?
0
capnjazz
Asked:
capnjazz
1 Solution
 
conickCommented:
The Java Tutorial has an example of exactly that:
Take a look at:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Look at the source for TableEditDemo.java
There is a link to it on the page specified.
0
 
heyhey_Commented:
also there is a nice Example coming with Swing (and JDK 1.2) - part of SwingSet demo.
0
 
capnjazzAuthor Commented:
i see how it's implementing the same combobox w/ the same items for a particular column.  but some of my cells in that column won't have a combobox and for those that do have one the info will be different for each.

i guess the question is how do you implement for a particular cell within a column whether or not to have a combobox and for those that do have one, how do you specify the contents?

i'm guessing that once the comboboxes are in place, use the JTable.getValueAt() and cast it as a ComboBox. use the CB methods to add data then set it back w/ JTable.setValueAt().  haven't tried it yet, but it's just a thought.

and heyhey, do you have the address for the swingset demo.  i havent found it yet.  thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
conickCommented:
The SwingSet demo should be on your computer already. Its at:
<INSTALLDIR>\demo\jfc\SwingSet\src

You can specify a different editor for each cell if you want.  
Take a look at the WeirdRenderer example on the page I gave you before.
Its an example of a renderer but you can use the same technique for an editor.
Something like this:
TableCellEditor weirdEditor = new WeirdEditor();
        table = new JTable(...) {
            public TableCellEditor getCellEditor(int row, int column) {
                if ((row == 0) && (column == 0)) {   return weirdEditor;   }
                // else...
                return super.getCellRenderer(row, column);
            }
        };
The tough part is how you tell which cell gets a ComboBox and what doesnt. The example uses row == 0 and column == 0 but thats pretty trivial.
We would need more info to help you out with that.
0
 
conickCommented:
The getValueAt()/setValueAt() scheme will probably convert the ComboBox to a string and display a big long list of attributes.
0
 
capnjazzAuthor Commented:
just for further reference i'm using 1.1.7b and swing1.1
0
 
conickCommented:
As long as you have Swing I dont think you'll need any other 1.2 classes.
What is the status?
Do you have a way to decide which cell needs a JComboBox and which doesnt?
As for the contents of the combo box you would add them like any other JComboBox.
You have three choices..
1) provide a model (ComboBoxModel)
2) give it a Object array
3) give it a Vector
As to which one you should choose, we would need more info about the particular project.

0
 
capnjazzAuthor Commented:
i do have a way to tell if i need a combo box or not.  i'm reading from a table in a database and if the string is "All" i just place "All" in the cell.  if not it's a string of numbers where i get the associated location of each.  those locations are returned in a vector and need to be placed in the combobox.
0
 
conickCommented:
So in one case the value is a string in another it is a Vector?
Use the default editor when string values are returned.
table.setDefaultEditor(String.class,stringEditor);  //probably dont need cuz just need to display in the default way
table.setDefaultEditor(Vector.class, vectorEditor);
Define the vectorEditor to be a JComboBox.


0
 
capnjazzAuthor Commented:
i believe i'm doing this right so far.  but it's telling me that i haven't defined the methods in CellEditor that TableCellEditor extends

public class vectorEditor extends JComboBox
   implements TableCellEditor
{

   public vectorEditor()
   {
      super();
      setOpaque(true);
   }

   public Component getTableCellEditorComponent(
      JTable table,
      Object value,
      boolean isSelected,
      int row,
      int column)
   {
      try
      {
         for(int i=0; i < ((Vector)value).size(); i++)
            addItem(((Vector)value).elementAt(i));
      }
      catch(Exception e)
      {
         System.out.println(e.getMessage());
      }

      if(isSelected)
      {
         setBackground(table.getSelectionBackground());
         setForeground(table.getSelectionForeground());
      }
      else
      {
         setBackground(table.getBackground());
         setForeground(table.getForeground());
      }

      setFont(table.getFont());
      return this;
   }
}
0
 
conickCommented:
You have the right idea.  However since TableCellEditor is an interface that extends another interface, you would have to implement ALL of the methods from both interfaces.  The only method you want to override is the getTableCellEditor(...) method.
They provide a default implementation that you can override.
Check out the DefaultCellEditor class in the javax.swing.* package
There is a good example in the Java Tutorial that I mentioned before.
Notice they do not implement the entire interface but use the DefaultCellEditor class and override the getTableCellEditor() method.
There is a DefaultCellEditor constructor that takes a JComboBox as an argument.
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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