AGEM
AGEM

Reputation: 217

Accessing the components from another class

I am very new to java.

I have a class where i am creating gui and another class (main class), i am accessing the gui class. In the gui class i am creating some components and returning them.

gui class,

        public class Gui {

            public Component getTopPanelContent(){
                Jpanel jp = new Jpanel();
                JComboBox cbo1 = new JComboBox();
                JComboBox cbo2 = new JComboBox();
                JComboBox cbo3 = new JComboBox();
                JComboBox cbo4 = new JComboBox();
                JComboBox cbo5 = new JComboBox();
                JButton button = new JButton();

                jp.add(cbo1);
                jp.add(cbo2);
                jp.add(cbo3);
                jp.add(cbo4);
                jp.add(cbo5);
                jp.add(button);
                return jp; 
            }
        }

main class,

        public void addComponents(int id){
           Gui g = new Gui();
           Jpanel container = new Jpanel();

           if(id == 1){
               container.add(g.getTopPanelContent);     
           }
        }

upto this its working fine.

In the main class there is a JTextArea, Whenever i open a file, i have to display the country list in cbo1, the file contains the list of countries that has to be displayed,

       String[] countries = editArea.getContents().split("\n");

How to pass the country values to cbo1

Thanks

Upvotes: 3

Views: 3241

Answers (4)

Harmeet Singh
Harmeet Singh

Reputation: 2616

Here what you can do is :

public class Gui {
    public Component getTopPanelContent(){
        Jpanel jp = new Jpanel();
        JComboBox cbo1 = new JComboBox();
        ...
        return jp; 
    }

    public void addItemsToCBo1(String[] items){
        for(String item : items){
            cbo1.addItem(item);
        }
   }
}

Main Class:

public void addComponents(int id){
    Gui g = new Gui();
    Jpanel container = new Jpanel();

    if(id == 1){
        container.add(g.getTopPanelContent);
        g.addItemsToCBo1(editArea.getContents().split("\n"));
    }
}

Upvotes: 1

javatarz
javatarz

Reputation: 1202

You can move your components up to become class variables and add public getters. These getters will be accessible from your main class.

Gui Class ->

public class Gui {
    private JPanel jp;
    private JComboBox cbo1;
    private JComboBox cbo2;
    private JComboBox cbo3;
    private JComboBox cbo4;
    private JComboBox cbo5;
    private JButton button;

    public Component getTopPanelContent(){
        jp = new JPanel();
        cbo1 = new JComboBox();
        cbo2 = new JComboBox();
        cbo3 = new JComboBox();
        cbo4 = new JComboBox();
        cbo5 = new JComboBox();
        button = new JButton();

        jp.add(cbo1);
        jp.add(cbo2);
        jp.add(cbo3);
        jp.add(cbo4);
        jp.add(cbo5);
        jp.add(button);
        return jp; 
    }

    /**
     * @return the jp
     */
    public JPanel getJp() {
        return jp;
    }

    /**
     * @return the cbo1
     */
    public JComboBox getCbo1() {
        return cbo1;
    }

    /**
     * @return the cbo2
     */
    public JComboBox getCbo2() {
        return cbo2;
    }

    /**
     * @return the cbo3
     */
    public JComboBox getCbo3() {
        return cbo3;
    }

    /**
     * @return the cbo4
     */
    public JComboBox getCbo4() {
        return cbo4;
    }

    /**
     * @return the cbo5
     */
    public JComboBox getCbo5() {
        return cbo5;
    }

    /**
     * @return the button
     */
    public JButton getButton() {
        return button;
    }
}

Main Class ->

public void addComponents(int id){
    Gui g = new Gui();
    g.getCbo1().addItem("text");
}

Though this solves your problem, it begs the question, is there a specific reason you're creating a class just to hold GUI components? There are better ways to handle components than that (especially if you're application is going to be bigger.

Upvotes: 1

Will
Will

Reputation: 14519

Well, i usually see a GUI class having the swing components as the class attributes. You made something like a 'util' class for your GUI, so you will have to hunt your JComboBox

I suggest you to go for a full featured class to represent you GUI, like:

import javax.swing.*;

public class Gui extends JPanel {

    private JComboBox cbo1 = new JComboBox();
    private JComboBox cbo2 = new JComboBox();
    private JComboBox cbo3 = new JComboBox();
    private JComboBox cbo4 = new JComboBox();
    private JComboBox cbo5 = new JComboBox();
    private JButton button = new JButton();

    public Gui() {
        add(cbo1);
        add(cbo2);
        add(cbo3);
        add(cbo4);
        add(cbo5);
        add(button); 
    }
}

Also, add getters and setters ;-).

If you wanna keep your code your way, you can search the combobox by index:

System.out.println( jp.getComponent(0) );

Or, better, you can name your JComboBox and then search for it by name:

cbo1.setName("countryCombo");
jp.add(cbo1);

and then:

public Component findCbo1() {
    for (Component comp : this.getComponents()) {
        if(comp.getName() != null && comp.getName().equals("countryCombo")) {
            return comp;
        }
    }
    return null;
}

Upvotes: 3

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Two ways to do it....

  • Make the cbo1 as, static...so you will have to declare it outside the method in Class scope...

    So it will be like this..

    for(String s: countries){ 
    
     Gui.cbo1.addItem(s);
    
    
     }
    
  • Or use Singleton principle to have the Gui class as Singleton, and then use Composition to get access to the JComboBox cbo1

Upvotes: 2

Related Questions