?
Solved

How do I retrieve the value from the ComboBox which binds to the database.

Posted on 2009-02-21
6
Medium Priority
?
763 Views
Last Modified: 2013-11-23
In Netbeans I currently have a ComboBox. I bind the ComboBox to a table, it collects a column for me. How do I get the value of that column and use it throughout the program. I want to take for instance whatever is selected from the ComboBox, the primary key or foreign key. Then I can use that information on manual queries to the database to fill in other areas of the JFrame.
combodb.JPG
0
Comment
Question by:namsu55
  • 3
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
fsze88 earned 750 total points
ID: 23703626

        jComboBox1.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    jComboBox1_itemStateChanged(e);
                }
            });

    private void jComboBox1_itemStateChanged(ItemEvent e) {
        System.out.println(jComboBox1.getItemAt(jComboBox1.getSelectedIndex() )) ;
    }
0
 

Author Comment

by:namsu55
ID: 23703649
Hi fsze88 that worked. Please view the image. Do you know how to get the specific primary key value if I do click on one out of the list, as I will want to store it inside a variable. I get the classname and tablename showing up as well as the primary keys.
combo.bmp
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 750 total points
ID: 23703665
All you need to do is call the following in the method you write to query the db, casting the String  to the appropiate type in your query:
String key = jComboBox1.getSelectedItem().toString();

Open in new window

0
 
LVL 15

Expert Comment

by:fsze88
ID: 23703727
   private void jComboBox1_itemStateChanged(ItemEvent e) {
        String comboBoxValue;
        String Parameter;
        String[] pair;
       
        int beginIndex, endIndex;
        comboBoxValue = String.valueOf( jComboBox1.getItemAt(jComboBox1.getSelectedIndex() ));
        beginIndex = comboBoxValue.indexOf("[") +1;
        endIndex = comboBoxValue.indexOf("]");
        Parameter = comboBoxValue.substring(beginIndex,endIndex);
        System.out.println("Parameter : " + Parameter);
        pair = Parameter.split("=");
        System.out.println("value of figher : " + pair[1]);
//        System.out.println(e.toString() + e.getStateChange());
//        System.out.println(jComboBox1.getItemAt(jComboBox1.getSelectedIndex() )) ;
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23703960
:-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month6 days, 4 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question