Trying to add a listener to a JComboBox in a JTable

Hi all.
I have a little problem which I can't resolve.
I made a program in which I create a table using a class which extends JTable (but this isn't the point with the problem, I think. It's only for rendering purposes)
In this table I put various types of items (such as Strings, JButton, JComboBox). I have no problem with the visualization of them, but the problem is with the listener for the combo boxes. I created a listener of my own and registered it in the constructor of the combo box editor, but when I change a value it's never fired. Can someone please help me?
Here's some code which can help understanding the problem
Combo box editor:
public class MyComboBoxEditor extends DefaultCellEditor{
public MyComboBoxEditor(JTableX _tabellaDiAppartenenza) {
super(new JComboBox());                  
(JComboBox)super.editorComponent).addItemListener(new MyItemListener((JComboBox)super.editorComponent));    
public Component getTableCellEditorComponent(JTable arg0, Object arg1, boolean arg2, int arg3, int arg4) {
String elementi[]=new String[((JComboBox)arg1).getItemCount()];
for(int i=0;i<((JComboBox)arg1).getItemCount();i++){
elementi=(String)((JComboBox)arg1).getItemAt(i);
super.editorComponent=new JComboBox(elementi);
return super.getTableCellEditorComponent(arg0, arg1, arg2, arg3, arg4);
}combo box listener:public class MyItemListener implements ItemListener{
private JComboBox combo;
public MyItemListener(JComboBox object){
combo=object;
public void itemStateChanged(ItemEvent arg0) {
int changeEvent = arg0.getStateChange();
if(changeEvent==ItemEvent.SELECTED){
Object value=arg0.getItem();
combo.setSelectedItem(value);
}table:public class JTableX extends JTable{
protected MyCellEditorModel myCellEditorModel;
public JTableX(MyDataModel tm){
super(tm);
Object ge = defaultEditorsByColumnClass.get(Boolean.class);
DefaultCellEditor dce = (DefaultCellEditor)ge;
dce.setClickCountToStart(0);
MyButtonEditor mbe = new MyButtonEditor(new JCheckBox("Bottone"), this);
mbe.setClickCountToStart(0);
MyComboBoxEditor mcbe = new MyComboBoxEditor(this);
mcbe.setClickCountToStart(0);
defaultEditorsByColumnClass.clear();
Hashtable cellEditors = new Hashtable();
cellEditors.put(JButton.class, mbe);
cellEditors.put(JComboBox.class, mcbe);
cellEditors.put(Boolean.class, dce);
this.defaultRenderersByColumnClass.put(JButton.class, new MyButtonRenderer());
this.defaultRenderersByColumnClass.put(JComboBox.class, new MyComboBoxRenderer());
defaultEditorsByColumnClass.put(Object.class, new MyCellEditorModel1(cellEditors));
public TableCellEditor getCellEditor(int row, int col){
if(myCellEditorModel!=null) return myCellEditorModel;
return super.getCellEditor(row,col);
public TableCellRenderer getCellRenderer(int row, int column) {
Object value = getValueAt(row,column);
if (value !=null) {
return getDefaultRenderer(value.getClass());
return super.getCellRenderer(row,column);
public void setCellEditor(MyCellEditorModel anEditor) {
myCellEditorModel = anEditor;
}Hoping the code is enough to understand the problem, I thanks all for watching and reading the post and for the future answeres.
Bye all.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

super.editorComponent=new JComboBox(elementi);You create a new combo box each call to getTableCellEditComponent() (overriding the one of the constructor), but you aren't adding a ItemListener to it.

Similar Messages

  • Trying to add a java.awt.Choice object to a JTable

    Hi
    I am creating an app. which uses a JTable as the front end, displaying string data in all cells except the first column, which I need to make java.awt.Choice objects (or equivalent) - users will click the cell, a list of job numbers will be displayed and they click the one they want.
    Every time I run the app instead of a drop down list of job numbers I get the text:
    java.awt.Choice[choice0,0,0,0x0,invalid,current=K5000]
    I am unsure how to proceed.
    Any and all help appreciated - for reference my code at present is:
    Choice jobNumber = new Choice();
    jobNumber.addItem("K5000");
    Object[][] dataValues = {
    {jobNumbers[0], "A job title", new Integer(0), new Integer(0), new Integer(8), new Integer(8), new Integer(4), new Integer(0), new Integer(0)},
    where the JTable constructor is passed this array
    regards
    David

    Don't mix Swing and AWT!! Have a look at the JTable tutorial on this site.
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#combobox

  • How to add action listener to internal frame?

    I have frame Main where I am trying to add action listener.
    Action listener should be listening button in frame Menu.
    I think it should be:
    Menu frame = new Menu();
    frame.btn_start.addActionListener(this);but it seems to be doing absolutely nothing.
    If I try to add in menu:
    btn_start.addActionListener(new Main());it generates: java.lang.StackOverflowError
    What should I do? Please help noob :)

    To get better help sooner, post a [_Short, Self Contained, Compilable and Executable, Example Program (SSCCE)_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.
    You're quite possible adding the ActionListener to the wrong component. And constructing a new instance of a class within the constructor is likely the cause of your StackOverflowError.
    db

  • Trying to Add a focus listener to Jtabbedpane

    Trying to add a focus listener to Jtabbedpane. This code compiles fine but I can't get the getName() method of the inner class to print when a Jtabbedpane is selected. Perhaps the focus listener isn't setup properly?...don't know...please help.
    class SteelToolFrame extends JFrame {
    for(i =0;names.size() > i;i++){
    String hello = (String)names.get(i);
    screen = new SteelScreenBean(hello);
    String hi = (String)names.get(i);//names is an
    //arraylist
    JScrollPane v = screen.pane;
    JTabbedPane r = (JTabbedPane)tabs.get(1); //tabs is
    //an arraylist
    r.addFocusListener(new FocListener());
    (r).addTab(hi,v);
    class FocListener extends FocusAdapter{
    public void focusGained(FocusEvent e){
    JTabbedPane tb = (JTabbedPane)e.getComponent();
    System.out.println(tb.getName() + " gaincomponent");

    hi , i would use r.addChangeListener();In the Listener Method you can call getSelectedIndex(). Focus is not on a Pane itself when he on a component that is on Pane.

  • Trying to write a Listener on a CheckBox to change the Model of a ComboBox

    I am trying to change the list that appears in a ComboBox dynamically based on if a CheckBox is selected or not. I have multiple CheckBoxes and ComboBoxes being created in a loop based on a variable assigned at creation. Every tutorial i have seen writes out a listener that explicitly defines the checkbox instance but in my case i don't acctually know. My checkboxes are in an array of checkboxes as are the comboboxes as they are being created at runtime. Any help would be much apreciated this issue has been with me for two days now.

    here is a snippit of my code in how i was implementing this:
    int numButton = 17; //this is for testing only, this variable is set from another class normally
    // these variables are defined private elsewhere in the class
    JCheckBox PlayerByeCheck[] = new JCheckBox[numButton + 1];
    JCheckBox PlayerHomeCheck[] = new JCheckBox[numButton + 1];
    boolean PlayerOnBye[] = new boolean[numButton + 1];
    for (int i = 1; i <= numButton; i++) {
             PlayerByeCheck[i] = new JCheckBox();
         PlayerHomeCheck[i] = new JCheckBox();
         PlayerOnBye[i] = false;
    // more code here for building the gui it is not important for this problem
    // code to build the list starts here
    for (int i = 1; i <= numButton; i++) {
         ((TableLayout)PlayerRosterPanel.getLayout()).insertRow(1, TableLayout.PREFERRED);
             PlayerByeCheck.setOpaque(false);
         PlayerByeCheck[i].addItemListener(this);
         PlayerByeCheck[i].setName("PlayerByeCheck" + i);
         PlayerRosterPanel.add(PlayerByeCheck[i], new TableLayoutConstraints(2, 1, 2, 1, TableLayoutConstraints.CENTER, TableLayoutConstraints.FULL));
    //the comboox that i want to change
    //---- PlayerTeamSelect1 ----
         if (PlayerOnBye[i] = true) {
              PlayerTeamSelect[i].setModel(new DefaultComboBoxModel(new String[] {
                   "ARI",
                   "ATL",
                   "BAL",
              "BUF",
         "BYE"
              PlayerTeamSelect[i].setSelectedItem("BYE");
         } else {
              PlayerTeamSelect[i].setModel(new DefaultComboBoxModel(new String[] {
                   "ARI",
                   "ATL",
                   "BAL",
                   "BUF",
                   "CAR",
              "BYE"
         PlayerTeamSelect[i].setOpaque(false);
         PlayerRosterPanel.add(PlayerTeamSelect[i], new TableLayoutConstraints(4, 1, 4, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
    //more gui code here method gets closed
    //Item Action Listener starts here this is where i am having the problem                         
    public void itemStateChanged(ItemEvent e) {
         Object source = e.getItem();
         if (source == PlayerByeCheck[1])
         int select = e.getStateChange();
              if (select == ItemEvent.SELECTED)
              PlayerOnBye[1] = true;
    PlayerRosterPanel.repaint();
    //repaint the window i think can't get the listener to work to see if this is the right way to do this
    //i doubt it is
    I have tried several different ways to write the listener, the example in the code above is following the java swing tutorial by explicitly defining the checkbox. I have tried to write the listener as i am creating the checkbox, which doesn't seem to work or i am wrting it wrong, which is entirely possible. Once the listener sets the value i think it should repaint the screen but i am sure that that is wrong right now as well. As my hunch is the repaint will reset the checkbox to DESELCTED the default value. I will come to that issue once i figure out how to get the listener working. Ideally i want to be able to set the listener for the checkboxes regardless as to how many there are. As stated above i am generating a list of checkboxes, comboboxes and other fields based on a variable that is defined in one of my objects. If the user selects checkbox 3 than combobox 3 should be changed, and the others should not. if the user selects all the checkboxes than all the comboboxes change.
    thanks for any help. Still plugging along on it trying to figure this one out.

  • Is that possible to add a listener to a class that bind with a image?

    Hello, I am trying to add listener to a class that binds with image.  For example, I want to add listener to customDividerSkin(see example below, i bold and underline the text), so when user click the customdividerSkin image haloGreen box will be hided. Thanks,
    <?xml version="1.0" encoding="utf-8"?>
    <!-- http://blog.flexexamples.com/2008/09/18/customizing-the-divider-skin-on-a-dividedbox-conta iner-in-flex/ -->
    <mx:Application name="HDividedBox_dividerSkin_test"
    xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="vertical"
    verticalAlign="middle"
    backgroundColor="white"
    initialize="init();">
    <mx:Script>
    <![CDATA[
    [Embed("arrowLeft.png")]
    private const customDividerSkin:Class;
    private function init():void
    dividedBox.setStyle("dividerSkin", customDividerSkin);
    ]]>
    </mx:Script>
    <mx:HDividedBox id="dividedBox"
    horizontalGap="24"
    width="100%"
    height="100%">
    <mx:ViewStack id="AddFormViewStack"
      width="100%"
      height="100%">
    <mx:Box id="box1"
    backgroundColor="haloGreen"
    width="100%"
    height="100%"
    minWidth="100"/>
    <mx:Box id="box2"
    backgroundColor="haloBlue"
    width="100%"
    height="100%"
    minWidth="100"/>
    </mx:ViewStack>
    </mx:HDividedBox>
    </mx:Application>

    Set the class as the source for an mx:Image and set the listener to the  Image

  • I just updated my iPad2 to ios7 and everything returned except my movies. Why??? I was trying to ADD a movie for my 5 year old and it said I needed a later iOS. Now he has ZERO movies!!!! Just great!!

    I just updated my iPad2 to ios7 and everything returned except my movies. Why??? I was trying to ADD a movie for my 5 year old and it said I needed a later iOS. Now he has ZERO movies!!!! Just great!! Can anyone help??

    For what it's worth, you posted this in 2011, and here in 2014 I am still having this same issue. Over the last two days, I have had to unlock my apple account 8 times. I didn't get any new devices. I haven't initiated a password reset. I didn't forget my password. I set up two factor authentication and have been able to do the unlocking with the key and using a code sent to one of my devices. 
    That all works.
    It's this having to unlock my account every time I go to use any of my devices. And I have many: iMac, iPad, iPad2, iPad mini, iPhone 5s, iPod touch (daughter), and my old iPhone 4 being used as an ipod touch now.  They are all synced, and all was working just fine.
    I have initiated an incident with Apple (again) but I know they are just going to suggest I change my Apple ID. It's a simple one, and one that I am sure others think is theirs. I don't want to change it. I shouldn't have to. Apple should be able to tell me who is trying to use it, or at least from where.
    Thanks for listening,
    Melissa

  • Trying to add KPLU jazz radio station to itunes or desk top

    Hi good people:
    I have been trying to add the jazz radio station, KPLU, to my Library in iTunes and have got as far as listing it in the library but I can't onpen it up to play the music. I have imported plug ins and tried all most everything. Some time ago I had it on my desk top and all I had to do was click it, and on she came.
    Thanks for your help: Fred

    Fred,
    Which link on this page http://www.kplu.org/whatson/indexlisten.cgi are you trying to listen to.
    I tried this one:
    New: Shoutcast MP3 32k Stream
    Click here to launch in your player of your own choice
    and it launches in iTunes with no problem.
    If you're trying one of the AACplus streams, you're out of luck with iTunes.
    VLC (Google for VLC if you don't know what it is) however does support AACplus. Drop the downloaded link on the VLC-window and off you go.
    The AACplus 20K by the way definitely sounds better than the 32K mp3 stream.
    AACplus 48K requires a plug-in, which is not available for Mac
    Strike this last remark. See edit.
    M
    Just tried the 48K link. Doesn't require a plug-in at all. Sounds good in VLC
    17' iMac fp 800 MHz 768 MB RAM   Mac OS X (10.3.9)   Several ext. HD (backup and data)
    Message was edited by: macmenno

  • How to make each playlist for each movie songs ? when i tried to add songs all movie songs comes in row , its very difficult to find which movie song belongs?

    how to make each playlist for each movie songs ? when i tried to add songs all movie songs comes in row , its very difficult to find which movie song belongs?

    Thank you! I put some self help hypnosis tracks to listen to on my iPod. I did not want to go to the next track automatically, I wanted to listen to them individually, one at a time. (If I fell asleep, I did not want to 'wake up' in the middle of a different track.) Your information helps me understand that I need to re-load this playlist onto my iPod from my  Mac computer, only first, I need to UNCHECK the boxes in front of each track so the iPod will STOP each time after playing the selected track. It is too bad that so many functions are not availabvle within the iPod, but that is probably why they can make it so compact.

  • I'm trying to add a playlist manually to my iPhone 4. It won't let me because it's trying to delete all of the music currently on my iPhone. Is there any way to prevent this from happening in order to start manually updating it?

    I'm trying to add a playlist manually to my iPhone 4. It won't let me because it's trying to delete all of the music currently on my iPhone. Is there any way to prevent this from happening in order to start manually updating it?

    Are you leaving the box unchecked to Manually manage music & videos? You don't need to to add anything to the iPhone.
    Simply drag the playlist to the iPhone to manually add it.

  • I am trying to add music on my Iphone 4S without deleting my current music list

    I am trying to add music on my Iphone 4S without deleting my current music list

    See this user tip: Recovering your iTunes library from your iPod or iOS device.
    tt2

  • I have garage band ver 10.0.2 and am trying to add effects to an audio track but i have no info button or track info under the track tab.  How do i get these things to show up on my program?

    I have garage band ver 10.0.2 and am trying to add effects to an audio track but i have no info button or track info under the track tab.  How do i get these things to show up on my program?

    In GarageBand 10.0.2 you can no longer add all kinds of effects freely; this GarageBand '11 feature has been discontinued. Pick one of the predefined patches that already has the effects you want.
    However, you can add effects from the predefined audio units.
    You'll see the predefined effects on the track, when you open the Smart Controls. To add audio units, click the button and enlarge the the smart controls pane by dragging the dividing line to the Track Area upwards.

  • HT1386 I originally added music to my iphone under someone elses itunes account, now that I am trying to add under my own id, it is not showing on my phone...how can I fix this?

    What do I need to do to get my music from my itunes library onto my phone? I originally added music from a diffnerent itues account, and now that I am trying to add from my own it will not transfer...however, in the bar on the bottom of itunes it shows that it is on my phone...but it does not display anywhere else and I cannot find it in my phone. Do I need to connect to the original itunes account and delete that music in order for my library to show?

    As you connect your ipod to iTunes, an option in the upper right of the screen will appear. When you click this, it will show the different menu for your ipod.

  • I am trying to add my work email to my Iphone 3 and can't get it added....any ideas??

    I am trying to add a second email/work email to my iphone and can't get it added...anyone have any ideas?

    Talk to your IT department and make sure you have the correct settings.

  • I get error message when trying to add key tags

    I get error message when trying to add key tags

    You should always backup your iPad before ever doing anything major that coould result in a catastrophic loss is something goes wrong. You can then usually restore from the backup when your iPad has recovered.
    You should also be sure to have transfered any new purchases directly to your iPad to the iTunes app on your computer on a regular basis.

Maybe you are looking for

  • IBook won't boot after broken wireless connection trying to update OS

    Trying to help my kid with his iBook via telephone calls. He was updating the OS over a wireless connection, lost the signal which apparently froze the machine. When he rebooted it wouldn't go past the blue start-up screen with a spinning gear. He's

  • Can I make a non-italic font italic with indesign.

    I have a client who insists I can take a font they want me to use that does not come in italic and make it italic? I think I remember you used to be able to do this in quark but is there some way to do it in indesign?

  • ACR vs. LR 4

    I am wondering whether ACR and LR4 are the same or different.. I have been using ACR (6.7)  in photoshop CS5 for post processing camera images in Windows 7 environment. After processing ACR, the processed image returns to Photoshop in jpeg.   I alway

  • Pages Template issues on my Macbook

    I was wondering if anyone can help me with an issue I am having with Pages '09 When I go to load up any of the multipage templates only the first page will open. It is very frustrating especially when the layout I want to cribb off of is one of the l

  • Lion 10.7.3 and InDesign CS 5

    Since upgrading I am unable to open InDesign documents via the File:Open dialog. Opening from the Finder is fine but not from the Open dialog where the ID docs appear 'greyed out'. Furthermore if I want to import a Swatch from a previous donor docume