jcombobox

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

只愿长相守 提交于 2019-12-04 10:44:07
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) { //BTech courses if(jComboBox_mtech_dept.getSelectedItem().equals("Civil Engineering")) { jComboBox

How to use actionPerformed( ActionEvent e ) with more that one button?

我怕爱的太早我们不能终老 提交于 2019-12-04 06:03:00
问题 So my assignment says to create a sequential file. My professor gave me this simple code for the actionperformed: public void actionPerformed( ActionEvent e ) { //FOR STATE AND COUNTRY String country = (String)comboBox_1.getSelectedItem(); Object o = states.get( country ); if (o == null) { comboBox_2.setModel( new DefaultComboBoxModel() ); } else { comboBox_2.setModel( new DefaultComboBoxModel( (String[])o ) ); } //****DONE WITH THE STATE AND COUNTRY COMBOBOXEZ***** addRecord( ) ; if ( e

Values of JComboBox

帅比萌擦擦* 提交于 2019-12-04 03:49:26
问题 Is it possible, to define values different from the actual content in a JComboBox? In HTML it looks as follows: <select> <option value="value1">Content1</option> <option value="value2">Content2</option> <option value="value3">Content3</option> </select> Here it's possible to get a short value, no matter how long its content is. In Java I only know the following solution: // Creating new JComboBox with predefined values String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; private

JComboBox how to compute for two integers from two JComboBox and have the result in a JTextfield when clicking the JButton

感情迁移 提交于 2019-12-04 02:33:00
问题 I have 2 JComboBox consisting of numbers combobox1= 1 to 5 and c ombobox2= 1 to 6. and when I click my JButton , I want the two chosen numbers to be added and shown on a Textfield. I already have the complete code except for the calculation and how to have the result in the textfield. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class exer1 extends JFrame{ JFrame form = new JFrame ("haay"); JButton btn = new JButton ("Compute"); JTextField txt = new JTextField (10)

Disabling individual JComboBox items

倾然丶 夕夏残阳落幕 提交于 2019-12-04 01:37:04
问题 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(

Checking if an item already exists in a JComboBox?

好久不见. 提交于 2019-12-03 09:55:27
Is there an easy way to check if an item already exists in a JComboBox besides iterating through the latter? Here's what I want to do: Item item = ...; boolean exists = false; for (int index = 0; index < myComboBox.getItemCount() && !exists; index++) { if (item.equals(myComboBox.getItemAt(index)) { exists = true; } } if (!exists) { myComboBox.addItem(item); } Thanks! Use a DefaultComboBoxModel and call getIndexOf(item) to check if an item already exists. This method will return -1 if the item does not exist. Here is some sample code: DefaultComboBoxModel model = new DefaultComboBoxModel(new

How do I add a separator to a JComboBox in Java?

会有一股神秘感。 提交于 2019-12-03 09:27:05
问题 I have a JComboBox and would like to have a separator in the list of elements. How do I do this in Java? A sample scenario where this would come in handy is when making a combobox for font-family-selection; similar to the font-family-selection-control in Word and Excel. In this case I would like to show the most-used-fonts at the top, then a separator and finally all font-families below the separator in alphabetical order. Can anyone help me with how to do this or is this not possible in Java

How to set the title of a JComboBox when nothing is selected?

依然范特西╮ 提交于 2019-12-03 08:05:32
I want to have a JCombobox in my Swing application, which shows the title when nothing is selected. Something like this: COUNTRY ▼ Spain Germany Ireland I want "COUNTRY" to show when the selected index is -1 and thus, the user wouldn't be able to select it. I tried to put it on the first slot and then overriding the ListCellRenderer so the first element appears greyed out, and handling the events so when trying to select the "title", it selects the first actual element, but I think this is a dirty approach. Could you lend me a hand? Overriding the ListCellRenderer is a good approach, but you

Removing all Items from a combo box in Java

别说谁变了你拦得住时间么 提交于 2019-12-03 08:04:59
I need to remove all items from the combo box int itemCount = combo.getItemCount(); for(int i=0;i<itemCount;i++){ combo.removeItemAt(0); } This code will remove all items except the last one. It gives a NullPointerException. How to fix that? The code in the question would normally work. However, it looks like a threading issue. Another thread may be messing with the items. However, I sugeest you should better use the removeAllItems(); method: combo.removeAllItems(); How about JComboBox.removeAllItems() ? Asad In second line: combo.removeItemAt(0) ; I think instead of 0 it should be i . do it

Java can't find symbol, Java can't find my symbol in an array?

主宰稳场 提交于 2019-12-02 23:58:02
问题 This is the error i get when i write this line of code. symbol : variable isSeletecd location: class java.lang.String if (dorm[0].isSeletecd && meal[0].isSeletecd()) ^ if (dorm[0].isSeletecd && meal[0].isSeletecd()) totalCharges.setText("2150.00"); Program: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing