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 8278967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:10:55+00:00 2026-06-08T09:10:55+00:00

This is probably a rather stupid problem but I am playing around with gridbag

  • 0

This is probably a rather stupid problem but I am playing around with gridbag layouts and I am trying to build a basic login screen that sends the users information to another class that checks with a mysql DB. but before I do that I have to find a way to listen for my login button being pressed and then get the text from the text fields (this last part is where I am running into problems). I have tried declaring the JTextField outside of the addComponentsToPane method but to no avail.

here is my code so far

also I apologies to any neatness I am new to this site and am not yet knowledgeable enought to format my code correctly.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


/**
 *
 * @author TeslaSolari
 */
public class login {

    public void login() {
        createAndShowGUI();
    }

    private static void createAndShowGUI() {
        //Create and setup the windows
        JFrame frame = new JFrame("Login");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Setup the content pane
        addComponentsToPane(frame.getContentPane());

        //Display the window
        frame.pack();
        frame.setVisible(true);
    }

    public static void addComponentsToPane(Container pane) {
        pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        //parts
        JButton login;
        JTextField user;
        JLabel[] label = new JLabel[10];
        JPasswordField pass;
        ActionListener loginB = null;

        pane.setLayout(new GridBagLayout());
        //Cpnstraints
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.CENTER;

        // Buttons
        login = new JButton("Login");
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        pane.add(login, c);
        login.addActionListener(new Action());

        // textfields / labels
        label[0] = new JLabel("Username");
        user = new JTextField();
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(label[0], c);
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(user, c);

        label[1] = new JLabel("Password");
        pass = new JPasswordField();
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(label[1], c);
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 3;
        pane.add(pass, c);


    }

    //Actions

    static class Action implements ActionListener{
        public void actionPerformed (ActionEvent e){
            System.out.println(label[]);
        }
    }

}
  • 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-08T09:10:58+00:00Added an answer on June 8, 2026 at 9:10 am

    It seems your solution is right (moving the declarations JTextField declarations outside the method), but you might have missed declaring them as static (according to the rest of your class)

    private static JTextField user;
    private static JPasswordField pass;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably a stupid question but I'm still trying to wrap my head around nHibernate.
I know this is probably rather trivial but I have had a look at
I know the fix to this is probably rather easy, but how would I
I know this could probably be done without regular expressions, but I'd rather make
I am rather inexperienced C++ programmer, so this question is probably rather basic. I
My problem is probably rather simple, but I'm not at that skill level yet.
This probably sounds like a terrible idea at first glance, but here is my
This probably is a dummy question but I cannot find a clear indication. I
I know this probably really simple but Im not sure what im doing wrong...
I know this probably has been asked before but I am having issues with

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.