If i try and add the jtextfield to another class after hitting my submit button it wont work because the other class trys to take the array list before it is updated
import java.util.*;
import java.io.*;
import javax.swing.SwingUtilities;
public class BlackjackMain {
public static void main(String [] args) {
BlackjackGUIClass content = new BlackjackGUIClass();
ArrayList<String> hi = new ArrayList<String>();
content.outputGUI();
hi = content.newacc;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class BlackjackGUIClass implements ActionListener {
GridBagLayout ez;
GridBagConstraints gg;
private JFrame gui;
private JPanel content;
private JLabel title;
private JTextField usern;
private JLabel userent;
private JButton login;
private JTextField passw;
private JLabel passent;
private JButton create;
ArrayList<String> logacc = new ArrayList<String>();
ArrayList<String> newacc = new ArrayList<String>();
int check = 0;
public BlackjackGUIClass() {
}
public void outputGUI () {
gui = new JFrame("Blackjack Menu");
content = new JPanel();
gui.add(content);
ez = new GridBagLayout();
gg = new GridBagConstraints();
content.setLayout(ez);
title = new JLabel ("Abel's Blackjack Casino");
usern = new JTextField(30);
usern.setPreferredSize(new Dimension(10, 60));
userent = new JLabel ("Enter Username: ");
login = new JButton("Login");
login.setPreferredSize(new Dimension(300, 50));
create = new JButton("Create Account");
create.setPreferredSize(new Dimension(300, 50));
passw = new JTextField(30);
passw.setPreferredSize(new Dimension(10, 60));
passent = new JLabel ("Enter Password: ");
login.addActionListener(this);
create.addActionListener(this);
passw.addActionListener(this);
usern.addActionListener(this);
Font createSize = new Font("Times New Roman", Font.PLAIN, 50);
Font second = new Font("", Font.PLAIN, 30);
title.setFont(createSize);
userent.setFont(second);
passent.setFont(second);
gg.insets = new Insets(5, 5, 5, 5);
gg.gridx = 1;
gg.gridwidth = 3;
gg.gridy = 0;
content.add(title, gg);
gg.gridx = 2;
gg.gridwidth = 1;
gg.gridy = 1;
content.add(usern, gg);
gg.gridx = 1;
content.add(userent, gg);
gg.gridy = 2;
gg.gridx = 1;
content.add(passent, gg);
gg.gridx = 2;
content.add(passw, gg);
gg.gridy = 3;
gg.gridx = 1;
gg.gridwidth = 1;
content.add(login, gg);
gg.gridy = 3;
gg.gridx = 2;
gg.gridwidth = 1;
content.add(create, gg);
gui.setExtendedState(JFrame.MAXIMIZED_BOTH);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
BlackjackMain wow = new BlackjackMain();
if (e.getSource() == login) {
logacc.add(usern.getText());
logacc.add(passw.getText());
} else if (e.getSource() == create) {
newacc.add(usern.getText());
newacc.add(passw.getText());
}
gui.dispose();
}
}
i tried to set an array list in my main class equal to the array list in my gui class that is being updated but it tries to update before the input from the jtextfield is added
Like most GUI's, Swing is an Event Driven environment, that is, something happens and your respond to it (look at the
ActionListener, you don't know when it might be actioned, only that it may be actioned at some point in the future).When you call
setVisibleon aJFrame, the call will return immediately and at some point in the future, the window will be made visible.This means that in your code, you're trying to get the details before the users even had a chance to input anything.
Instead, make use of a modal
JDialog(see How to use dialogs for more details) and/or an observer pattern to determine when you should take action on what the user has inputted.I'd also discourage you from using
setPreferredSize, this will have an adverse effect on different platforms.