Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Ad icon
Video Content Creator
pramukag
Updated:
Today at 6:10 AM
Ad icon
QA Engineer Intern
pramukag
Updated:
Today at 6:07 AM
Ad icon
Sell your Land, House on idamata.lk for FREE
sajith.xp.pk
Updated:
Thursday at 9:03 AM
Handmade Character Soft Toys
anil1961
Updated:
Tuesday at 2:11 PM
Bodim.lk out now !
Manoj Suranga Bandara
Updated:
Jun 21, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
ElaKiri Programmer's Club
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="KingCM" data-source="post: 17882024" data-attributes="member: 482722"><p>[CODE]import java.awt.*;</p><p>import java.awt.event.*;</p><p>import javax.swing.*;</p><p> </p><p>public class Compare extends JFrame implements ItemListener {</p><p> JComboBox combo, lcombo;</p><p> </p><p> public Compare() {</p><p> setLayout(null);</p><p> String a[] = { "Select", "Watches", "Mobiles", "shoes" };</p><p> combo = new JComboBox(a);</p><p> combo.setBounds(50, 50, 100, 20);</p><p> combo.addItemListener(this);</p><p> add(combo);</p><p> </p><p> lcombo = new JComboBox();</p><p> lcombo.addItemListener(this);</p><p> lcombo.setEnabled(false);</p><p> add(lcombo);</p><p> lcombo.setBounds(50, 100, 100, 20);</p><p> </p><p> setSize(300, 300);</p><p> }</p><p> </p><p> public void itemStateChanged(ItemEvent e) {</p><p> String b[] = { "Titan", "HMT" };</p><p> String c[] = { "Nokia", "Sony", "Motorola", "Samsung" };</p><p> String d[] = { "Liberty", "Action", "Bata", "Campus", "Relaxo" };</p><p> </p><p> if (e.getSource() == combo) {</p><p> if (combo.getSelectedItem().equals("Select")) {</p><p> lcombo.setEnabled(false);</p><p> } else if (combo.getSelectedItem().equals("Watches")) {</p><p> lcombo.setEnabled(true);</p><p> lcombo.removeAllItems();</p><p> for (int i = 0; i < b.length; i++) {</p><p> lcombo.addItem(b[i]);</p><p> }</p><p> } else if (combo.getSelectedItem().equals("Mobiles")) {</p><p> lcombo.setEnabled(true);</p><p> lcombo.removeAllItems();</p><p> for (int i = 0; i < c.length; i++) {</p><p> lcombo.removeItem(c[i]);</p><p> lcombo.addItem(c[i]);</p><p> }</p><p> } else if (combo.getSelectedItem().equals("shoes")) {</p><p> lcombo.setEnabled(true);</p><p> lcombo.removeAllItems();</p><p> for (int i = 0; i < d.length; i++) {</p><p> lcombo.addItem(d[i]);</p><p> }</p><p> }</p><p> }</p><p> }</p><p> </p><p> public static void main(String args[]) {</p><p> (new Compare()).setVisible(true);</p><p> }</p><p>}[/CODE]</p><p>or</p><p></p><p>[CODE]import java.awt.*;</p><p>import java.awt.event.*;</p><p>import java.util.*;</p><p>import javax.swing.*;</p><p></p><p>public class ComboBoxTwo extends JFrame implements ActionListener</p><p>{</p><p> private JComboBox mainComboBox;</p><p> private JComboBox subComboBox;</p><p> private Hashtable subItems = new Hashtable();</p><p></p><p> public ComboBoxTwo()</p><p> {</p><p> String[] items = { "Select Item", "Color", "Shape", "Fruit" };</p><p> mainComboBox = new JComboBox( items );</p><p> mainComboBox.addActionListener( this );</p><p></p><p> // prevent action events from being fired when the up/down arrow keys are used</p><p>// mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);</p><p> getContentPane().add( mainComboBox, BorderLayout.WEST );</p><p></p><p> // Create sub combo box with multiple models</p><p></p><p> subComboBox = new JComboBox();</p><p> subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4</p><p> getContentPane().add( subComboBox, BorderLayout.EAST );</p><p></p><p> String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };</p><p> subItems.put(items[1], subItems1);</p><p></p><p> String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };</p><p> subItems.put(items[2], subItems2);</p><p></p><p> String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };</p><p> subItems.put(items[3], subItems3);</p><p> mainComboBox.setSelectedIndex(1);</p><p> }</p><p></p><p> public void actionPerformed(ActionEvent e)</p><p> {</p><p> String item = (String)mainComboBox.getSelectedItem();</p><p> Object o = subItems.get( item );</p><p></p><p> if (o == null)</p><p> {</p><p> subComboBox.setModel( new DefaultComboBoxModel() );</p><p> }</p><p> else</p><p> {</p><p> subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );</p><p> }</p><p> }</p><p></p><p> public static void main(String[] args)</p><p> {</p><p> JFrame frame = new ComboBoxTwo();</p><p> frame.setDefaultCloseOperation( EXIT_ON_CLOSE );</p><p> frame.pack();</p><p> frame.setLocationRelativeTo( null );</p><p> frame.setVisible( true );</p><p> }</p><p>}[/CODE]</p></blockquote><p></p>
[QUOTE="KingCM, post: 17882024, member: 482722"] [CODE]import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Compare extends JFrame implements ItemListener { JComboBox combo, lcombo; public Compare() { setLayout(null); String a[] = { "Select", "Watches", "Mobiles", "shoes" }; combo = new JComboBox(a); combo.setBounds(50, 50, 100, 20); combo.addItemListener(this); add(combo); lcombo = new JComboBox(); lcombo.addItemListener(this); lcombo.setEnabled(false); add(lcombo); lcombo.setBounds(50, 100, 100, 20); setSize(300, 300); } public void itemStateChanged(ItemEvent e) { String b[] = { "Titan", "HMT" }; String c[] = { "Nokia", "Sony", "Motorola", "Samsung" }; String d[] = { "Liberty", "Action", "Bata", "Campus", "Relaxo" }; if (e.getSource() == combo) { if (combo.getSelectedItem().equals("Select")) { lcombo.setEnabled(false); } else if (combo.getSelectedItem().equals("Watches")) { lcombo.setEnabled(true); lcombo.removeAllItems(); for (int i = 0; i < b.length; i++) { lcombo.addItem(b[i]); } } else if (combo.getSelectedItem().equals("Mobiles")) { lcombo.setEnabled(true); lcombo.removeAllItems(); for (int i = 0; i < c.length; i++) { lcombo.removeItem(c[i]); lcombo.addItem(c[i]); } } else if (combo.getSelectedItem().equals("shoes")) { lcombo.setEnabled(true); lcombo.removeAllItems(); for (int i = 0; i < d.length; i++) { lcombo.addItem(d[i]); } } } } public static void main(String args[]) { (new Compare()).setVisible(true); } }[/CODE] or [CODE]import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class ComboBoxTwo extends JFrame implements ActionListener { private JComboBox mainComboBox; private JComboBox subComboBox; private Hashtable subItems = new Hashtable(); public ComboBoxTwo() { String[] items = { "Select Item", "Color", "Shape", "Fruit" }; mainComboBox = new JComboBox( items ); mainComboBox.addActionListener( this ); // prevent action events from being fired when the up/down arrow keys are used // mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); getContentPane().add( mainComboBox, BorderLayout.WEST ); // Create sub combo box with multiple models subComboBox = new JComboBox(); subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 getContentPane().add( subComboBox, BorderLayout.EAST ); String[] subItems1 = { "Select Color", "Red", "Blue", "Green" }; subItems.put(items[1], subItems1); String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" }; subItems.put(items[2], subItems2); String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" }; subItems.put(items[3], subItems3); mainComboBox.setSelectedIndex(1); } public void actionPerformed(ActionEvent e) { String item = (String)mainComboBox.getSelectedItem(); Object o = subItems.get( item ); if (o == null) { subComboBox.setModel( new DefaultComboBoxModel() ); } else { subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) ); } } public static void main(String[] args) { JFrame frame = new ComboBoxTwo(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } }[/CODE] [/QUOTE]
Insert quotes…
Verification
Payakata winadi keeyak tibeda?
Post reply
Top
Bottom