使用从所选组合框项目中获取的对象数据填充JTextFields

时间:2017-01-11 00:00:46

标签: java swing

我会尽力解释这个问题。

基本上我所拥有的是一个接收客户数据的系统(客户是一个对象),然后将其添加到一个数组中。我的工作没有问题。我正在努力工作的是,我还有一个选项,可以让用户编辑客户的详细信息。我试图实现这个的方式是;在一个单独的GUI中,我有一个comboBox,它将填充当前在arrayList中的所有客户名字。这也有效。

这是我的问题 - 我想要发生的是,当从comboBox中选择一个客户名字时,它应该找到该对象,然后用其各自的数据填写JTextFields,例如:客户名字应该弹出第一个名字textField而他的第二个名字会变成第二个名字TextField等。

我无法弄清楚这个问题,所以感谢任何帮助!

这只是少数几个课程中的一个,其余课程都有效。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import java.awt.SystemColor;
import java.text.ParseException;
import java.util.ArrayList;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.UIManager;
import java.awt.GridLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class editCustomers extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
ArrayList<Customers> customerList;
private MaskFormatter mask = null;


/**
 * Create the frame.
 */
public editCustomers(ArrayList<Customers> aList) {
    customerList = aList;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 700, 600);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.CENTER);
    panel.setLayout(new GridLayout(6, 0, 0, 0));

    JPanel panel_12 = new JPanel();
    panel.add(panel_12);
    panel_12.setLayout(new BorderLayout(0, 0));

    JLabel lblSelectACustomer = new JLabel("Select a Customer to Edit");
    panel_12.add(lblSelectACustomer, BorderLayout.WEST);

    JComboBox comboBox = new JComboBox();
    //comboBox.setModel(new DefaultComboBoxModel());
    for (Customers temp : customerList) {
        comboBox.addItem(temp.getfName());
        }
    panel_12.add(comboBox, BorderLayout.EAST);



    JPanel panel_3 = new JPanel();
    panel.add(panel_3);
    panel_3.setLayout(new BorderLayout(0, 0));

    JLabel lbl1 = new JLabel("Edit Customers First Name");
    lbl1.setVerticalAlignment(SwingConstants.TOP);
    Dimension d = lbl1.getPreferredSize();
    lbl1.setPreferredSize(new Dimension(d.width + 50, d.height));
    panel_3.add(lbl1, BorderLayout.WEST);

    JPanel panel_8 = new JPanel();
    panel_3.add(panel_8, BorderLayout.CENTER);

    textField = new JTextField();
    panel_8.add(textField);
    textField.setColumns(30);

    JPanel panel_4 = new JPanel();
    panel.add(panel_4);
    panel_4.setLayout(new BorderLayout(0, 0));

    JLabel lbl2 = new JLabel("Edit Customers Second Name");
    lbl2.setVerticalAlignment(SwingConstants.TOP);
    lbl2.getPreferredSize();
    lbl2.setPreferredSize(new Dimension(d.width + 50, d.height));
    panel_4.add(lbl2, BorderLayout.WEST);

    JPanel panel_9 = new JPanel();
    panel_4.add(panel_9, BorderLayout.CENTER);

    textField_1 = new JTextField();
    panel_9.add(textField_1);
    textField_1.setColumns(30);

    JPanel panel_5 = new JPanel();
    panel.add(panel_5);
    panel_5.setLayout(new BorderLayout(0, 0));

    JLabel lbl3 = new JLabel("Edit Customers Address");
    lbl3.setVerticalAlignment(SwingConstants.TOP);
    lbl3.getPreferredSize();
    lbl3.setPreferredSize(new Dimension(d.width + 50, d.height));
    panel_5.add(lbl3, BorderLayout.WEST);

    JPanel panel_10 = new JPanel();
    panel_5.add(panel_10, BorderLayout.CENTER);

    textField_2 = new JTextField();
    panel_10.add(textField_2);
    textField_2.setColumns(30);

    JPanel panel_6 = new JPanel();
    panel.add(panel_6);
    panel_6.setLayout(new BorderLayout(0, 0));

    JLabel lbl4 = new JLabel("Edit Customers Date of Birth");
    lbl4.setVerticalAlignment(SwingConstants.TOP);
    lbl4.getPreferredSize();
    lbl4.setPreferredSize(new Dimension(d.width + 50, d.height));
    panel_6.add(lbl4, BorderLayout.WEST);

    JPanel panel_11 = new JPanel();
    panel_6.add(panel_11, BorderLayout.CENTER);

    textField_3 = new JTextField();
    try {
        mask = new MaskFormatter("##/##/####");
        mask.setPlaceholderCharacter(' ');
    } catch (ParseException e) {
        e.printStackTrace();
    }       
    JFormattedTextField textField_3 = new JFormattedTextField(mask);
    textField_3.setText("dd/mm/yyyy");
    panel_11.add(textField_3);
    textField_3.setColumns(30);

    JPanel panel_2 = new JPanel();
    panel.add(panel_2);
    panel_2.setLayout(new BorderLayout(0, 0));

    JPanel panel_7 = new JPanel();
    panel_2.add(panel_7, BorderLayout.CENTER);

    JButton btnConfirm = new JButton("Confirm");
    btnConfirm.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    panel_7.add(btnConfirm);

    JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    panel_7.add(btnCancel);

    JPanel panel_1 = new JPanel();
    panel_1.setBackground(SystemColor.activeCaption);
    contentPane.add(panel_1, BorderLayout.NORTH);

    JLabel lblEditACurrent = new JLabel("Edit a Current Customer");
    lblEditACurrent.setForeground(Color.BLACK);
    lblEditACurrent.setFont(new Font("Arial", Font.PLAIN, 19));
          lblEditACurrent.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
    panel_1.add(lblEditACurrent);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                editCustomers frame = new editCustomers(new ArrayList<Customers>());
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

你可以试试这样的...... 更改第63-67行如下。将客户放入组合框而不仅仅是名称。

JComboBox<Customer> comboBox = new JComboBox<>();
//comboBox.setModel(new DefaultComboBoxModel());
for (Customers temp : customerList) {
    comboBox.addItem(temp);
}

然后为组合框实现ActionListener,如下所示。

comboBox.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Customer customer = comboBox.getSelectedItem();
        // Use the above customer object's fields to populate your text fields

    }
});