jcombobox

How to set selected index JComboBox by value

别说谁变了你拦得住时间么 提交于 2019-11-30 08:02:30
I want to set the selected index in a JComboBox by the value not the index. How to do that? Example public class ComboItem { private String value; private String label; public ComboItem(String value, String label) { this.value = value; this.label = label; } public String getValue() { return this.value; } public String getLabel() { return this.label; } @Override public String toString() { return label; } } JComboBox test = new JComboBox(); test.addItem(new ComboItem(0, "orange")); test.addItem(new ComboItem(1, "pear")); test.addItem(new ComboItem(2, "apple")); test.addItem(new ComboItem(3,

Why isn't getSelectedItem() on JComboBox generic?

蓝咒 提交于 2019-11-30 07:49:10
JCombobox in Java 7 has been updated to use generics - I always thought it was a bit of an oversight that it didn't already so I was pleased to see this change. However, when attempting to use JCombobox in this way, I realised that the methods I expected to use these generic types still just return Object. Why on earth is this? It seems like a silly design decision to me. I realise the underlying ListModel has a generic getElementAt() method so I'll use that instead - but it's a bit of a roundabout way of doing something that appears like it could have been changed on JComboBox itself. jarnbjo

JComboBox width

故事扮演 提交于 2019-11-30 06:56:59
问题 I have created a jComboBox but it takes the full width of the frame. how to set the width fixed. yes borderlayout for the frame and box layout for the panel. i am adding the code here: import javax.swing.*; import java.awt.BorderLayout; public class Window8 { JFrame frame; JPanel panel; JComboBox combo; public void go(){ String[] option = { "STUDENT", "TEACHER" }; combo.setPreferredSize(new Dimension(1,25)); combo = new JComboBox(option); menu.setSelectedIndex(0); frame = new JFrame("DELETION

How can I know when the text of an editable JComboBox has been changed?

天大地大妈咪最大 提交于 2019-11-30 01:53:18
I have an editable JComboBox where I want to take some action whenever the text is changed, either by typing or selection. In this case, the text is a pattern and I want to verify that the pattern is valid and show the matches that result in some test data. Having done the obvious, attach an ActionHandler, I have found that, for typing, the event seems to fire unreliably, at best (selection is fine). And when it does fire as a result of typing, the text retrieved (using getEditor().getItem(), since getSelectedItem() only gets the text when it was selected from the list) seems to be the text as

How do I know if an item of an auto-complete decorated JComboBox is mouse clicked?

半世苍凉 提交于 2019-11-29 20:30:07
问题 I'm using the SwingX AutoCompleteDecorator for a JComboBox . The autocomplete feature works beautifully... But I have trouble to identify the moment of the final user selection ; to persist my data seldom. Let me try to explain: The combobox fires an "comboBoxChanged"- ActionEvent for every selection. I have to ignore these events while the user is typing characters and the combobox is auto-matching and selecting items. If the user hits the return-key an "comboBoxEdited"- ActionEvent is

JTable fill data with JComboBox

偶尔善良 提交于 2019-11-29 18:01:06
I have a frame with table,combobox, i want to fill the table with data from database by combobox, but if i use with itemlistener i dont see the table, without itemlistener and String sql="select * from Arlista" then i see my table with data (combob=combobox) combob.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent e){ tesztvalt=(combob.getSelectedItem()).toString(); if (e.getItem().equals(tesztvalt)) { try { Class.forName( driver ); Connection connection = DriverManager.getConnection( url ); String sql="select * from "+tesztvalt+""; Statement stmt =

Using JComboBox as a search box

徘徊边缘 提交于 2019-11-29 17:57:41
Im using a JComboBox to search a query from a sql database. Here is my code. private void srKeyTyped(java.awt.event.KeyEvent evt){ sr.removeAllItems(); String sch = ((JTextField)sr.getEditor().getEditorComponent()).getText(); String schh = "SELECT * FROM tbl WHERE name LIKE '" + sch + "%';"; search = conn.getQuery(schh); try { while (search.next()) { String item = search.getString("name"); sr.addItem(item); } } catch (SQLException ex) { Logger.getLogger(dataprocess.class.getName()).log(Level.SEVERE, null, ex); } sr.setSelectedItem(null); sr.setPopupVisible(true); System.out.println(sch); } sr

Returning column names from table in SQLite using Java GUI

穿精又带淫゛_ 提交于 2019-11-29 17:30:06
I need to get column names from table, only difference is that I made a function that makes new column depending on users entry in txtbox. So for example if a user enters "3", column would be named "Sezonalni_utjecaj_3". Now when you saw that example I need to make a query that returns column names so I can put them inside my combobox, so everytime I enter a new column, the name of the column would be placed inside of the combobox (column names have something in common, and thats "Sezonalni_utjecaj_", but I have other columns too, not only ones that have "Sezonalni_utjecaj_" inside their name

How do you change border of the pop up section of a JComboBox?

ⅰ亾dé卋堺 提交于 2019-11-29 16:44:38
I wan't to change the border of the popup/selection part of the JComboBox. Note that the UI is BasicComboBoxUI I've tried: weaponCB.setRenderer(new DefaultListCellRenderer() { @Override public void paint(Graphics g) { setBorder(whiteBorder) //whiteBorder is a white border super.paint(g); } }); but it gave me this: and: for (int i=0; i<weaponCB.getComponentCount(); i++) { if (weaponCB.getComponent(i) instanceof AbstractButton) { ((AbstractButton)weaponCB.getComponent(i)).setBorder(whiteBorder); } } but it gave me this: what i wan't is something like this: (it was done in photoshop) I don't mind

how to compute the result of two integer values but get the additive or multiplicative operator from a jComboBox in java

落花浮王杯 提交于 2019-11-29 16:28:21
assume i have integer variables a,b and c. c = a + b; or c = a - b; or c = a / b; or c = a * b; As you can see, the computation operator needs to be passed dynamically at run time. So i have a jComboBox of operators, so a user will select either a +, -, * or / from the jcomboBox. how do i get the jCombobox selectedItem(which will be either /, *, -, or + ) and use it in getting the value of c. Eg. if the user selects *, then the expression should be c = a * b else if the user selected say +, then the expression should be c = a + b . Here is a way to 'cheat'. All the parsing is done by the