I'm trying to make a message dialog appear when the user enters an invalid input.
When I try to use null
as the location for the dialog, the dialog appears correctly when 0 or a negative number is entered, but there's no title or text in the dialog.
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draw lines
endY = getHeight(); // bottom
endX = getWidth(); // right side
Input = Integer.parseInt(inputField.getText());
if (Input > 0 ) {
for (int beginY = getHeight() / 2; beginY <= endY; beginY += Input) {
g.drawLine(0, beginY, endX, beginY);
}
} else {
JOptionPane.showMessageDialog(null, "Please do not enter 0 or a negative number.", "Wrong input", JOptionPane.ERROR_MESSAGE);
}
}
I've tried using my JFrame
object which has been declared and initialized in a different class as the first parameter and this works.
However, I want to be able to use this code in my JPanel
class in the paintComponent
method so I can add if/else logic to it when drawing the lines.