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 8701635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:26:55+00:00 2026-06-13T02:26:55+00:00

I have JComboBox with 2 columns and I have JButton . When I click

  • 0

I have JComboBox with 2 columns and I have JButton. When I click the JButton, I need to get the result of the JComboBox selected value from first column and seconds column separately…

How do I this?

Also: how do I set the header of that JComboBox ?

The Code:

 public class Combo extends JFrame implements ActionListener{
    private JComboBox combo = new JComboBox();
    private JButton button = new JButton();
    public Combo() {

        setLayout(new FlowLayout());
        combo.setRenderer(new render());

        add(combo);

        combo.addItem(new String[] {"1","bbb"});
        combo.addItem(new String[] {"2","ff"});
        combo.addItem(new String[] {"3","gg"});
        combo.addItem(new String[] {"4","ee"});

        add(button);
        button.addActionListener(this);
        pack();
    }


    public static void main(String[]args){
        new Combo().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button){
            System.out.println(combo.getSelectedItem());
        }
    }
}
class render extends JPanel implements ListCellRenderer{

    private JLabel label1 = new JLabel();
    private JLabel label2 = new JLabel();
    private JLabel label3 = new JLabel();
    private JLabel label4 = new JLabel();
    private JLabel label5 = new JLabel();

    public render() {
        setLayout(new GridLayout(2,5));
        add(label1);
        add(label2);   
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        String[] values = (String[]) value;
        label1.setText(values[0]);
        label2.setText(values[1]);
        if(index ==0){
            label1.setForeground(Color.red);
            label2.setForeground(Color.red);
        }else{
            label1.setForeground(Color.white);
            label2.setForeground(Color.white);
        }

        return this;
    }

}

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-13T02:26:57+00:00Added an answer on June 13, 2026 at 2:26 am

    Your items are arrays of strings, so you can print the selected item as follows:

    System.out.println(Arrays.toString((String[])combo.getSelectedItem()));
    

    EDIT:

    String[] selectedItem = (String[])combo.getSelectedItem();
    for (int i = 0; i < selectedItem.length; i++){
        System.out.println(String.format("item %s = %s", i, selectedItem[i]));
    }
    

    Or shortly if all you need is the first item – (String[])combo.getSelectedItem())[0].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable with two columns and both are JComboBox, for this purpose
i have combobox in datagrid (sourse in mysql (web)) how i can get value
I have a JTable and inside on of the columns I have a JComboBox.
i have a data gridview with 4 columns first 2 columns are combobox columns,
Hie! I have a JTable. Columns of this JTable are rendered by JComboBox. I
I have an excel file which has two columns (1. Name and 2. Value)
I have a Combobox which defaults to the first item in the list from
I do have a strange focus problem when using a JComboBox inside an embedded
I have a little problem. I had some JComboBox to a JDialog but they
I have a combobox whose value I want to use with a SQL WHERE

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.