如何从JComboBox中选择与多个阵列选择相关联?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
class ContactsReader extends JFrame
{
public JPanel mainPanel;
public JPanel buttonPanel;
public JPanel displayPanel;
public JLabel titleLabel;
public JLabel nameLabel;
public JLabel ageLabel;
public JLabel emailLabel;
public JLabel cellPhoneLabel;
public JLabel comboBoxLabel;
public JButton exitButton;
public JTextField nameTextField;
public JTextField ageTextField;
public JTextField emailTextField;
public JTextField cellPhoneTextField;
public JComboBox<String> contactBox;
public String[] getContactNames;
public String[] displayContactNames;
public File contactFile;
public Scanner inputFile;
public String selection;
public ContactsReader()
{
super("Contacts Reader");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildPanel();
add(mainPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void buildPanel()
{
titleLabel = new JLabel("Please enter contact information");
mainPanel = new JPanel(new BorderLayout());
buttonPanel();
displayPanel();
mainPanel.add(titleLabel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(displayPanel, BorderLayout.CENTER);
}
public void buttonPanel()
{
//create submit and exit buttons
exitButton = new JButton("Exit");
exitButton.addActionListener(new exitButtonListener());
buttonPanel = new JPanel(); //create button panel
buttonPanel.add(exitButton); //add exit button to panel
}
public void displayPanel()
{
String nameHolder;
int count = 0;
nameLabel = new JLabel("Name");
ageLabel = new JLabel("Age)");
emailLabel = new JLabel("Email");
cellPhoneLabel = new JLabel("Cell Phone #");
comboBoxLabel = new JLabel("Select a Conact");
nameTextField = new JTextField(10);
nameTextField.setEditable(false);
ageTextField = new JTextField(10);
ageTextField.setEditable(false);
emailTextField = new JTextField(10);
emailTextField.setEditable(false);
cellPhoneTextField = new JTextField(10);
cellPhoneTextField.setEditable(false);
try{
contactFile = new File("ContactData.txt");
inputFile = new Scanner(contactFile);
}
catch (Exception event){}
while (inputFile.hasNext())
{
nameHolder = inputFile.nextLine();
count++;
}
inputFile.close();
String getContactNames[] = new String[count];
String displayContactNames[] = new String[count/4];
try{
contactFile = new File("ContactData.txt");
inputFile = new Scanner(contactFile);
}
catch (Exception event){}
for (int i = 0; i < count; i++)
{
if (inputFile.hasNext())
{
nameHolder = inputFile.nextLine();
getContactNames[i] = nameHolder;
if (i % 4 == 0)
{
displayContactNames[i/4] = getContactNames[i];
}
}
}
inputFile.close();
contactBox = new JComboBox<String>(displayContactNames);
contactBox.setEditable(false);
contactBox.addActionListener(new contactBoxListener());
displayPanel = new JPanel(new GridLayout(10,1));
displayPanel.add(comboBoxLabel);
displayPanel.add(contactBox);
displayPanel.add(nameLabel);
displayPanel.add(nameTextField);
displayPanel.add(ageLabel);
displayPanel.add(ageTextField);
displayPanel.add(emailLabel);
displayPanel.add(emailTextField);
displayPanel.add(cellPhoneLabel);
displayPanel.add(cellPhoneTextField);
}
private class exitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0); //set exit button to exit even when pressed
}
}
private class contactBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//get selection from dropdown menu
selection = (String) contactBox.getSelectedItem();
}
}
public static void main(String[] args)
{
new ContactsReader(); //create instance of Contact Reader
}
}
我希望选择将姓名,年龄,电子邮件和手机号码发送到相应的文本字段。我可以想出一个选择,但不知道如何让它选择正确的数组选择并将其发送到文本字段。
答案 0 :(得分:4)
不要让JComboBox只保存字符串,而是让它保存自定义类的对象,其中包含选择时所需的所有信息。然后使用选定的对象填充JTextField。
例如,考虑创建一个类,比如名为Contact,
public class MyContact {
String name;
Date dateOfBirth; // in place of age
String email;
String cellPhone;
//...
}
然后创建JComboBox<MyContact>
选择项目后,调用相应的getXXX()
getter方法以提取信息以填充JTextField。您需要为JComboBox提供一个自定义的CellRenderer,以便它能很好地显示联系人。
例如:
import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
public class MyComboEg extends JPanel {
private static final MyData[] data = {
new MyData("Monday", 1, false),
new MyData("Tuesday", 2, false),
new MyData("Wednesday", 3, false),
new MyData("Thursday", 4, false),
new MyData("Friday", 5, false),
new MyData("Saturday", 6, true),
new MyData("Sunday", 7, true),
};
private JComboBox<MyData> myCombo = new JComboBox<MyData>(data);
private JTextField textField = new JTextField(10);
private JTextField valueField = new JTextField(10);
private JTextField weekendField = new JTextField(10);
public MyComboEg() {
add(myCombo);
add(new JLabel("text:"));
add(textField);
add(new JLabel("value:"));
add(valueField);
add(new JLabel("weekend:"));
add(weekendField);
myCombo.setRenderer(new DefaultListCellRenderer(){
@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
String text = value == null ? "" : ((MyData)value).getText();
return super.getListCellRendererComponent(list, text, index, isSelected,
cellHasFocus);
}
});
myCombo.setSelectedIndex(-1);
myCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// MyData myData = (MyData) myCombo.getSelectedItem();
MyData myData = myCombo.getSelectedItem();
textField.setText(myData.getText());
valueField.setText(String.valueOf(myData.getValue()));
weekendField.setText(String.valueOf(myData.isWeekend()));
}
});
}
private static void createAndShowGui() {
MyComboEg mainPanel = new MyComboEg();
JFrame frame = new JFrame("MyComboEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MyData {
private String text;
private int value;
private boolean weekend;
MyData(String text, int value, boolean weekend) {
this.text = text;
this.value = value;
this.weekend = weekend;
}
public String getText() {
return text;
}
public int getValue() {
return value;
}
public boolean isWeekend() {
return weekend;
}
}