• Post Reply Bookmark Topic Watch Topic
  • New Topic

help needed Swing  RSS feed

 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a dynamic interface in java. It resizes and adds multiple components depending on your input on certain things. I know this is like what...

The application allows a user to add business objectives. I created a JTabbedPane with the number of panels == number of objectives defined.

Each panel in the JTabbedPane uses a JComboBox[][] to define certain ratings they should use to rate objectives. The panels use GridLayout to add the JComboBoxes[][]

Under ActionListiner, I need to find which combobox was selected and it's position in the panel as per the gridLayout. So effectively, I can use event.getSource() to get the combobox but need to get it's position in the gridLayout.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you aware you can add an action listener directly to the JCOmboBox? I don't understand why you need this roundabout route to figure out which combo box triggered the action.
Also you can use the getComponentAt(int index) or similar methods to obtain components laid out in a container.

Does this help you or did I misunderstand your question?
 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried that in my loop..


combo[n-1][j].addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {System.out.println("Clicked: "+(n-1)+" "+this.j); }});

it's now complaining about local variable i.e 'n' and 'j' need to be declared final
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try declaring them as final? Did it solve your problem?

Also please use code tags and a meaningful subject line
http://faq.javaranch.com/java/UseCodeTags
http://faq.javaranch.com/java/UseAMeaningfulSubjectLine

You can edit your original post by using the button
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vuthlarhi donald wrote:I just tried that in my loop..


combo[n-1][j].addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {System.out.println("Clicked: "+(n-1)+" "+this.j); }});

it's now complaining about local variable i.e 'n' and 'j' need to be declared final



May be this is better since I dislike the idea of creating an action listnener for each JComboBox but rather have a one listener which can be the panel or the frame etc.. and inside it you can parse and get the row and col which


ex: ( code not tested)



Hope this could help you sort your problem


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!