Search code examples
javaswingjoptionpane

Setting the text of JTextFields in a JOptionPane


I have a JTable which stores data about dishes. When the user tries to add a new dish, he must enter values in four fields. Although a JTable is editable by default I want to make my own implementation for editing a row. I have a method that generates a custom dialog box and an array list that stores the references to the text fields. My goal is to set the text of all text fields to the corresponding ones in the row and then display the dialog box. This is what I've tried so far.

        edit.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                if (dishes.getSelectionModel().isSelectionEmpty())
                {
                    JOptionPane.showMessageDialog(null, "You need to select a dish in order to edit it",
                            "No element selected", JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    String[] labels = {"Name:", "Description:", "Price:", "Restock Level:"};

                    int fields = 4
;
                    JOptionPane optionPane = new JOptionPane();

                    optionPane.setVisible(false);

                    optionPane.showConfirmDialog(null, createInputDialog(labels,fields),
                        "New Dish", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

                    for(int i = 0; i < textFields.size(); i++)
                    {
                        textFields.get(i).setText(dishes.getValueAt(dishes.getSelectedRow(), i).toString());
                    } 

                    optionPane.setVisible(true);
                }
            }
        });

And here is the code that creates the panel used in the dialog box

//Creates an input dialog with the specified number of fields and text for the labels
    public JPanel createInputDialog(String[] labels, int numFields)
    {
        JPanel input = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5,5,5,5);

        textFields = new ArrayList<JTextField>();

        for(int i = 0; i < numFields; i++)
        {
            gbc.gridx = 2;
            gbc.gridy = i;
            gbc.anchor = GridBagConstraints.WEST;

            JLabel label = new JLabel(labels[i]);
            label.setFont(font);

            input.add(label, gbc);

            gbc.gridx = 4;

            JTextField field = new JTextField(10);
            field.setFont(font);

            input.add(field, gbc);
            textFields.add(field);
        }

        error = new JLabel("");
        error.setForeground(Color.RED);

        gbc.gridx = 2;
        gbc.gridy = numFields + 1;
        gbc.gridwidth = 2;

        input.add(error, gbc);

        return input;
    }

Solution

  • The basic idea would be something along the lines of

    • Use JTable#getSelectedRow to get the selected row index (and be -1 for no selection).
    • Use JTable#getValueAt to get the column values.
    • Pass these values to the createInputDialog method so that it populate the text fields