FAQ Database Discussion Community


java refer to component by a variable name [closed]

java,jcombobox
okay, i'm trying to figure out if there's a way to do the following: say i have: jcombobox someCombo1 = new jcombobox(); jcombobox someCombo2 = new jcombobox(); changeSomething(someCombo1); changeSomething(someCombo2); i want to be able to refer to this combobox later, but by a variable from a method, say: public void...

JTable JComboBox is defaulting to display 1st item when list is expanded

java,swing,jtable,jcombobox,jlist
I have been trying to determine why my JComboBox is displaying the 1st item in the list through numerous Google searches, but I'm struggling to find relevant help. It could be that I don't know the correct terminology (hence the overly specific title of this question) and thus not finding...

Highlight text in JComboBox then erase it if user input something

java,swing,text,actionlistener,jcombobox
I'm trying to make a 'JComboBox' that can take user input, but that's not the problem. I want that if the 'JComboBox' loose the focus then regain it the text is highlighted. Then if the user input anything it erase the text in place and replace it by the new...

JComboBox with lookup at Geocoder Google APIs

java,swing,autocomplete,jcombobox
I'm trying to implement a custom JComboBox with a lookup on Google Geocoder API when the user hit VK_ENTER into the editable JComboBox. Here the code: package lucasepe.desktop.arsenal.widgets; import java.awt.Component; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import javax.swing.BorderFactory; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JLabel;...

Java: Make one item of a jcombobox unselectable(like for a sub-caption) and edit font of that item

java,swing,jcombobox
How to make one item in a combobox unselectable because I need to separate items in a combobox with a sub-topic. And is it possible to modify the font of that particular item individually? jComboBox_btech_course.setFont(new java.awt.Font("Tahoma", 0, 14)); jComboBox_btech_course.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Course" })); jComboBox_btech_course.setName(""); private class theHandler implements...

Accessing JComboBox data from separate JForm

java,swing,netbeans,jframe,jcombobox
I've spent a bit of time browsing Stack Overflow and the internet looking for the answer to my question, but I found all the answers hard to understand and I was quite unsure if any of them related to my problem, so I decided I needed help in the right...

how to apply ItemListener on JComboBox

java,swing,jcombobox,itemlistener
import java.awt.*; import java.awt.event.*; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.*; public class AddBook extends JPanel { JLabel book_name_lbl,author_lbl,isbn_lbl,available_lbl,new_lbl,total_book_lbl,title_lbl; JTextField isbn_text,available_text,new_text,total_book_text; JComboBox book_name_box,author_box; JButton submit_btn,exit_btn; Connection conn; Statement smtp; ResultSet rs; String book_name; public AddBook(Connection c) { conn = c;...

Why is the code creating two JDialog objects?

java,swing,jcombobox,jdialog
I have created a JFrame in my project which takes input from JComboBox. I have added EventListener to combo box. I.E. when I select "Other" item from ment, it should create one instance of JDialog. But It's creating two instances. Can anybody tell me why is this happening? Main package...

JComboBox dont get my values

java,swing,jcombobox
I got a problem with my JComboBox. description: I create a new file by writing the name of my file in a Textfield. by clicking on a button I create a file with this value and add this into my JComboBox, but I only see the Object value, for example...

How to have two JComboBox with the same elements?

java,swing,clone,jcombobox,comboboxmodel
I have a list (ArrayList) with elements that I want to show in two JComboBox so both of them show the same elements but when you choose some elements in one JComboBox the other one must not change. What I do now is to create two DefaultComboBoxModel and add the...

radio button use what setter?

java,swing,jcombobox,jradiobutton
My values get it from database (mysql). If in text field, there will be like this : txtMID.setText("" + rs.getString("menu_id")); Are in combo box like this? cmbMCat.setSelectedItem("" + rs.getString("menu_cat")); How about radio button? I think that use if-else condition. But I don't get how to write the code. My radio...

hide options in comboBox java swing

java,swing,duplicates,jcombobox
I'm wanting to create the option for users in a game to select their own piece to play with from a list of piece's that I have made. To do this I have two comboBox's that contain the same items in them but i dont want users to be able...

How to set Label as Display and value as value in JCombobox in Swing?

java,swing,jcombobox
My code dynamically creates Jcombobox and populates items from a query Result. This query returns an Object which includes label and value. I want to display label and returns value when calling combo.getSelectedItem(). I saw this Example while searching, but I didn't get the idea. JComboBox<String> jComboBox=new JComboBox<String>(); if(dataSourceAttributeObjs!=null &&...

Issue about JComboBox with actionlistner

java,swing,actionlistener,jcombobox
For the below example, I am writing a JComboBox with data 1,3,5,7,9, and expecting it will change to 2,4,6,8,10 after OK is pressed. However it just doesn't work..... Any advice would be appreciated, thanks. public class Test extends JFrame{ Test (){ final ArrayList<Integer> value = new ArrayList<>(); value.add(1); value.add(3); value.add(5);...

Convert several JComboBoxes, JButtons to a Set in an efficient way

java,swing,collections,set,jcombobox
My program uses several JComboBoxes and JButtons to allow the users how and which data is shown. Until today I did this with many of these: if (e.getSource() == someMenü_or_Button){ if(someMenü_or_Button.getSelectedItem()=="showstuff")display.oneOfManySetter(0); } Now I thought that I could collect all the options in a Set<String> Attributes = new HashSet<String>(); But...

How can I extend JComboBox to allow user to remove list items by clicking an x button, like an Internet browser tab?

java,swing,jbutton,jlabel,jcombobox
I am trying to create an object that resembles a JComboBox, but I want the user to be able to remove an item from the list. Imagine that each list element has to its right an x icon or button like what you find on a tab in your internet...

jcombobox populate with hashmap

java,swing,hashmap,jcombobox
I want to fill my combobox with a display value and a number value HashMap<Integer, String> databaseList I would to display the string in the combobox but the value of the comboxbox must be integer. I know this is posible in c# is this also possible in Java?...

How to change height of JComboBox?

java,swing,combobox,size,jcombobox
I have a class extends JFrame and want to add JComboBox on a panel. When I added this, the JComboBox size is very large, I want to change size, but don't know how to do this. .setSize() and .setPrefferedSize() not worked. JFrame layout must be GridLayout. public CountryController() { setLayout(new...

Wrong argument to the function - AutoComplete in JComboBox

java,swing,autocomplete,jcombobox
I am using a JComboBox as a cell editor for one of my JTbale column.I want to implement autocomplete in my JComboBox. I used a jar file from this link here. When I run my program it is giving me this error: at Exception in thread "AWT-EventQueue-0" java.lang.VerifyError: (class: org/jdesktop/swingx/autocomplete/Configurator,...

How can I put a index in a jcombobox in java?

java,indexing,jcombobox,value
I want to put in a jcombobox, the name and use the id for link the option select and the name. I get the data of the db, but I don't know the way for add a items. I try to write a item, with 2 parameters, but in the...

JComboBox, set space between choices

java,touch,jcombobox
I have created a program that will be used on both computer and tablet. The problem is that the options that come up when you press the JComboboxes becomes very small on the tablet. How can you with ease get a small gap between the options? Or are there other...

Adding items to combo box in a table in netbeans

java,swing,netbeans,jtable,jcombobox
I have a table and one of its columns needs to contain combo boxes. When I run the following code, I get combo boxes in each cell but the items are not added into it. When I try to print the items using system.out.println(val), they get printed but they do...

Editable JComboBox Having no default selection

java,swing,jcombobox
When I have not made any selection yet, how do I make nothing appear in the editable field? Usually the first option appears and I have to backspace it before typing my custom value.

How to link two jComboBoxes together and display the data into a jtextarea?

java,swing,jcombobox,jtextarea,jlist
My question is how to link two comboboxes together. The reason being that I want the user to be able to choose a manufacturer brand and for the next combobox to display the cars linked to that manufacturer. Then I want the data to be put into a jtextarea to...

JComboBox Select and Item and Based off of the selection display a new list

java,swing,user-interface,jcombobox
I have the following code so that based off of the user selection a list is displayed: private class SheepTypeListHandler implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { JComboBox<String> cb = (JComboBox<String>)(e.getSource()); if (cb.getSelectedItem().equals(sheepType[0])) { for (String item : eweClass ) sheepClassCB.addItem(item); } else if (cb.getSelectedItem().equals(sheepType[1])) { for (String item...

Initializing a JComboBox object using a String array

java,arrays,swing,jcombobox
I did it as follows, but it wouldn't work: JComboBox brand = new JComboBox({"Pizza Hut", "Papa John's", "Dominos" }); The error is: Syntax error on token "new", @ expected after this token Kindly help me understand what the problem is....

Java: getSelectedItem() is undefined for type string

java,string,swing,jcombobox
I am having a problem with my java program, i am trying to get some information for an if statement using JComboBox and a JButton. The problem is that the .getSelectedItem() is undefined and i do not know what to do about this. These are the combo boxes: static String...

Exception in thread “AWT-EventQueue-0” java.lang.StringIndexOutOfBoundsException: String index out of range: 8

java,swing,substring,jcombobox,bufferedwriter
I have several jComboBoxes and jTextFields on my jPanel. Selected and typed values saving in text file. PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter... outfile.write(jcb1.getSelectedItem().toString().substring(1, 8)); outfile.write(jTextField1.getText()); But when I allow jComboBoxes to be empty, I cannot save empty value of jcb1 and getting this error: Exception in thread "AWT-EventQueue-0"...

How To Initialize a JComboBox Whose Items are added Dynamically?

java,swing,jcombobox
I have Created a JComboBox and its item are being added dynamically through a LinkedList, how to initialize its selected value. Suppose "list" contains A->B->C->D->null I want to intialize the ComboBox selected index with B (i.e 2nd Item In the list). I have tried to do it as below ComboBox.setSelectedIndex(1);...

seek to code properly [closed]

java,swing,jcombobox
I a GUI which contains two JTextField and one JComboBox, based on the content of the JComboBox, I will show another JPanel that contains a set of components. so I proceeded as follows (it worked) but I want to know if I coded properly. I created a class that filled...

JComboBox Option Selected Not Imposing Change

java,string,swing,jcombobox
In my code I have a simple JComboBox which is used to turn a certain feature on or off. The JComboBox code is: public static JComboBox getErrorLoggingOnOrOff(){ String[] options = { "On", "Off" }; JComboBox combo = new JComboBox(options); return combo; } The issue is that this always returns the...

ActionListener of JComboBox and initialize JPanel

java,swing,compiler-errors,actionlistener,jcombobox
I want to write program in which you can chance scene (JPanel) thanks to JComboBox. I used ActionListener, but it doesn't work. At the beginning of constructor I defined panel as final, but it didn't help. scene.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String choice = String.valueOf(scene.getSelectedItem()); if(choice=="Sceneria"||choice=="Scene"){ slider.setEnabled(false);...

grabbing the selected data from the Jcombo

java,swing,jcombobox
Trying to save the users selection into a string to use further in the code. but I'm unsuccessful in retrieving the data that the users selects. not to sure if I'm doing it wrong or right, or how to do it to be honest because i feel as if I'm...

jcombobox holding array of int error

java,jcombobox
Can a Jcombobox hold an int? because i tried this and got an error. int[] timeSched = new int[] {200,300,400,500}; JComboBox Jcombo1 = new JComboBox(timeSched);...

DefaultListCellRenderer does not render empty String correctly when using an editable combo box

java,swing,render,jcombobox
I've installed a custom renderer to an editable JCombobox, but I've problems with rendering an empty String. Do you have an idea how to show the empty item with the correct/similar height? Or this is a Java bug? See the following example and the screenshots for the difference: package combo;...

How to fill JComboBox after user fills necessary text fields?

java,swing,arraylist,jcombobox
I want to add a JComboBox to my main panel and fill it with an ArrayList I made in a different method named rectSizeList (which right now prints the ArrayList to the console), this method gets its parameters from another static method named actionPerformed. I cant figure a way to...

How to display all Cardlayouts

java,swing,layout-manager,jcombobox,cardlayout
How do I get the String from the selected card on the combo box and by using the getSelectedItem method on the fractalChooser combobox and cast the result to String? Currently it only displays one object. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FractalDriver { private static final int...

JComboBox gives an array after running for 3 times [jdbc]

java,swing,netbeans,jdbc,jcombobox
i was making an library management system, here when u are adding book, u select a category from jcombobox, now jcombobox is getting categories(items) from another table, issue is this that it runs perfectly for two or three times after it , it gives an error and ADD BOOK frame...

Getting Integer value from JComboBox in java

java,swing,integer,jcombobox,classcastexception
I am using JComboBox to give Integer value as input.Tell me which fuction i can use to get the value from combobox. My combox will have these values. cb1=new JComboBox(); for(int i=1;i<=31;i++) { if(i<10) { cb1.addItem("0"+i); } else { cb1.addItem(i); } } I used this function to get the value....

Radio button to change JCombo Data

java,swing,radio-button,jcombobox,buttongroup
Looking for a way to change the data outputted to the combo box by selecting the radio buttons, but the data is being pulled in off a text file and saved into an array and then passed back into JCOMBO array list. sorry if question is a big vague i...

NullPointerException when i try Select data from Mysql base

java,mysql,model-view-controller,jcombobox
I have problem. When i try select data from database i see error NullPointerException. To connect to database i use Singleton pattern. Singleton class : public class Database { private static Database instance = new Database(); private Connection con; private Database() { } public static Database getInstance() { return instance;...

How to get an input from two different combo boxes?

java,swing,jcombobox
I have to write a Java code where I should get input from 2 different combo boxes. The input, that I will get, has to be display in a text field. I have written a part of my code but I can't get the input. This is what I have...

How can I hold the menu open in the JComboBox?

java,swing,jcombobox
I have create a custom JComboBox, so if I try to open my comboBox, I can see the check Button near the description. This is ok. But if I want to select n items I must, open the select list, then check one items, re-open the select list, select another...

JoptionPane is showed two times when a list item is selected

java,swing,jcombobox,itemlistener
When I click on an item of a JComboBox to show a JoptionPane with the text of selected item , it is showed two times. How can I desactive the item listener event? import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; public class Combo implements ItemListener { JFrame f; JComboBox cb; Combo()...

display the text file item from certain column in jcombobox

java,string,swing,text-files,jcombobox
I am populating jComboBox1 from data.txt file. I am using this code to populate jComboBox1. The structure of text file as follows: AAA 123456 BB 22 Some text AAA 234567 CC 23 Some text AAA 345678 DD 24 Some text The goal that I want to achieve is display in...

How to get the value from 2nd SubComboBox?

java,jcombobox
I'm new here, I need help about the codes in Java. And asking question here is my last option, and there you go. I already squeezed every last bit in my brain. I didn't get the output that I want. This code was posted here in stackoverflow. And this code...

Adding a JComboBox to a JTable cell. Selected item doesn't stay

java,swing,jtable,jcombobox,selecteditem
I looked through a few posts here but couldn't find an answer to this. I have successfully added a JComboBox to a Jtable cell. However the selected item is not being "remembered". More specifically: 1) Select an item from a combo box of row A 2) Go to select a...

Search Icon inside a Editable JComboBox / JTextField

java,swing,jcombobox
I have an Editable JComboBox search field in one of my Java swing application. I wanted to save space of the search box header and put in search icon inside the editable jcombobox to depict it as a search field. Which should look quite similar to a Facebook search...