Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6731927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:35:15+00:00 2026-05-26T10:35:15+00:00

I am attempting a very simple form designed to take user input into a

  • 0

I am attempting a very simple form designed to take user input into a JTextField and show that same input via a pop up dialog.

I can hardcode the JTextField to have a preset number using setText(). If I do this, my program works flawlessly.

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.)

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."); } } }

Any help would be greatly appreciated. I am researching using a KeyListener or KeyEvent but I don’t quite understand it well enough.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:35:16+00:00Added an answer on May 26, 2026 at 10:35 am

    You’re creating a “shadow” ButtonFrame variable inside of the b2’s ActionListener. Yes the bf variable refers to a ButtonFrame object which is of the same class as the displayed ButtonFrame object, but it refers to a completely distinct and non-visualized object. The key to a solution is to get the text from the ButtonFrame object that is actually displayed, and this can be obtained from within an inner class via the ButtonFrame.this construct:

         b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               //!! ButtonFrame bf = new ButtonFrame();
               if ("calculate".equals(e.getActionCommand())) {
    
                  //!! note use of ButtonFrame.this:
                  JOptionPane.showMessageDialog(frame, ButtonFrame.this.loanAmt.getText());
               }
            }
    
         });
    

    Next consider using public getters rather than accessing fields such as the JTextField directly. This reduces the chances of the code causing side effects, such as changing the properties of the JTextField object inadvertently.

    For instance (changes denoted by //!! comment):

    import java.awt.*;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    
    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 {
    
       private JTextField loanAmt; // !! Make field private
    
       @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);
       }
    
       // !! create a public method to get JTextField's text
       // !! without exposing the JTextField itself.
       public String getLoanAmtText() {
          return loanAmt.getText();
       }
    
       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())) {
    
                      //!! call public method on ButtonFrame object
                      JOptionPane.showMessageDialog(frame,
                            ButtonFrame.this.getLoanAmtText());
                   }
                }
    
             });
    
          }
    
          @Override
          public void actionPerformed(ActionEvent ae) {
             throw new UnsupportedOperationException("Not supported yet.");
          }
    
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to build a very simple wxPython GUI that monitors and displays external
I am attempting to write a very simple rake task (and merge it into
I am attempting to construct a very simple proof of concept that I can
I am attempting to implement a very simple Trie in Java that supports 3
I understand that doing what I'm attempting is very bad programming form (relying on
I'm attempting to make a very simple 2-person chatroom for my Django site. I'm
I have an editor I am attempting to emulate that has some very nice
I am attempting to create a very simple web interface for changing some system
I am attempting to use the Browser control in a very simple WPF application,
I'm attempting to solve what I thought would be a very simple problem. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.