Search code examples
javaswingjlabelflowlayout

How do I get my GUI to display my JLabels in line with JTextFields?


This is what the passwordVault GUI currently looks like

I want to have three rows containing a JLabel and JTextField on each row and then the three buttons at the bottom. My current code cannot accomplish this but does anyone know how I could?

I have tried putting the order that I add each element to the JFrame but so far, nothing works. Please help! This is for my IB Computer Science IA.

import javax.swing.*;
import javax.swing.table.*;

import java.awt.FlowLayout;
import java.awt.event.*;

public class PasswordVault implements ActionListener
{
    static String masterPassword, enteredPassword;
    JFrame masterPasswordFrame, passwordVault;
    JPasswordField masterPasswordField;
    JTable passwordTable;
    Object[] columnNames = {"Name of Application", "Application Password", "Description"};
    Object[] rowData = new Object[3];
    JTextField appName, appPass, appDesc;
    JButton add, delete, update;
    JLabel nameOfApp, passOfApp, descOfApp;

    public static void  main(String[] args) 
    {   
        String name = JOptionPane.showInputDialog("What is your name?");
        masterPassword = JOptionPane.showInputDialog("Hello, " + name + ". " + "What would you like your master password to be?");
        //The master password will be used to access the Password Vault
        new PasswordVault();
    }

    public PasswordVault()
    {
        //This is the frame that will ask the user to input their password
        masterPasswordFrame = new JFrame("Master Password");
        masterPasswordFrame.setSize(400,100);
        masterPasswordFrame.setLocationRelativeTo(null);
        masterPasswordFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel masterPasswordPanel = new JPanel();

        //Accepts the master password from the user; when the user types the password, rather than words it will show asterisks
        masterPasswordField = new JPasswordField(10);
        masterPasswordField.setEchoChar('*');
        JLabel passwordLabel = new JLabel("Enter Password: ");
        JButton okayButton = new JButton("Check");
        okayButton.addActionListener(this);

        masterPasswordPanel.add(passwordLabel);
        masterPasswordPanel.add(masterPasswordField);
        masterPasswordPanel.add(okayButton);

        masterPasswordFrame.add(masterPasswordPanel);
        masterPasswordFrame.setVisible(true);
        masterPasswordFrame.pack();

        enteredPassword = new String(masterPasswordField.getPassword());    

        passwordVault = new JFrame("Password Vault");
        passwordTable = new JTable();
        JPanel passwordPanel = new JPanel();

        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.setColumnIdentifiers(columnNames);
        passwordTable.setModel(tableModel);

        nameOfApp = new JLabel("App Name: ");
        passOfApp = new JLabel("App password: ");
        descOfApp = new JLabel("Description: ");    

        appName = new JTextField();
        appPass = new JTextField();
        appDesc = new JTextField();

        add = new JButton("Add");
        delete = new JButton("Delete");
        update = new JButton("Update");  

        appName.setBounds(400, 220, 100, 25);
        appPass.setBounds(400, 250, 100, 25);
        appDesc.setBounds(400, 280, 100, 25);

        add.setBounds(530, 220, 100, 25);
        update.setBounds(530, 250, 100, 25);
        delete.setBounds(530, 280, 100, 25);

        FlowLayout flowLayout = new FlowLayout();

        JScrollPane scrollPane = new JScrollPane(passwordTable);
        scrollPane.setBounds(0, 0, 1000, 200);
        passwordVault.setLayout(flowLayout);
        passwordVault.add(scrollPane);

        //These labels don't appear in the vault
        passwordPanel.add(nameOfApp);
        passwordPanel.add(passOfApp);
        passwordPanel.add(descOfApp);
        passwordVault.add(passwordPanel);

        passwordVault.add(appName);
        passwordVault.add(appPass);
        passwordVault.add(appDesc);

        passwordVault.add(add);
        passwordVault.add(update);
        passwordVault.add(delete);

        add.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                rowData[0] = appName.getText();
                rowData[1] = appPass.getText();
                rowData[2] = appDesc.getText();

                tableModel.addRow(rowData);

                appName.setText("");
                appPass.setText("");
                appDesc.setText("");
            }
        });

        update.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                int i = passwordTable.getSelectedRow();
                if(i >= 0) 
                {
                   tableModel.setValueAt(appName.getText(), i, 0);
                   tableModel.setValueAt(appPass.getText(), i, 1);
                   tableModel.setValueAt(appDesc.getText(), i, 2);

                   appName.setText("");
                   appPass.setText("");
                   appDesc.setText("");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Update Error");
                }
            }
        });

        delete.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                int i = passwordTable.getSelectedRow();
                if(i >= 0)
                {
                    tableModel.removeRow(i);

                    appName.setText("");
                    appPass.setText("");
                    appDesc.setText("");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Delete Error");
                }
            }
        });

        passwordTable.addMouseListener(new MouseAdapter()
        {
        public void mouseClicked(MouseEvent e)
        { 
            int i = passwordTable.getSelectedRow();

            appName.setText(tableModel.getValueAt(i, 0).toString());
            appPass.setText(tableModel.getValueAt(i, 1).toString());
            appDesc.setText(tableModel.getValueAt(i, 2).toString());
        }
        });

        passwordVault.setSize(1000,500);
        passwordVault.setLocationRelativeTo(null);
        passwordVault.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent event)
    {
        if(new String(masterPasswordField.getPassword()).equals(masterPassword))
        {
            masterPasswordFrame.setVisible(false);
            passwordVault.setVisible(true);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Password Incorrect. Please Try Again.");
        }
    }
}

Solution

  • You can achieve this components layout by following below example. Here I have used layout manager GridBagLayout for the main panel. For buttonPanel I have not set a layout manager. So, the default layout manager FlowLayout is used there.

    Notice how I have used GridBagConstraints objects with different values for each component.

    import javax.swing.*;
    import java.awt.*;
    
    public class LayoutComponents
    {
      public static void main(String[] args)
      {
        JLabel nameOfApp = new JLabel("App Name: ");
        JLabel passOfApp = new JLabel("App password: ");
        JLabel descOfApp = new JLabel("Description: ");
    
        JTextField appName = new JTextField();
        JTextField appPass = new JTextField();
        JTextField appDesc = new JTextField();
    
        JButton add = new JButton("Add");
        JButton delete = new JButton("Delete");
        JButton update = new JButton("Update");
    
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(add);
        buttonPanel.add(delete);
        buttonPanel.add(update);
    
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(new GridBagLayout());
    
        f.getContentPane().add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        f.getContentPane().add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        f.getContentPane().add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        f.getContentPane().add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        f.getContentPane().add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        f.getContentPane().add(appDesc, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        f.getContentPane().add(buttonPanel, new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    
        f.setBounds(300, 200, 400, 300);
        f.setVisible(true);
      }
    }