Reputation: 57
I have a frame with several panels. Whenever I press/release/click the center panel, a JLabel shows the amount of times and the coordinate where I have performed such an action. The problem is that the window doesn't refresh, so all these values on JLabel stay at 0. Usually with paintComponents I would use repaint()
, but I'm not using this method.
Here is the piece of code where I am supposed to 'repaint':
public void mousePressed(MouseEvent e) {
pressCounter++;
x = e.getX();
y = e.getY();
buildIt(); //this works, but doesn't do what i want it to do
}
Calling buildIt()
actually works, but it would open a new window after every action, instead of refreshing the original window. I've also tried revalidate()
and removeAll()
here, but they don't seem to do anything at all.
So how do I refresh the window?
Here is the complete code for reference:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MouseMonitor extends JPanel implements MouseListener {
JFrame frame;
JPanel topPanel, centerPanel, bottomPanel;
JLabel pressLabel, releaseLabel, clickLabel, xLabel, yLabel;
int pressCounter = 0;
int releaseCounter = 0;
int clickCounter = 0;
int x, y;
void buildIt() {
frame = new JFrame("Mouse Monitor");
topPanel = new JPanel();
centerPanel = new JPanel();
bottomPanel = new JPanel();
pressLabel = new JLabel("pressed: " + pressCounter);
releaseLabel = new JLabel("released: " + releaseCounter);
clickLabel = new JLabel("clicked: " + clickCounter);
xLabel = new JLabel("x-coordinate: " + x);
yLabel = new JLabel("y-coordinate: " + y);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
topPanel.add(pressLabel);
topPanel.add(releaseLabel);
topPanel.add(clickLabel);
bottomPanel.add(xLabel);
bottomPanel.add(yLabel);
pressLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
releaseLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
clickLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
xLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
yLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
Color color = new Color(193, 236, 255);
centerPanel.setBackground(color);
centerPanel.addMouseListener(this);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e) {
pressCounter++;
x = e.getX();
y = e.getY();
buildIt();
}
public void mouseReleased(MouseEvent e) {
releaseCounter++;
}
public void mouseClicked(MouseEvent e) {
clickCounter++;
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public static void main(String[] args) {
new MouseMonitor().buildIt();
}
}
Upvotes: 0
Views: 54
Reputation: 2266
You don't need to call the built()
method to refresh the frame or panel. For the purpose of what you want, you just have to change the text on the label. So for instance, considering mousePressed(MouseEvent e)
method, take out the buildIt()
method:
public void mousePressed(MouseEvent e) {
pressCounter++;
pressLabel.setText("pressed: " + pressCounter);
}
Do this for each of the methods and you are good to go. Maintain code in the main
method.
Upvotes: 1