Search code examples
netbeansvisibilityjlabeljtextfield

Converting jLabel to a jTextField


I'm working on a JFrame interface to interact easily with a large amount of data. The data is saved in a .txt file, and when a name is selected from the JList, my program reads the appropriate lines of data, converts them into an Object I've defined (CounterParty), and displays the appropriate fields of the objects in JLabels. This all works well. I've also written code to launch a new JPanel that edits the selected Object. The JPanel opens, already populated with the data, and when a button is clicked the existing information on the .txt file is deleted and replaced with the new, edited data. This also works well.

However, I would like to make this a bit more user-friendly. I want the JLabels where the information is initially displayed to convert into JTextFields populated with the data from the jLabels when the Edit button is clicked. This would remove needing to launch the new JPanel window altogether. I assume is would change the visibility to false of the JLabels and create new JTextField objects. I'm having trouble with this.
Can JLabel objects be converted to JTextFields? Can I maybe have both objects in the exact same spot, but alternate visibility? I'm not sure how to go about this.

I'm using NetBeans.

Thank you for your help! Let me know if any additional information is needed.


Solution

  • I created a small example which I think demonstrates what you want. It uses a button which when pressed will either remove the JTextField and add the JLabel and vice versa and then it will call revalidate() and repaint() to show changes to the frame after each button click:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class JLabelToJTextField extends JFrame {
    
        JLabel jLabel;
        JTextField jTextField;
        JButton jButton;
        JPanel mainPanel;
    
        public JLabelToJTextField() {
            jLabel = new JLabel("Name");
            jTextField = new JTextField(15);
            jButton = new JButton("Edit");
            mainPanel = new JPanel(new BorderLayout());
    
            createUI();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JLabelToJTextField().setVisible(true);
                }
            });
        }
    
        private void createUI() {
            setTitle("JLabel to JtextField");
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addComponentsToPanel();
            setLocationRelativeTo(null);
            pack();
    
        }
    
        private void addComponentsToPanel() {
            mainPanel.add(jLabel, BorderLayout.CENTER);
            mainPanel.add(jButton, BorderLayout.SOUTH);
            addActionListeners();
            getContentPane().add(mainPanel);
        }
    
        private void addActionListeners() {
            jButton.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    switch (jButton.getText()) {
                        case "Edit":
                            mainPanel.remove(jLabel);//remove component
                            mainPanel.add(jTextField, BorderLayout.CENTER);//add new component
                            jButton.setText("Done");
                            //refresh JFrame
                            revalidate();
                            repaint();
    
                            break;
                        case "Done":
                            mainPanel.remove(jTextField);//remove component
                            mainPanel.add(jLabel, BorderLayout.CENTER);//add new component
                            jButton.setText("Edit");//set button text to original
                            //refresh JFrame
                            revalidate();
                            repaint();
                            break;
                    }
                }
            });
        }
    }