Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:04:20+00:00 2026-06-11T03:04:20+00:00

I am very new to java. I have a class where i am creating

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:04:21+00:00Added an answer on June 11, 2026 at 3:04 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very strange behaviour of Java Date class: System.out.println(new Date().toGMTString()); long l =
Very new to Java. I have a class called GraphicsObject and a class Bug
I have a very simple Java JTable question. I am creating this class to
I am very new to Android development and Java. Have read around but I'm
I am very new to OSM. I have to build an Java App for
I am very new to Java generics and have spent an exorbitant amount of
Suppose I am building a very simple eclipse plugin for creating new java projects.
I am very new to spring and java. I have been using mostly springsource.org
I am hoping someone can help. Still very new to Android/Java. I have an
I'm very new in Java and I have a small question. I believe it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.