Java Get Selected Combobox from Another Class
<p>newbie here. First I'm sorry if this post doesn't abide the rules of stackoverflow. I want to ask the same question (I think it has wrong answer) from 3 years ago from this source:
<a href="https://stackoverflow.com/questions/14085734/how-to-call-combobox-selected-item-from-another-class-in-java">stackoverflow source</a></p>

<p>How to get the selected item of ComboBox from one class and use the value of that selected item in new class. </p>

<p>Let's say, source class and other class. I want to print item 3 (third item in a ComboBox) from source class at other class.</p>

<p>I already used the answer from above source. Yet, it only return the first item. Because I think each time I call the constructor from the source class, it will restarted the selected item to the first item.</p>

<p>How to do it when I'm using javax.swing.JFrame (I use Netbeans)?</p>

<pre><code>public class Source extends javax.swing.JFrame{

final JComboBox test = new JComboBox();
test.setModel(new DefaultComboBoxModel(new String[] {"Item 1", "Item 2", "Item 3"}));

...

public String getSelectedItem() {
   return (String) test.getSelectedItem();
}
</code></pre>

<p>The other class:</p>

<pre><code>public class Other extends javax.swing.JFrame{

public Other(){

Source sc = new Source();
String var = sc.getSelectedItem();

System.out.println(var);
}
}
</code></pre>

<p>Let's say I chose Item 3 at Source class. So will it get item 3 at Other class? Or I do wrong constructor? Sorry for the inconvenience.</p>