jcombobox

JComboBox popup menu not appearing

ⅰ亾dé卋堺 提交于 2020-01-02 17:20:11
问题 I have a JComboBox inside a JPanel (which itself is nested within a few other JPanels). It's populated with members of an enum. I'm running into a problem where the popup menu doesn't appear when I click the expand button. Here's the information I've gathered so far: 1) The first click on the expand button does nothing. The second click highlights the contents of the box, but the popup still doesn't appear. 2) Once I've clicked the button and given it focus, up/down keystrokes cycle through

How do I detect that a JComboBox is empty?

那年仲夏 提交于 2020-01-02 06:16:21
问题 How do I detect that a JComboBox is empty? Is it something like: combobox.isEmpty() 回答1: What's wrong with JComboBox.getItemCount()? If this method returns 0 , the component is empty. 回答2: if(JComboBox.getItemCount() != 0){ //JComboBox is not empty - do something.. } 回答3: I suppose you're looking for getItemCount() method 来源: https://stackoverflow.com/questions/9161659/how-do-i-detect-that-a-jcombobox-is-empty

JcomboBox multiple selection

那年仲夏 提交于 2020-01-02 01:30:28
问题 I have a jcombobox in my application. and i want it to support multiple selection (like it do in a jlist). is there any code example? 回答1: I think it's not possible, unless you use a JList, like you said. The JComboBox API reports: The user can select a value from the drop-down list, which appears at the user's request. And a JComboBox tutorial: Lists are not terribly attractive, but they're more appropriate than combo boxes when the number of items is large (say, over 20) or when selecting

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

可紊 提交于 2020-01-01 12:18:05
问题 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 ActionListener { public void actionPerformed(ActionEvent evt)

Populating JCombobox with a text file [duplicate]

三世轮回 提交于 2019-12-31 07:17:08
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: How do I populate JComboBox from a text file? I am new to programming Java with only 2 months of experience. Can anyone help me to populate a JComboBox with a text file, consisting of 5 lines? I have looked at code on Google, but I keep getting errors. 回答1: private void populate() { String[] lines; lines = readFile(); jComboBox1.removeAllItems(); for (String str : lines) { jComboBox1.addItem(str); } } Here is

Using the ComboBoxEditor interface with Custom JComponent, and allow edit, and display the List

别说谁变了你拦得住时间么 提交于 2019-12-31 05:35:10
问题 I was checking the documentation. https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html https://alvinalexander.com/java/jwarehouse/openjdk-8/jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxEditor.java.shtml http://www.java2s.com/Tutorials/Java/javax.swing/ComboBoxEditor/Java_ComboBoxEditor_getEditorComponent_.htm ... and I'm trying to use my Custom ComboBox implementing the interface ComboBoxEditor . Here my complete Code... I hava one JPanel with JComponents ...

Detecting when user presses enter in Java

被刻印的时光 ゝ 提交于 2019-12-30 19:54:41
问题 I have a subclass of JComboBox. I attempt to add a key listener with the following code. addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { if(evt.getKeyCode() == KeyEvent.VK_ENTER) { System.out.println("Pressed"); } } }); This however does not correctly detect when the user presses a key. It is actually not called at all. Am I adding this listener wrong? Are there other ways to add it? 回答1: Key events aren't fired on the box itself, but its editor. You need to add the

Detecting when user presses enter in Java

血红的双手。 提交于 2019-12-30 19:52:12
问题 I have a subclass of JComboBox. I attempt to add a key listener with the following code. addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { if(evt.getKeyCode() == KeyEvent.VK_ENTER) { System.out.println("Pressed"); } } }); This however does not correctly detect when the user presses a key. It is actually not called at all. Am I adding this listener wrong? Are there other ways to add it? 回答1: Key events aren't fired on the box itself, but its editor. You need to add the

populate multiple combobox with same model but select diff

和自甴很熟 提交于 2019-12-30 11:53:13
问题 Having problems with the ComboBox , I have populated multiple ComboBox es with the same model, but when I run my program and select a value from one ComboBox it selects the same value for the rest. ComboHBoy.setModel(defaultComboBoxModel); ComboHGirl.setModel(defaultComboBoxModel); ComboDHBoy.setModel(defaultComboBoxModel); ComboDHGirl.setModel(defaultComboBoxModel); 回答1: That's because they all are referenced to the same model , any change of the model will affect the all the other combos.

Java Swing Combobox removeAllItems calling ItemStateChanged also?

血红的双手。 提交于 2019-12-30 11:05:25
问题 My code is quite simple actually. I saw a simple and similar code was from this article. At first, I have 1 combobox . I have a listener on it called itemStateChanged() . My purpose to add into this listener is that; " to execute some code when user click (select) an item from its dropbox ". Cmb_ItemCategory = new javax.swing.JComboBox(); Cmb_ItemCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Loading..." })); Cmb_ItemCategory.addItemListener(new java.awt.event