简体   繁体   中英

JComboBox selected index change use to send different JTextField values to JTable using Button Click Event

I attempt to send Text Field values to Table using button click event. But using combo box I need to change Unit type as "Imperial" or "Metric".

When select "Imperial" from combo box and Click ADD Button, Table should fill using "Name", "Unit Imperial" & "Price Imperial" Text Field values.

But When select "Metric" from combo box and Click ADD Button,Table should fill using "Name", "Unit Metric" & "Price Metric" Text Field values.

I don't have clear idea to use comboBox Item change use to effect on Button click event. Thanks in advance to guiding me to solve this problem.

public class UnitTable {

    private JFrame frame;
    private JTable table;
    private JTextField txtName;
    private JTextField txtUImp;
    private JTextField txtPImp;
    private JTextField txtUMetric;
    private JTextField txtPMetric;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UnitTable window = new UnitTable();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public UnitTable() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 649, 288);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 10, 526, 181);
        frame.getContentPane().add(scrollPane);

        table = new JTable();
        Object[] columns = { "Name", "Unit", "Price" };
        DefaultTableModel model = new DefaultTableModel();
        scrollPane.setViewportView(table);
        model.setColumnIdentifiers(columns);
        table.setModel(model);

        JLabel lblName = new JLabel("Name");
        lblName.setBounds(10, 201, 96, 13);
        frame.getContentPane().add(lblName);

        txtName = new JTextField();
        txtName.setBounds(10, 224, 96, 19);
        frame.getContentPane().add(txtName);
        txtName.setColumns(10);

        JLabel lblUImp = new JLabel("Unit Imperial");
        lblUImp.setBounds(121, 201, 91, 13);
        frame.getContentPane().add(lblUImp);

        txtUImp = new JTextField();
        txtUImp.setBounds(116, 224, 96, 19);
        frame.getContentPane().add(txtUImp);
        txtUImp.setColumns(10);

        JLabel lblPImp = new JLabel("Price Imperial");
        lblPImp.setBounds(222, 201, 96, 13);
        frame.getContentPane().add(lblPImp);

        txtPImp = new JTextField();
        txtPImp.setBounds(222, 224, 96, 19);
        frame.getContentPane().add(txtPImp);
        txtPImp.setColumns(10);

        JLabel lblUMetric = new JLabel("Unit Metric");
        lblUMetric.setBounds(330, 201, 94, 13);
        frame.getContentPane().add(lblUMetric);

        txtUMetric = new JTextField();
        txtUMetric.setBounds(328, 224, 96, 19);
        frame.getContentPane().add(txtUMetric);
        txtUMetric.setColumns(10);

        JLabel lblPMetric = new JLabel("Price Metric");
        lblPMetric.setBounds(434, 201, 102, 13);
        frame.getContentPane().add(lblPMetric);

        txtPMetric = new JTextField();
        txtPMetric.setBounds(434, 224, 96, 19);
        frame.getContentPane().add(txtPMetric);
        txtPMetric.setColumns(10);

        JButton btnAdd = new JButton("ADD");
        Object[] row = new Object[3];
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                row[0] = txtName.getText();
                row[1] = txtUImp.getText();
                row[2] = txtPImp.getText();

                model.addRow(row);

            }
        });
        btnAdd.setBounds(546, 45, 85, 21);
        frame.getContentPane().add(btnAdd);

        JComboBox cmbUType = new JComboBox();
        cmbUType.setModel(new DefaultComboBoxModel(new String[] { "Imperial", "Metric" }));
        cmbUType.setBounds(546, 8, 85, 21);
        frame.getContentPane().add(cmbUType);

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

                int i = table.getSelectedRow();
                if (i >= 0) {
                    model.removeRow(i);
                } else {
                    JOptionPane.showMessageDialog(null, "Please Select Item to Delete");

                }

            }
        });
        btnDelete.setBounds(546, 76, 85, 21);
        frame.getContentPane().add(btnDelete);

    }
}

表更改

You should not be using a null layout and absolute positioning. Study Swing layout managers .

A JComboBox can hold any object. You need to tell the compiler what type of object ( String ) you're passing. You also need to tell the compiler what type of object you're storing in the DefaultComboBoxModel .

Having said that, I made cmbUType a class variable so I could reference it in the Add button action listener.

Here's your modified code.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class UnitTable {

    private JFrame frame;
    private JTable table;
    private JTextField txtName;
    private JTextField txtUImp;
    private JTextField txtPImp;
    private JTextField txtUMetric;
    private JTextField txtPMetric;
    private JComboBox<String> cmbUType;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UnitTable window = new UnitTable();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public UnitTable() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 649, 288);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 10, 526, 181);
        frame.getContentPane().add(scrollPane);

        table = new JTable();
        Object[] columns = { "Name", "Unit", "Price" };
        DefaultTableModel model = new DefaultTableModel();
        scrollPane.setViewportView(table);
        model.setColumnIdentifiers(columns);
        table.setModel(model);

        JLabel lblName = new JLabel("Name");
        lblName.setBounds(10, 201, 96, 13);
        frame.getContentPane().add(lblName);

        txtName = new JTextField();
        txtName.setBounds(10, 224, 96, 19);
        frame.getContentPane().add(txtName);
        txtName.setColumns(10);

        JLabel lblUImp = new JLabel("Unit Imperial");
        lblUImp.setBounds(121, 201, 91, 13);
        frame.getContentPane().add(lblUImp);

        txtUImp = new JTextField();
        txtUImp.setBounds(116, 224, 96, 19);
        frame.getContentPane().add(txtUImp);
        txtUImp.setColumns(10);

        JLabel lblPImp = new JLabel("Price Imperial");
        lblPImp.setBounds(222, 201, 96, 13);
        frame.getContentPane().add(lblPImp);

        txtPImp = new JTextField();
        txtPImp.setBounds(222, 224, 96, 19);
        frame.getContentPane().add(txtPImp);
        txtPImp.setColumns(10);

        JLabel lblUMetric = new JLabel("Unit Metric");
        lblUMetric.setBounds(330, 201, 94, 13);
        frame.getContentPane().add(lblUMetric);

        txtUMetric = new JTextField();
        txtUMetric.setBounds(328, 224, 96, 19);
        frame.getContentPane().add(txtUMetric);
        txtUMetric.setColumns(10);

        JLabel lblPMetric = new JLabel("Price Metric");
        lblPMetric.setBounds(434, 201, 102, 13);
        frame.getContentPane().add(lblPMetric);

        txtPMetric = new JTextField();
        txtPMetric.setBounds(434, 224, 96, 19);
        frame.getContentPane().add(txtPMetric);
        txtPMetric.setColumns(10);

        JButton btnAdd = new JButton("ADD");
        Object[] row = new Object[3];
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String type = (String) cmbUType.getSelectedItem();
                if (type.equals("Imperial")) {
                    row[0] = txtName.getText();
                    row[1] = txtUImp.getText();
                    row[2] = txtPImp.getText();
                } else {
                    row[0] = txtName.getText();
                    row[1] = txtUMetric.getText();
                    row[2] = txtPMetric.getText();
                }

                model.addRow(row);

            }
        });
        btnAdd.setBounds(546, 45, 85, 21);
        frame.getContentPane().add(btnAdd);

        cmbUType = new JComboBox<>();
        cmbUType.setModel(new DefaultComboBoxModel<String>
            (new String[] { "Imperial", "Metric" }));
        cmbUType.setBounds(546, 8, 85, 21);
        frame.getContentPane().add(cmbUType);

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

                int i = table.getSelectedRow();
                if (i >= 0) {
                    model.removeRow(i);
                } else {
                    JOptionPane.showMessageDialog(null, "Please Select Item to Delete");

                }

            }
        });
        btnDelete.setBounds(546, 76, 85, 21);
        frame.getContentPane().add(btnDelete);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM