0

I would like to know how to link JComboBox to Lists, java.awt.List? When I select France, for example, to show me l2 from the list java.awt.List ! Can someone help me? Here is my code:

JPanel p1 = new JPanel();
//p1.setLayout(null); (see my photo)not function good
  p1.setLayout(null); (see my photo) are using Layout Null
JComboBox<String> cb = new JComboBox<>();
cb.setBounds(0,0,160,25);
cb.addItem("Austria");
cb.addItem("France");
p1.add(cb);
cb.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent arg0) {
                int selectedIndex = cb.getSelectedIndex(); not help me
            }
        });

List l1 = new List();
l1.add("Text1");
l1.add("Text2");

List l2 = new List();
l2.add("Text1");
l2.add("Text2");

Each country has its list by numbered it. example France = l2 I want when I select France to list my l2. I would like an example as a solution if possible.

I want to answer someone who knows not beginners in Java.

Using Layout enter image description here

Not using Layout

enter image description here

What I want is this! I select Romania and this list appears to me! enter image description here

All of the examples here are useless. That's why I need someone who knows.

Iulian
  • 1
  • 3
  • Two questions jump out 1- Why are you using `null` layouts? You're just causing yourself unnecessary pain; 2- Why would you wan to use a `java.awt.List` in Swing? Why not use a `javax.swing.JList`? Again, it's just unnecessary pain – MadProgrammer Jan 28 '18 at 09:15
  • Your examples not help me. Not duplicate. – Iulian Jan 28 '18 at 09:16
  • Oh, it is, you just can't see. You use a `ActionListener` on the `JComboBox` to be notified when it changes, you get the selected item from the `JComboBox` (you can use a instance field or the `ActionEvent`'s `getSource`, which ever is easier), from there you populate the list with the data you need. – MadProgrammer Jan 28 '18 at 09:19
  • See [How to use use combobox](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html), [How to write an action listener](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) and [How to use lists](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html) for more details – MadProgrammer Jan 28 '18 at 09:19
  • You also need to change the way you're think about the problem. Rather the "showing" the list on the selection change, you just want to change the list's model (with the data you want displayed) – MadProgrammer Jan 28 '18 at 09:23
  • I think the pictures I've been giving say everything. I am ready to create the lists only as a JComboBox user to choose which country he wants and that (JComboBox to display the list assigned to that country - to this I refer) to link JComboBox to country lists. – Iulian Jan 28 '18 at 09:46
  • Here I have 2 lists one for channels and one for url here I link them. l2.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent ie) { int selectedIndex = l2.getSelectedIndex(); String url = l3.get(selectedIndex); emp.prepareMedia(url); emp.play(); – Iulian Jan 28 '18 at 09:56
  • I want to do the same with JComboBox to display my selected list when I select France to show my list l2 when I select Romania to show my list l3. Is not that understood now? Because I tried those examples and none helped me. Do you think I will open a post if I found a solution? But I'm not an idiot! – Iulian Jan 28 '18 at 09:56
  • Then way are you using 48 AWT Lists in Swing? You could do the same thing with 1 JList and simply swap out the model and renders. The fact is, all those other questions/answer ARE the solution, any other “solution” you get will basically be the same thing – MadProgrammer Jan 28 '18 at 12:15
  • and tell me how to solve with JList then? how do I write the code? just do not forget that I have the list of ex. l3.add ("http: //"); ---- for Austria ex. l4.add ("http:"); ---- for France how can I then include the 2 list: TV Channel lists and the URL list in a single JList? I accept any changes as long as the application is working properly. – Iulian Jan 28 '18 at 12:45
  • I found the solution here: https://www.youtube.com/watch?v=Tan5cHm8OtM But I do not know how to implement this thing. Replace image to use lists. A little help can you? – Iulian Jan 28 '18 at 16:28
  • I’ve already linked the tutorials on how to use lists - SO is not a replacement for good tutorials – MadProgrammer Jan 28 '18 at 18:35
  • IMHO `ActionListener` is a better choice then `ItemListener` as it simpler to use, but the basic concepts remain, which has been already been demonstrated by the previous duplicate questions. I also go back to the my first command - Why are you using null layouts? This is going to cause you no end of issues – MadProgrammer Jan 28 '18 at 18:42

0 Answers0