Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "do something" on Swing component resizing?

Tags:

java

swing

Like Adam Paynter suggested, you can also add an inner class to your code, like this:

class ResizeListener extends ComponentAdapter {
        public void componentResized(ComponentEvent e) {
            // Recalculate the variable you mentioned
        }
}

The code you have entered between the innermost brackets will be executed everytime the component get resized.

Then you add this listener to your component with

myJPanel.addComponentListener(new ResizeListener());

You can get your component by using e.getComponent(). This way you can call any method of your component from inside the inner class like

e.getComponent().getWeight();

I suppose you could override the various setSize and resize methods and perform the calculation there. However, you may not find all the places where the size can be changed. You may want to have your class implement ComponentListener and simply listen to itself for resize events.

Warning: I am not a Swing expert.

Warning: I have not compiled this code.

public class MyJPanel extends JPanel implements ComponentListener {

    public MyJPanel() {
        this.addComponentListener(this);
    }

    public void paintComponent(Graphics g) {
        // Paint, paint, paint...
    }

    public void componentResized(ComponentEvent e) {
        // Perform calculation here
    }

    public void componentHidden(ComponentEvent e) {}

    public void componentMoved(ComponentEvent e) {}

    public void componentShown(ComponentEvent e) {}

}

If I understand the question correctly then you should read the section from the Swing tutorial on How to Write a Component Listener which shows you how to listen for a change in a components size.


If the calculation isn't time consuming, I would just re-calculate the value each time in paintComponent().

Otherwise, you can save a value that is the size of the component and check it against the new size in paintComponent. If the size changed, then recalculate, otherwise don't.

private Dimension size;

protected void paintComponent(Graphics g){
    if (!size.equals(getSize())){
        size = getSize();
        // recalculate value
    }
}

Or, you can do the calculation on a resize event.

//in the constructor add the line
addComponentListener(resizeListener);

private ComponentListener resizeListener = new ComponentAdapter(){
    public void componentResized(ActionEvent e){
        // recalculate value
    }
};

The simplest way is to implement a ComponentListener:

myjpanel.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        //recalculate variable
    }
});

Here, I have used a ComponentAdapter because I only intend on overriding componentResized().


Here's what I use (where CoordinatePlane is a JPanel):

I'm not an expert

public CoordinatePlane() {
    setBackground(Color.WHITE);
    this.addComponentListener(new ComponentAdapter(){
        public void componentResized(ComponentEvent e) {
            //YOUR CODE HERE         
        }
    });
}