Advertisement

02.29.2008 at 12:37PM PST, ID: 23205023
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Using JComboBox in a JTable
Tags: Java
I've run into some problems in trying to add or delete items and also listening to item selections for a combo box that is rendered in a JTable cell. I've been able to create the JTable and have a column as JComboBoxes,
but I can't seem to add any items to the combobox. I'm not sure how to get a reference to it. I thought maybe I could use the getValueAt(row,col) method, but that seems not work for the cells with a JComboBox .
I had initialized the ComboBox with an Item, which is displayed at the beginning, but as soon as I click on
the JComboBox it dissappears. I need to add rows dynamically, which I've added a JButton to do so.
I also need to dynamically add and delete items for all comboBoxes on all rows as well as perform a function up an item selection in the comboBox. I've added an actionListener and action performed function, but the
print out never gets printed out when I click on a comboBox.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
package comboBoxTable;
 
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
 
public class MyFrame extends JFrame{
    /** Creates a new instance of MyFrame */
    MyTable myTable;
    public MyFrame() {
        myTable = new MyTable();
        JScrollPane scrollPane = new JScrollPane(myTable);
        JPanel myPanel = new JPanel();
        myPanel.add(scrollPane);
        JButton addRowButton = new JButton("Add Row");
        addRowButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				addRow();
			}
		});
        myPanel.add(addRowButton);
        getContentPane().add(myPanel);
        setSize(new Dimension(400,400));
        pack();
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    public void addRow(){
        Object [] rowData = {"Column 1","ComboBoxItem"};    
        DefaultTableModel myModel = (DefaultTableModel)myTable.getModel();
        myModel.insertRow(myTable.getModel().getRowCount(),rowData);
    }
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }
}
 
 
 
package comboBoxTable;
 
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
 
 
 
public class MyTable extends JTable{
    DefaultTableModel model = new DefaultTableModel();
    
    public MyTable() {
        setModel(model);
        model.addColumn("Column 1");
        model.addColumn("ComboBox");
        String [] items = {"ITEM1","ITEM2"};
        getColumn("ComboBox").setCellEditor(new MyComboBoxEditor());
        getColumn("ComboBox").setCellRenderer(new MyComboBoxRenderer(items));
        setVisible(true);
    }
    
    // Don't need to implement this method unless your table's data can change.
     /*
        public void setValueAt(Object value, int row, int col) { }
      */
    
    public boolean isCellEditable(int row, int column) {
        boolean editable = true;
        if(column < 1){
            editable = false;//only edit combobox
        }
        return editable;
    }
 
}
 
 
package comboBoxTable;
 
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
 
public class MyComboBoxEditor extends DefaultCellEditor {
   // JComboBox comboBox;
    public MyComboBoxEditor(String[] items) {
        super(new JComboBox(items));
    }
    
    public MyComboBoxEditor() {
        super(new JComboBox());
    }
}
 
 
package comboBoxTable;
 
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
 
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
    public MyComboBoxRenderer(String[] items) {
        super(items);
    }
    public MyComboBoxRenderer() {
        super();
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ComboBox Action Performed");
            }
        });
    }
    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;
    }
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: mitchguy
Solution Provided By: CEHJ
Participating Experts: 2
Solution Grade: A
Views: 181
Translate:
Loading Advertisement...
02.29.2008 at 12:43PM PST, ID: 21017356

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:16PM PST, ID: 21017660

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:26PM PST, ID: 21017749

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:32PM PST, ID: 21017813

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:38PM PST, ID: 21017852

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:43PM PST, ID: 21017891

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:44PM PST, ID: 21017895

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:47PM PST, ID: 21017918

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 01:49PM PST, ID: 21017937

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 02:04PM PST, ID: 21018055

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 02:33PM PST, ID: 21018248

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 03:01PM PST, ID: 21018441

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.29.2008 at 12:43PM PST, ID: 21017356

Rank: Genius

You need to add String [] items to your CellEditor if you want them to be there when you activate it
 
02.29.2008 at 01:16PM PST, ID: 21017660

Rank: Genius

use a ComboBoxModel, then add your new rows to the model:

public class MyComboBoxEditor extends DefaultCellEditor {
   DefaultComboBoxModel comboBox;
    public MyComboBoxEditor(String[] items) {
        comboBox = new DefaultComboBoxModel(items);
        super(new JComboBox(comboBox));
    }
   
    public MyComboBoxEditor() {
        this(new String[0]);
    }

     public void addElement(Object element) {
        comboBox.addElement(element);
     }
}

Assisted Solution
 
02.29.2008 at 01:26PM PST, ID: 21017749
Yep that fixed the issue of it dissapearing when I click on it. Thank you. My real problems still exist though. I was really trying to start with an empty comboBox and then add and delete entries at will.
Secondly get the actionPerformed print out to print when I select an item from the list.
I tried adding items to the comboBox when I create  a new Row, but I'm really confused here.
When I remove the String [] items from both CellEditor and CellRenderer the first entry of rowData "Column 1" is initialized in the column 1 cell for each new row I create, but the second
entry "ComboBoxItem" does not go into the ComboBox.
  public void addRow(){
        Object [] rowData = {"Column 1","ComboBoxItem"};    
        DefaultTableModel myModel = (DefaultTableModel)myTable.getModel();
        myModel.insertRow(myTable.getModel().getRowCount(),rowData);
   
//using addItem doesn't do anything for me either
       String [] data = {"One","Two","Three"};
        addItem(data,myTable.getModel().getRowCount()-1,1);
    }
    public void addItem(String [] names,int row,int col){
        DefaultTableModel myModel = (DefaultTableModel)myTable.getModel();
        myModel.setValueAt(names,row,col);
    }
 
02.29.2008 at 01:32PM PST, ID: 21017813
>>Objects
I get the error  "cannot reference comboBox before supertype constructor has been called"
  public MyComboBoxEditor(String[] items) {
      ERROR HERE>>>>>>  super(new JComboBox(comboBox));
        comboBox = new DefaultComboBoxModel(items);
    }
 
02.29.2008 at 01:38PM PST, ID: 21017852

Rank: Genius

You don't need to create a DefaultComboBoxModel - that will be done automatically when you add the array as i mentioned

If you want to print when an item is selected in the combo, add a TableModelListener to your table
 
02.29.2008 at 01:43PM PST, ID: 21017891
>>CEHJ
The array I was using was only to initialize the JComboBox because I've been unable to
add any items after initialization. My actual plan is to start with an empty comboBox and
add items dynamically.

Where would you add the TableModelListener, where I tried to add an actionListener?
 public MyComboBoxRenderer() {
        super();
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ComboBox Action Performed");
            }
        });
    }
 
02.29.2008 at 01:44PM PST, ID: 21017895

Rank: Genius

>>ERROR HERE>>>>>>  super(new JComboBox(comboBox));
        comboBox = new DefaultComboBoxModel(items);
>>

As i said, you don't need to create that model explicitly

super(comboBox); // 'comboBox' was create with 'items'
 
02.29.2008 at 01:47PM PST, ID: 21017918

Rank: Genius

>>My actual plan is to start with an empty comboBox and add items dynamically.

Oh OK - in that case, get the model that's already there

DefaultComboBoxModel m = (DefaultComboBoxModel)comboBox.getModel();
 
02.29.2008 at 01:49PM PST, ID: 21017937

Rank: Genius

>>Where would you add the TableModelListener, where I tried to add an actionListener?

Well you can actually stick with the sort of thing you've got if you want, but it must be in the Editor, not the Renderer
 
02.29.2008 at 02:04PM PST, ID: 21018055
Sounds like all my problems lie in the Editor.
I'm still confused as to where to use "DefaultComboBoxModel m = (DefaultComboBoxModel)comboBox.getModel();"
 I put the listener in the renderer, because I was thinking that's where I extend the JComboBox. So if it's to go in the editor how would I do that exactly.
This gives an error for void type not allowed here
  public MyComboBoxEditor(String[] items) {
        super(new JComboBox().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ComboBox Action Performed");
            }
        }));
    }
 
02.29.2008 at 02:33PM PST, ID: 21018248

Rank: Genius

1:
2:
3:
4:
5:
6:
7:
8:
public MyComboBoxEditor(String[] items) {
        super(comboBox();
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ComboBox Action Performed");
            }
        });
}
Open in New Window
 
02.29.2008 at 03:01PM PST, ID: 21018441

Rank: Genius

If you're not filling the combo there, you should have the following ctor too:
1:
2:
3:
4:
5:
6:
7:
8:
public MyComboBoxEditor(JComboxBox comboBox) {
        super(comboBox();
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ComboBox Action Performed");
            }
        });
}
Open in New Window
Accepted Solution
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628