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

  • Home
  • SEARCH
  • 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 8368445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:23:29+00:00 2026-06-09T13:23:29+00:00

I am a little bit confused on exactly how to program this. I have

  • 0

I am a little bit confused on exactly how to program this. I have a class extending JFrame with two textfields. I want to be able to get input from JFrame. Somehow just wait for the input from my mainMaybe class…Any ideas?

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;


public class mainMaybe extends JFrame{
    JTextField login;
    JTextField pass;

    public mainMaybe() throws InterruptedException{
        login = new JTextField();
        pass = new JTextField();
        pass.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                 System.out.println("DO SOMETHING");
            }       
        });
        add(login,BorderLayout.NORTH);
        add(pass,BorderLayout.SOUTH);
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void returnEmail(){
        //SOME HOW WAIT FOR THE ACTION LISTENER?
    }

    public static void main(String[] args) throws InterruptedException {
        new mainMaybe().returnEmail(); 
        //CONTROL SHOULD STAY WITH mainMaybe until an email is returned         
    }

}
  • 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-06-09T13:23:30+00:00Added an answer on June 9, 2026 at 1:23 pm

    Java Swing is based on the concept of event-driven architecture. As such, events trigger actions and based on these events you would perform some kind of operation.

    Using Swing Component would be a good point to start to understand and consequently implement what you want to achieve in terms of your UI and its corresponding behavior. So I would suggest that you read a bit about it and then try to tackle what you’re trying to achieve.

    I’m not so sure of what you’re trying to achieve but if I were you, I would have two text fields, maybe some labels attached to it and a “Submit” button. And I would add an ActionListener to this button to perform some kind of action.

    Maybe an SSCCE might help you get started. So have a look at this code:

    package com.test;
    
    import javax.swing.JOptionPane;
    
    public class SimpleUI extends javax.swing.JFrame {
    
        /**
         * Creates new form SimpleUI
         */
        public SimpleUI() {
            initComponents();
        }
    
        private void initComponents() {
    
            mainPanel = new javax.swing.JPanel();
            labelName = new javax.swing.JLabel();
            textName = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            textPassword = new javax.swing.JPasswordField();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2));
    
            labelName.setText("Name: ");
    
            jLabel1.setText("Password: ");
    
            jButton1.setText("Submit");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
            mainPanel.setLayout(mainPanelLayout);
            mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(labelName)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(textName)
                        .addComponent(textPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addGap(0, 65, Short.MAX_VALUE))
            );
            mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelName)
                        .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addContainerGap(15, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            );
    
            pack();
        }
    
    
        /**
        *   This is the method that is called when an action is performed.
        *   Over here I just simply show an error message if any of the text fields are empty or just show their names.
        */
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            String name         = textName.getText();
            String password     = String.valueOf(textPassword.getPassword());
    
            if("".equals(password) || "".equals(name)){
                JOptionPane.showMessageDialog(this, "Name or password is empty!", "Incorrect Input", JOptionPane.ERROR_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this, "Hello "+name+" with password "+password, "Incorrect Input", JOptionPane.PLAIN_MESSAGE);
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
    
            try {
                javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
    
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new SimpleUI().setVisible(true);
                }
            });
        }
    
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel labelName;
        private javax.swing.JPanel mainPanel;
        private javax.swing.JTextField textName;
        private javax.swing.JPasswordField textPassword;
    
    }
    

    Note: I generated this code using NetBeans IDE, so I didn’t have to handle much about the layout or anything. If you want to learn about Layouts, here’s an interesting tutorial: “A Visual Guide to Layout Managers”

    The main idea for me was to show you how events are triggered. So what I did was I added an actionlistener to my “Submit” button and when someone clicks on the button, I perform some kind of action. Hope you get the picture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little bit confused by the PHP function declare . What exactly
I'm a little bit confused. I want to start learn Flex3 with Eclipse and
I am a little bit confused with Doctrine class. I created a class and
I am little bit confused about this statement that I read in a book
I am little bit confused about following problem & their solutions: i have 2
I'm a little bit confused with Html helpers in MVC3. I used this syntax
I'm a little bit confused about simple program which I wrote, can You please
I'm a little bit confused about these two terms, can somebody explain what is
Actually i m little bit confused in Intent. Suppose i have three activities. A,b,c
Ok, I'm a little bit confused here. I'm trying to select a DAO class

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.