
i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox.only few values in the combobox need this functionality. need solution. tahnks in advance.

Here is an example that shows the selected value from the jcombobox to textfield.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SelectComboboxValue{
public static void main(String[] args) throws Exception{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab=new JLabel("Select Programming Language: ");
final JComboBox combo=new JComboBox();
final JTextField text=new JTextField(20);
combo.addItem("--Select--");
combo.addItem("Java");
combo.addItem("C/C++");
combo.addItem(".NET");
combo.addItem("Perl");
ActionListener actionListener = new ActionListener(){
public void actionPerformed(ActionEvent actionEvent) {
ItemSelectable is = (ItemSelectable)actionEvent.getSource();
String st=selectedString(is);
text.setText(st);
}
};
combo.addActionListener(actionListener);
lab.setBounds(20,20,200,20);
combo.setBounds(200,20,80,20);
text.setBounds(280,20,100,20);
f.add(lab);
f.add(combo);
f.add(text);
f.setVisible(true);
f.setSize(400,120);
}
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.