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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:22:34+00:00 2026-06-16T12:22:34+00:00

I have created a JComboBox and populated its content from database using DefaultComboBoxModel .

  • 0

I have created a JComboBox and populated its content from database using DefaultComboBoxModel.

Here is the code:

DefaultComboBoxModel model = new DefaultComboBoxModel();
PreparedStatement statement = con.prepareStatement("SELECT _fid, fruit_name FROM fruits;");

ResultSet result = statement.executeQuery();
while (result.next()) {
    model.addElement(result.getString(2));
}
comboBox = new JComboBox(model);

How can I also set the index of the JComboBox with the value of _fid?

I’m fairly new to Java & MySQL and right now I don’t have a working idea.

  • 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-16T12:22:35+00:00Added an answer on June 16, 2026 at 12:22 pm

    it’s still the same and on the class Fruit it says the id is unused.

    not true, my class Item works as I expected, in your case required for better help sooner post an SSCCE, otherwise everything here are shots to the dark, for example, you can to modify and apply ItemRenderer too,

    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.SwingUtilities;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class SelectedComboBoxID {
    
        public SelectedComboBoxID() {
            JComboBox comboBox = new JComboBox();
            comboBox.addItem(new Item(1, "-"));
            comboBox.addItem(new Item(2, "Snowboarding"));
            comboBox.addItem(new Item(3, "Rowing"));
            comboBox.addItem(new Item(4, "Knitting"));
            comboBox.addItem(new Item(5, "Speed reading"));
            comboBox.addItem(new Item(6, "Pool"));
            comboBox.addItem(new Item(7, "None of the above"));
            comboBox.setMaximumRowCount(3);
            comboBox.setPrototypeDisplayValue(" None of the above ");
            comboBox.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JComboBox comboBox = (JComboBox) e.getSource();
                    Item item = (Item) comboBox.getSelectedItem();
                    System.out.println(item.getId() + " : " + item.getDescription());
                }
            });
            //comboBox.setRenderer(new ItemRenderer());
            JFrame frame = new JFrame("MyComboEg");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(comboBox);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
       private class ItemRenderer extends BasicComboBoxRenderer {
            private static final long serialVersionUID = 1L;
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value != null) {
                    Item item = (Item) value;
                    setText(item.getDescription().toUpperCase());
                }
                if (index == -1) {
                    Item item = (Item) value;
                    setText("" + item.getId());
                }
                return this;
            }
        }
    
       private class Item {
    
            private int id;
            private String description;
    
            public Item(int id, String description) {
                this.id = id;
                this.description = description;
            }
    
            public int getId() {
                return id;
            }
    
            public String getDescription() {
                return description;
            }
    
            @Override
            public String toString() {
                return description;
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    SelectedComboBoxID selectedComboBoxID = new SelectedComboBoxID();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code I have 2 ComboBox DropDownLists, created by the code below. The
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created an android application that calls (using kSOAP library) a SOAP based
I have created a Registration form using java swing package, For the purpose of
I have created a custom dialog for Visual Studio Setup Project using the steps
I have a JComboBox already created but I would like to update the information
I have created a JDialog which contains a JComboBox and a panel underneath which
I have created customized combobox which extends JCombobox. It is having checkboxes in popup
I have a created a UI in swing using NetBeans. There are some use
I have created a custom JComboBox with a custom popup that implements the KeyListener

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.