jcombobox

Java JComboBox is it possible to set editable true into one item only?

只谈情不闲聊 提交于 2019-12-01 12:17:47
问题 i want to create a JComboBox that have a three items on it.. I want to set editable only to the last item of the JComboBox for example: JComboBox cb = new JComboBox(); cb.addItem("Dog"); cb.addItem("Cat"); cb.addItem("Other"); when i drop down the JComboBox and choose the item Others.. JComboBox became editable.. how can I do that? 回答1: You can use an ItemListener to know which item is selected and then you can set the JComboBox editable as needed. Try this example: import java.awt.Dimension;

Java Swing Combobox removeAllItems calling ItemStateChanged also?

霸气de小男生 提交于 2019-12-01 10:48:24
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.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { Cmb_ItemCategoryItemStateChanged

JCombobox - Only execute actionlistener when value changes

风流意气都作罢 提交于 2019-12-01 07:40:54
问题 I have a JComboBox , and I have a listener attached to it. Right now every time the user "selects" something from the drop-down the event fires, even if they just reselected the value that was selected prior. Is there any way to only fire the event if the selected value of the combo box is DIFFERENT than it was before it was selected? I imagine I could store the value of the combo box in a different field, and compare it on event firing each time, this just seems kind of overkill. I have 20

How do I allow a user to change his font in a JTextPane using a JComboBox?

こ雲淡風輕ζ 提交于 2019-12-01 07:21:45
问题 I'm finding the amount of helpful documentation/tutorials on the internet are lacking when it comes to the topic of JTextPanes. I'm trying to do a simple text processor, and I want it to be able to select a font family from a JComboBox that populates itself based on the fonts a user has installed on their system. However, no matter what I try with experimenting, I can't figure out how to make it work. What I have is a toolbar class that is built off of a JTextPane. Currently, it has a bunch

Disabling individual JComboBox items

做~自己de王妃 提交于 2019-12-01 06:49:12
This is a fairly common problem, and the solution I've used is similar to what I searched and found later. One implements a ListCellRenderer with a JLabel that enables or disables itself based on the current selected index: public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setText(value.toString()); UIDefaults defaults = UIManager.getDefaults(); Color fc; if (index == 1) { setEnabled(false); fc = defaults.getColor("Label.disabledForeground"); setFocusable(false); } else { // fc = defaults.getColor("Label.foreground");

display a non-selectable default value for JComboBox

倖福魔咒の 提交于 2019-12-01 06:17:10
I have a JComboBox that contains three Items {"Personel", "Magasinier", "Fournisseur"} . I want this JComboBox to display the value "Choisir une option :" , which is a non-selectable value. I tried this code after initComponents(); : this.jComboBox1.setSelectedItem("Choisir une option :"); but it doesn't work. How can I do that ? You could override the selection code in your JComboBox model, with code such as the following SSCCE: public class JComboExample { private static JFrame frame = new JFrame(); private static final String NOT_SELECTABLE_OPTION = " - Select an Option - "; private static

JComboBox fails to expand in JTable TableHeader

倾然丶 夕夏残阳落幕 提交于 2019-12-01 05:39:57
I have read through the majority of the JTable/JComboBox responses to other questions of this ilk, but haven't yet found a solution to my problem. I have created a table that has JComboBox TableHeader elements. None of the JComboBox elements will open to display a list of items. How do I get the item lists for the individual JComboBox elements to display? Please note that a distinguishing element of this question is that the JComboBox is in the TableHeader, not embedded within a JTable cell. Any help is appreciated. SSCE import java.awt.Component; import java.awt.Dimension; import java.util

display a non-selectable default value for JComboBox

时光毁灭记忆、已成空白 提交于 2019-12-01 05:29:10
问题 I have a JComboBox that contains three Items {"Personel", "Magasinier", "Fournisseur"} . I want this JComboBox to display the value "Choisir une option :" , which is a non-selectable value. I tried this code after initComponents(); : this.jComboBox1.setSelectedItem("Choisir une option :"); but it doesn't work. How can I do that ? 回答1: You could override the selection code in your JComboBox model, with code such as the following SSCCE: public class JComboExample { private static JFrame frame =

editable JComboBox

☆樱花仙子☆ 提交于 2019-12-01 04:03:23
I have editable JComboBox and wish to add values to it from its input,e.s when I typing something in JComboBox and press enter I want that text appear in JComboBox list : public class Program extends JFrame implements ActionListener { private JComboBox box; public static void main(String[] args) { new Program().setVisible(true); } public Program() { super("Text DEMO"); setSize(300, 300); setLayout(new FlowLayout()); Container cont = getContentPane(); box = new JComboBox(new String[] { "First", "Second", "..." }); box.setEditable(true); box.addActionListener(this); cont.add(box); } @Override

Synchronized JList and JComboBox?

故事扮演 提交于 2019-12-01 03:53:57
In Java Swing, what's the best way for a JList and a JComboBox to be synchronized in terms of the data, i.e., to have the same list of items at any given point of time? Basically, if I add items to (or remove items from) one, the other should reflect the change automatically. I've tried doing the following, but it doesn't seem to work: JList list = new JList(); JComboBox comboBox = new JComboBox(); DefaultListModel listModel = new DefaultListModel(); // add items to listModel... list.setModel(listModel); comboBox.setModel(new DefaultComboBoxModel(listModel.toArray())); Your models - the