I have a class variable, that I want to set in a separate class. I am instantiating an instance of this class in my second class, and referencing and setting the variable. If I use a message box to show the value of the variable in the EventHandler the value is set. However, when I try to access the value from my 'main' class Data the value is empty.
How should I re-write the below code so the value of playerSelected remains set?
import javax.swing.*; import java.awt.event.*; import javax.swing.border.LineBorder; import java.awt.*; public class Data { JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); JPanel secondPanel = new JPanel(); String playerSelected; String playerimage; String cpuimage; String BattlesWon = "0"; String BattlesLost = "0"; String Gold = "0"; String PlayerLevel = "1"; public static void main(String args[]) { new Data().start(); } public void start() { JLabel myLabel = new JLabel("Enter Player Name:"); JPanel pnlHeaderInfo = new JPanel(); JPanel pnlCharacters = new JPanel(); JPanel pnlNextButton = new JPanel(); JTextField myText = new JTextField(20); myLabel.setAlignmentX(JTextField.CENTER_ALIGNMENT); pnlHeaderInfo.setPreferredSize(new Dimension(300, 60)); pnlHeaderInfo.setLayout(new FlowLayout()); pnlHeaderInfo.add(myLabel); pnlHeaderInfo.add(myText); pnlHeaderInfo.setBorder(new LineBorder(Color.RED, 3)); mainPanel.setPreferredSize(new Dimension(600, 400)); mainPanel.add(pnlHeaderInfo); JLabel lblLeftChar1 = new JLabel(); JLabel lblCenterChar2 = new JLabel(); JLabel lblRightChar3 = new JLabel(); MyMouseHandler myMouseHandler = new MyMouseHandler(); lblLeftChar1.setMinimumSize(new Dimension(141,244)); lblLeftChar1.setMaximumSize(new Dimension(141,244)); lblLeftChar1.setName("Link"); lblLeftChar1.setIcon(new ImageIcon(getClass().getResource("/resources/__Link.jpg"))); lblLeftChar1.addMouseListener(myMouseHandler); lblCenterChar2.setMinimumSize(new Dimension(141,244)); lblCenterChar2.setMaximumSize(new Dimension(141,244)); lblCenterChar2.setName("Mario"); lblCenterChar2.setIcon(new ImageIcon(getClass().getResource("/resources/__Mario.png"))); lblCenterChar2.addMouseListener(myMouseHandler); lblRightChar3.setMinimumSize(new Dimension(141,244)); lblRightChar3.setMaximumSize(new Dimension(141,244)); lblRightChar3.setName("Pikachu"); lblRightChar3.setIcon(new ImageIcon(getClass().getResource("/resources/__Pikachu.png"))); lblRightChar3.addMouseListener(myMouseHandler); pnlCharacters.setPreferredSize(new Dimension(496, 256)); pnlCharacters.setLayout(new BorderLayout()); pnlCharacters.add(lblLeftChar1, BorderLayout.LINE_START); lblCenterChar2.setHorizontalAlignment(JLabel.CENTER); pnlCharacters.add(lblCenterChar2, BorderLayout.CENTER); pnlCharacters.add(lblRightChar3, BorderLayout.LINE_END); pnlCharacters.setBorder((new LineBorder(Color.BLUE, 3))); mainPanel.add(pnlCharacters); //JOptionPane.showMessageDialog(null, playerSelected, "TITLE", JOptionPane.WARNING_MESSAGE); JButton btnNext = new JButton("Start Game"); btnNext.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //hide the current main panel mainPanel.setVisible(false); beginstagetwo(); } }); btnNext.setMinimumSize(new Dimension(112,29)); btnNext.setMaximumSize(new Dimension(112,29)); pnlNextButton.add(btnNext, BorderLayout.CENTER); pnlNextButton.setBorder((new LineBorder(Color.GREEN))); mainPanel.add(pnlNextButton); frame.add(mainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void beginstagetwo() { JPanel pnlPlayerStats = new JPanel(); pnlPlayerStats.setPreferredSize(new Dimension(300, 60)); JLabel lblPlayerName = new JLabel("Player Name:"); JTextField txtPlayerName = new JTextField(30); txtPlayerName.setText(playerSelected); lblPlayerName.setAlignmentX(JTextField.CENTER_ALIGNMENT); JLabel lblPlayerGold = new JLabel("Gold:"); JTextField txtPlayerGold = new JTextField(6); lblPlayerGold.setAlignmentX(JTextField.CENTER_ALIGNMENT); pnlPlayerStats.setLayout(new FlowLayout()); pnlPlayerStats.add(lblPlayerName); pnlPlayerStats.add(txtPlayerName); pnlPlayerStats.add(lblPlayerGold); pnlPlayerStats.add(txtPlayerGold); pnlPlayerStats.setBorder(new LineBorder(Color.ORANGE, 3)); secondPanel.setPreferredSize(new Dimension(600, 400)); secondPanel.add(pnlPlayerStats); frame.add(secondPanel); frame.pack(); frame.setVisible(true); } } class MyMouseHandler extends MouseAdapter { Data dt = new Data(); @Override public void mouseClicked(MouseEvent evt) { JLabel source = (JLabel) evt.getSource(); String labelname = source.getName(); dt.playerSelected = labelname; } }