Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JCombo inside a JTable

Posted on 1999-07-20
12
Medium Priority
?
328 Views
Last Modified: 2008-02-26
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
Comment
Question by:capnjazz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 7

Accepted Solution

by:
conick earned 400 total points
ID: 1246605
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1246606
also there is a nice Example coming with Swing (and JDK 1.2) - part of SwingSet demo.
0
 

Author Comment

by:capnjazz
ID: 1246607
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 top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 7

Expert Comment

by:conick
ID: 1246608
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
 
LVL 7

Expert Comment

by:conick
ID: 1246609
The getValueAt()/setValueAt() scheme will probably convert the ComboBox to a string and display a big long list of attributes.
0
 

Author Comment

by:capnjazz
ID: 1246610
just for further reference i'm using 1.1.7b and swing1.1
0
 
LVL 7

Expert Comment

by:conick
ID: 1246611
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
 

Author Comment

by:capnjazz
ID: 1246612
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
 
LVL 7

Expert Comment

by:conick
ID: 1246613
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
 

Author Comment

by:capnjazz
ID: 1246614
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
 
LVL 7

Expert Comment

by:conick
ID: 1246615
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6830440
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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