When I run my program, I get a StackOverflowError.
What is incorrect in my approach? I'm not sure how to pass it in without causing it to fail to compile. The initDialog and initComponents methods are just for the rest of the program to create the interface
public class DiceGUI extends JFrame {
DiceGUI(String title) {
super(title);
initDialog();
setSize(1000, 800);
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
setDefaultCloseOperation(sch.closeHandler());
}
});
}
public static void main(String[] args) {
DiceGUI diceGUI = new DiceGUI("Dice Game");
diceGUI.setVisible(true);
}
}
public void initDialog() {
dPanel = new JPanel();
dPanel.setLayout(new BoxLayout(dPanel, BoxLayout.Y_AXIS));
JLabel invalidInput = new JLabel("");
String[] options = {"OK"};
dPanel.add(new JLabel("Leave blank to make target 101, enter a number below to change it"));
dPanel.add(invalidInput);
JTextField text = new JTextField("");
dPanel.add(text);
boolean flag;
do {
int changeGameTarget = JOptionPane.showOptionDialog(null, dPanel, "Dice Game", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
flag = sch.dialogHandler(changeGameTarget, text, invalidInput);
} while (!flag);
text.setText("");
}
Second Class
public class SwingComponentsHandler {
private DiceGUI diceGUI = new DiceGUI("");
public void restartHandler(JButton r) {
r.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg = "New Game?";
int yes = JOptionPane.showConfirmDialog(null, msg, "New Game?", JOptionPane.YES_NO_OPTION);
// Restart game
if (yes == JOptionPane.YES_OPTION) {
diceGUI.initDialog();
}
}
});
}
}
Stacktrace:
Exception in thread "main" Exception in thread "main" java.lang.StackOverflowError
at sun.awt.X11GraphicsConfig.pGetBounds(Native Method)
at sun.awt.X11GraphicsConfig.getBounds(X11GraphicsConfig.java:314)
at java.awt.Window.init(Window.java:505)
at java.awt.Window.<init>(Window.java:537)
at java.awt.Frame.<init>(Frame.java:420)
at javax.swing.JFrame.<init>(JFrame.java:233)
at DiceGUI.<init>(DiceGUI.java:21)
at SwingComponentsHandler.<init>(SwingComponentsHandler.java:11)
at DiceGUI.<init>(DiceGUI.java:16)