Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2005, 10:35 AM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can I add a JComboBox twice to the same jpanel

Hi

I want to be able to view the same JComboBox twice.

I am trying to add the same JComboBox to two different jpanels so i can select the jcombobox using either of the panels references to it.

The last reference to the JComboBox is the only one I can see when the program is run.

Can anyone help?
Below is a snippet of how I add the JComboBox

        JComboBox jCombo1 = new JComboBox();
        getContentPane().add(jCombo1, java.awt.BorderLayout.CENTER);
        getContentPane().add(jCombo1, java.awt.BorderLayout.EAST);



 
Old March 2nd, 2005, 09:17 PM
Registered User
 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Ancibit Send a message via Yahoo to Ancibit
Default

Try this:
Code:
JComboBox jCombo1 = new JComboBox();
JComboBox jCombo2 = jCombo1;
getContentPane().add(jCombo1, java.awt.BorderLayout.CENTER);
getContentPane().add(jCombo1, java.awt.BorderLayout.EAST);
That makes jCombo2 a pointer to jCombo1 but still another object. I couldn't test this so I don't know for sure if it works, but try it.

 
Old March 13th, 2005, 04:16 AM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should use the MVC -pattern instead and let the two 'Views' share the same 'Model' (the 'Controller' -part is handled by the component itself). Hope it helps.
/S

public class FrameTest extends JFrame {
    Object[] items = new Object[]{"One", "Two", "Three"};
    JComboBox jCombo1 = new JComboBox(items);
    JComboBox jCombo2 = new JComboBox(jCombo1.getModel());

    public FrameTest() {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(jCombo1, java.awt.BorderLayout.NORTH);
        getContentPane().add(jCombo2, java.awt.BorderLayout.SOUTH);

        this.setSize(200, 100);
        this.setVisible(true);
    }

    public static void main(String args[]) {
        new FrameTest();
    }
}


 
Old March 14th, 2005, 08:36 PM
Registered User
 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Ancibit Send a message via Yahoo to Ancibit
Default

So then, the only difference between your's and mine is that you have 2 JComboBox objects, and mine has 1 plus a pointer to it. If there is a difference, I'd like to know for future reference.

 
Old May 14th, 2005, 11:42 AM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is a difference and the fact of the matter is sharing the same model or String[] object is the best way.
I have not try your method yet but I could learn from that as well...
I love these forums!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a CDATA section; add nodeset to CDATA section kssudhish XSLT 3 January 3rd, 2008 07:13 AM
How to paste a bitmap into a JPanel? AndrewH Java GUI 1 May 1st, 2007 12:19 AM
Add Push button or Check Box in outlook add-ins capdevs VS.NET 2002/2003 0 January 7th, 2006 08:51 AM
JPanel components on same line biffta Java GUI 2 September 1st, 2005 06:18 PM
User can add Item in JComboBox list ?? bikash Java GUI 2 March 20th, 2004 05:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.