Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get user input from a JTextField?
    primarykey
    data
    text
    <p>I am attempting a <em>very</em> simple form designed to take user input into a JTextField and show that same input via a pop up dialog.</p> <p>I can hardcode the JTextField to have a preset number using setText(). If I do this, my program works flawlessly.</p> <p>However, when I leave the field blank and try getText() to show the text in the pop up dialog, I either get an empty pop up frame, or I get an 'empty string' exception (I am attempting to parse String to Double.)</p> <p><pre></p> <code>package buttontest; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ComponentEvent; import javax.swing.*; import java.awt.event.ActionEvent; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ButtonFrame extends JFrame { @SuppressWarnings("LeakingThisInConstructor") public ButtonFrame() { setTitle("SunStream Loan Calculator v2.0"); setSize(900,900); ButtonPanel panel = new ButtonPanel(); panel.add(new JLabel("Enter your loan amount:")); loanAmt = new JTextField(40); panel.add(loanAmt); add(panel,BorderLayout.CENTER); } public JTextField loanAmt; class ButtonPanel extends JPanel implements ActionListener { private Component frame; public ButtonPanel() { final JButton b2 = new JButton("Calculate"); add(b2, BorderLayout.SOUTH); b2.setActionCommand("calculate"); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ButtonFrame bf = new ButtonFrame(); if("calculate".equals(e.getActionCommand())) { JOptionPane.showMessageDialog(frame, bf.loanAmt.getText()); } } }); } @Override public void actionPerformed(ActionEvent ae) { throw new UnsupportedOperationException("Not supported yet."); } } } </code></pre> <p></p> <p>Any help would be greatly appreciated. I am researching using a KeyListener or KeyEvent but I don't quite understand it well enough.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload