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[]);
}
}
}