Reputation:
If I run my code without the paint() method, the UI appears fine, but after executing paint() the UI only appears after clicking on/hovering over the elements.
I do not know why this happens, I've read somewhere that I might not be calling upon the paint() method the right way or that my setVisible() is not correct but I'm not sure.
My main method:
public static void main(String[] args) {
frame.createGUI();
if(list != null) {
System.out.println(list.toString());
}else{
System.out.println("Het is niet gelukt om uw stoelen juist in te delen. De zaal zit vol.");
}
}
My createGUI method:
public void createGUI() {
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setTitle("Bioscoop Challenge");
JFrame.setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = this.getContentPane();
window.setLayout(new FlowLayout());
resetButton = new JButton("Reset");
seatAmountField = new JTextField("Seats total");
nField = new JTextField("Seats wanted");
methodButton = new JButton("Reservate");
errorMessage = new JLabel("Error message field");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
list = fillList(seatcount);
frame.validate();
frame.repaint();
}
});
methodButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
list = fillSeats(n, list);
frame.validate();
frame.repaint();
}
});
window.add(resetButton);
window.add(seatAmountField);
window.add(nField);
window.add(methodButton);
window.add(errorMessage);
pack();
validate();
setVisible(true);
}
The paint method:
public void paint (Graphics g) {
int x = 215;
int y = 200;
int width = 40;
int height = 60;
try {
for (int i = 0; i < list.size(); i++) {
Color color;
Color color2;
if (list.get(i).IsFree()) {
color = red;
color2 = black;
} else {
color = black;
color2 = red;
}
if (list.get(i).booked) {
color = blue;
}
Rectangle r = new Rectangle(x, y, width, height);
g.setColor(color);
g.fillRect(
(int) r.getX(),
(int) r.getY(),
(int) r.getWidth(),
(int) r.getHeight()
);
g.setColor(color2);
g.drawString(list.get(i).seatNumber.toString(), (width + x) - ((width / 2) + (width / 2) / 2), (height + y) - (height / 2));
x = x + 50;
if (x == 1715) {
x = 215;
y = y + 80;
}
}
} catch(Exception e){
errorMessage = new JLabel("ERROR");
}
}
Help would be appreciated, thanks in advance.
Upvotes: 0
Views: 50
Reputation: 324098
Why do my UI elements disappear after executing paint()?
public void paint (Graphics g)
{
...
}
The paint() method is responsible for painting all the child components, but you don't invoke the default behaviour. The code should be:
public void paint (Graphics g)
{
super.paint(g);
...
}
However, that is still not the proper solution.
You should NOT be overriding paint()!!!
Custom painting is done by overriding paintComopnent()
of a JPanel. Then you add the panel to the frame. Now you won't have any of these issues.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
Upvotes: 1