sharpNd
sharpNd

Reputation: 3

Pass user input (from TextField) to another class?

I have the following problem: I have 2 classes in my game - CONFIGUREGAME (CG) and ROULETTETABLE (RT) - and the user is able to specify details about the game like his name or his money in the class CG. In the class RT I want the input from a JTextField from the class CG to be shown on a button in class RT.

Here's my code (I simplified it alot):

public class CONFIGUREGAME extends JFrame implements ActionListener
{
    Jframe frame = new JFrame("...");
    public JTextField playername1 = new JTextField();
    public JButton startgame = new JButton();

    public CONFIGUREGAME()
    {
        startgame.addActionListener(this);
    }

    public static void main(String (String[] args) 
    {
       new CONFIGUREGAME();
    }

    public void actionPerformed(ActionEvent aEvt) 
    {
        if(aEvt.getSource()==startgame)
        {
            frame.dispose();
            new ROULETTETABLE();
        }
    }

now Class 2:

import ...;

public class ROULETTETABLE extends CONFIGUREGAME implements ActionListener
{
     public player1 = new JButton();

     public ROULETTETABLE()
     {
         String Strplayername1 = playername1.getText();
         player1.setText(Strplayername1);
     }

     public static void main(String (String[] args) 
     {
         new ROULETTETABLE();
     }
}

I tried various ways that were supposed to help but they didn't. My UI is working totally fine so if there's a mistake in it it's because I made a mistake simplifying it. I appreciate any from of help!

Upvotes: 0

Views: 1161

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

Option #1

Pass the result CONFIGUREGAME to ROULETTETABLE

public class CONFIGUREGAME extends JFrame implements ActionListener {

    //Jframe frame = new JFrame("..."); WHY?

    //...

    public void actionPerformed(ActionEvent aEvt) {
        if (aEvt.getSource() == startgame) {
            frame.dispose();
            new ROULETTETABLE(playername1.getText());
        }
    }


public class ROULETTETABLE extends JFrame /* CONFIGUREGAME why? */implements ActionListener
{
     public JButton player1 = new JButton();

     public ROULETTETABLE(String playerName)
     {
         player1.setText(playerName);
     }
}

I'm not a fan of this because it couples of the CONFIGUREGAME class to the ROULETTETABLE

A Better Option...

Use a JDialog to collect the configuration information and then pass it to the ROULETTETABLE class

First, some reconfiguration of the classes. As a general rule, avoid extending from top level classes like JFrame, they couple the code to single access point and reduce it's flexibility and re-use

public class Roulettetable extends JPanel implements ActionListener {

    private JButton player1 = new JButton();

    public Roulettetable(String name) {
        player1.setText(name);
    }
}

public class ConfigureGame extends JPanel {

    private JTextField playername1 = new JTextField();

    public ConfigureGame() {
    }

    public String getPlayerName() {
        return playername1.getText();
    }
}

Then you wrap it altogether...

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        ConfigureGame configureGame = new ConfigureGame();
        JOptionPane.showOptionDialog(null, configureGame, "Configure Game", JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[] {"Start"}, 0);
        String name = configureGame.getPlayerName();

        Roulettetable roulettetable = new Roulettetable(name);

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(roulettetable);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});

This example is pretty simple, it simply makes use of JOptionPane to display the dialog.

Have a look at How to Make Dialogs for more details

Upvotes: 0

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

You need something like this.

public class CONFIGUREGAME extends JFrame implements ActionListener
{
    Jframe frame = new JFrame("...");
    public JTextField playername1 = new JTextField();
    public JButton startgame = new JButton();

    public CONFIGUREGAME()
    {
        startgame.addActionListener(this);
    }

    public static void main(String (String[] args) 
    {
       new CONFIGUREGAME();
    }

    public void actionPerformed(ActionEvent aEvt) 
    {
        if(aEvt.getSource()==startgame)
        {
            frame.dispose();
            new ROULETTETABLE(playername1.getText());
        }
    }
}

public class ROULETTETABLE extends CONFIGUREGAME implements ActionListener
{
     public JButton player1 = new JButton();

     public ROULETTETABLE(String playerName)
     {
         player1.setText(playerName);
     }

     public static void main(String (String[] args) 
     {
         new ROULETTETABLE();
     }
}

P.S. Please learn the Java method and class notation. CapitalizedClassName, firstWordLowercaseMethodName, firstWordLowercaseVariableName, UPPER_CASE_CONSTANT_NAME

Upvotes: 1

Related Questions