user6483434
user6483434

Reputation:

trying to get JTextField input onto a JLabel then to JPanel GUI applet

I'm making a "check writer" applet for homework and I'm trying to get the user's name to show up anywhere on the UI. I've tried a couple things but now I'm just trying to get the input onto a JLabel then onto a Panel.

public class project2 extends JPanel {

private JLabel labelName, labelAmount, resultName, resultMoney;
//private JTextArea resultName, resultMoney;
private JTextField name, dollarAmount;
private final int w = 1000, h = 200;

protected void paintComponent(Graphics g) {
g.drawString("Bank of Northwest Vista College", 425, 40);
g.drawString("Pay to the order of: ", 75, 70);
g.drawString("$", 730, 70);
}
public project2() {
    setBackground(Color.white);
    setPreferredSize(new Dimension(w, h));

    labelName = new JLabel("Name:");
    labelAmount = new JLabel("Check Amount:");
    resultName = new JLabel("");
    //resultMoney = new JLabel("$" + dollarAmount);

    name = new JTextField(20);
    name.addActionListener(new nameListener());

    dollarAmount = new JTextField(20);
    dollarAmount.addActionListener(new dollarListener());

    add(labelName);
    add(name);
    add(labelAmount);
    add(dollarAmount);
    add(resultName);
    //add(resultMoney);
}

where i basically need the most help

private class nameListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        JLabel namelabel = new JLabel(name.getText());
        JPanel namepanel = new JPanel();
        namepanel.setPreferredSize(new Dimension(40, 10));
        namepanel.add(name);
    }
}

private class dollarListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        String MONEY = dollarAmount.getText();
    }
}

}

Upvotes: 0

Views: 77

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Problems:

  • You seem to be doing a combination of writing directly to the component within its paintComponent method, as well as trying to display text with a JLabel, and to combine these methodologies. That's not the cleanest way to solve this. Usually it's better to use all components or all graphics rather than mixing if you're trying to display a unified message.
  • You're trying to place a JLabel on a JPanel using absolute positioning, ignoring the JPanel's layout manager. JPanels use FlowLayout as their default layout manager, and this layout will ignore component sizes and bounds, as will most Swing layout managers.
  • You're overriding a JPanel's paintComponent, but not calling the super's method.
  • In the ActionListener, you're creating new components, adding the new JLabel to the new JPanel, but don't add the JPanel to the GUI. Regardless, you don't really want to be creating new components here, as explained down below.

Suggestions:

  • Again either display your text either in Swing components such as JLabels, or from within the paintComponent method, but not both together, not unless you're displaying two completely different things that aren't supposed to line up.
  • If overriding paintComponent, call the super.paintComponent(g); method, usually as the first call within your override method. This will ensure that dirty pixels are cleaned up and that other component graphics housekeeping is done.
  • Avoid using null layouts and setBounds but instead use layout managers and let the layout managers do their thing to help you easily compose your GUI.
  • Don't add a new JLabel from within the ActionListener but rather add text to an already existing and displayed JLabel.

For example:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class SimpleExampleWithComponents extends JPanel {
    private static final int COLS = 15;
    private JTextField nameField = new JTextField(COLS);
    private JTextField amountField = new JTextField(COLS);
    private JButton submitButton = new JButton("Submit");

    private JLabel payeeLabel = new JLabel("           ");
    private JLabel amountLabel = new JLabel("$         ");

    public SimpleExampleWithComponents() {
        SubmitListener submitListener = new SubmitListener();
        nameField.addActionListener(submitListener);
        amountField.addActionListener(submitListener);
        submitButton.addActionListener(submitListener);
        submitButton.setMnemonic(KeyEvent.VK_S);

        JPanel topPanel  = new JPanel();
        topPanel.add(new JLabel("Name:"));
        topPanel.add(nameField);
        topPanel.add(Box.createHorizontalStrut(15));
        topPanel.add(new JLabel("Amount:"));
        topPanel.add(amountField);
        topPanel.add(Box.createHorizontalStrut(15));
        topPanel.add(submitButton);

        // holds one row and 3 columns
        JPanel middlePanel = new JPanel(new GridLayout(1, 3));
        middlePanel.add(new JLabel("Pay to the order of:"));
        middlePanel.add(payeeLabel);
        middlePanel.add(Box.createHorizontalStrut(15));
        middlePanel.add(amountLabel);

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(middlePanel, BorderLayout.CENTER);
    }

    private class SubmitListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String name = nameField.getText().trim();
            String amount = amountField.getText().trim();

            payeeLabel.setText(name);
            amountLabel.setText("$" + amount);
        }
    }

    private static void createAndShowGui() {
        SimpleExampleWithComponents mainPanel = new SimpleExampleWithComponents();

        JFrame frame = new JFrame("GUI");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Upvotes: 1

Related Questions