JComboBox problem

Hi guys. I have 2 comboboxes that i have created with the following code:

String[] cRateItems ={"Select Rate","5.35","5.50","5.75","Enter Custom Value"};

String[] cTermItems ={"Select Term","7","15","30","Enter Custom Value"};

JComboBox cRate =new JComboBox(cRateItems);

JComboBox cTerm =new JComboBox(cTermItems);

My question is, how do I make the last item in each combobox editable so the user can replace the value "Enter Custom Value" with their own.

Thanks in advace

[945 byte] By [oc084a] at [2007-11-27 2:42:35]
# 1

Hey dude, here's some code man! Righteous!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

public class QuickEditCombo extends JFrame {

public QuickEditCombo(String title) {

super(title);

createUI();

}

public void createUI() {

String[] cRateItems = {"Select Rate","5.35", "5.50", "5.75", "Enter Custom Value"};

String[] cTermItems = {"Select Term","7", "15", "30", "Enter Custom Value"};

JComboBox cRate = new JComboBox(cRateItems);

cRate.setEditable(true);

cRate.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

final JComboBox cmb = (JComboBox)e.getSource();

if(cmb.getSelectedItem().equals("Enter Custom Value")) {

SwingUtilities.invokeLater( new Runnable() {

public void run() {

((JTextField)cmb.getEditor().getEditorComponent()).setText("");

}

});

}

}

});

JComboBox cTerm = new JComboBox(cTermItems);

cTerm.setEditable(true);

cTerm.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

final JComboBox cmb = (JComboBox)e.getSource();

if(cmb.getSelectedItem().equals("Enter Custom Value")) {

SwingUtilities.invokeLater( new Runnable() {

public void run() {

((JTextField)cmb.getEditor().getEditorComponent()).setText("");

}

});

}

}

});

JPanel all = new JPanel();

all.add( cRate );

all.add( cTerm );

getContentPane().add(all);

pack();

setLocationRelativeTo(null);

}

public static void main(String[] args) {

QuickEditCombo qec = new QuickEditCombo("Example");

qec.setVisible(true);

}

}

ICE

icewalker2ga at 2007-7-12 3:07:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hey dude! Here is some code man! Righteous!!!!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

public class QuickEditCombo extends JFrame {

public QuickEditCombo(String title) {

super(title);

createUI();

}

public void createUI() {

String[] cRateItems = {"Select Rate","5.35", "5.50", "5.75", "Enter Custom Value"};

String[] cTermItems = {"Select Term","7", "15", "30", "Enter Custom Value"};

JComboBox cRate = new JComboBox(cRateItems);

cRate.setEditable(true);

cRate.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

final JComboBox cmb = (JComboBox)e.getSource();

if(cmb.getSelectedItem().equals("Enter Custom Value")) {

SwingUtilities.invokeLater( new Runnable() {

public void run() {

((JTextField)cmb.getEditor().getEditorComponent()).setText("");

}

});

}

}

});

JComboBox cTerm = new JComboBox(cTermItems);

cTerm.setEditable(true);

cTerm.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

final JComboBox cmb = (JComboBox)e.getSource();

if(cmb.getSelectedItem().equals("Enter Custom Value")) {

SwingUtilities.invokeLater( new Runnable() {

public void run() {

((JTextField)cmb.getEditor().getEditorComponent()).setText("");

}

});

}

}

});

JPanel all = new JPanel();

all.add( cRate );

all.add( cTerm );

getContentPane().add(all);

pack();

setLocationRelativeTo(null);

}

public static void main(String[] args) {

QuickEditCombo qec = new QuickEditCombo("Example");

qec.setVisible(true);

}

}

ICE

icewalker2ga at 2007-7-12 3:07:23 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks but I was looking for something that makes ONLY the "Enter Custom Value" item editable. Is that possible?
oc084a at 2007-7-12 3:07:23 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Thanks but I was looking for something that makes ONLY the "Enter Custom Value" item editable.

So use the concept present here to implement your requirement. For example when the user clicks on that item, display a JOptionPane asking the user to enter a new value and then add the value entered to the combo box model.

I doubt you will be able to play with the "editable" property of the combo box to do what you want.

camickra at 2007-7-12 3:07:23 > top of Java-index,Desktop,Core GUI APIs...
# 5
why not play with the above code & make the editable or non editable based on the various conditions you have for yourself. Think man! nothing comes cheap.
icewalker2ga at 2007-7-12 3:07:23 > top of Java-index,Desktop,Core GUI APIs...