I have this simple code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.border.LineBorder;
@SuppressWarnings("serial")
public class MyFrame extends JFrame{
SpringLayout layout;
JPanel contentPane;
JPanel pane1;
public MyFrame(){
layout=new SpringLayout();
contentPane=new JPanel(layout);
setContentPane(contentPane);
setSize(new Dimension(800,600));
pane1=new JPanel(new SpringLayout());
pane1.setPreferredSize(new Dimension(200,200));
pane1.setBorder(new LineBorder(Color.black));
layout.putConstraint(SpringLayout.WEST, pane1, 0, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.EAST, pane1, getWidth()/2, SpringLayout.WEST, contentPane);
contentPane.add(pane1);
setEvents();
setVisible(true);
}
private void setEvents(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
this.addComponentListener(new ComponentListener(){
@Override
public void componentHidden(ComponentEvent arg0) {}
@Override
public void componentMoved(ComponentEvent arg0) {}
@Override
public void componentResized(ComponentEvent arg0) {
System.out.println("RESIZING EVENT:");
System.out.println("Frame Size: "+getWidth()+", "+getHeight());
System.out.println("Pane1 Size: "+pane1.getWidth()+", "+pane1.getHeight()+"\n");
layout.putConstraint(SpringLayout.EAST, pane1, getWidth()/2, SpringLayout.WEST, contentPane);
contentPane.revalidate();
contentPane.repaint();
}
@Override
public void componentShown(ComponentEvent arg0) {}
});
}
public static void main(String[] args){
MyFrame frame=new MyFrame();
}
}
I'm having trouble in getting the actual size of my pane1 component.
if I execute my code, MAXIMIZE and restore my window, numbers are wrong. Let me explain with a simple output of this code:
RESIZING EVENT: Frame Size: 800, 600 Pane1 Size: 400, 200
RESIZING EVENT: Frame Size: 800, 600 Pane1 Size: 400, 200
RESIZING EVENT: Frame Size: 1382, 744 Pane1 Size: 400, 200 maximizing here
RESIZING EVENT: Frame Size: 800, 600 Pane1 Size: 691, 200 restoring here
As you can see, the last dimension value (691, 200) should be referred to (1382,744) frame dimension, and not to the last one (800,600)...
calling revalidate and repaint methods updates my graphics (showing what is expected from this code) but doesn't help with this problem. Any help is appreciated
The problem is that
contentPaneis being used in two different conflicting roles. First the code usescontentPaneas the content pane ofMyFramewith a call tosetContentPane(contentPane). Next,contentPaneis treated as a child of itself when used as an argument tolayout.putConstraint.The content pane of a
JFrameis the parent of a children of thatJFrameand the container for which the layout is applied. See[RootPaneContainer][1].Therefore, you need a third panel. I suggest calling it
panelWestand use it in thelayout.putConstraintinstead ofcontentPane. Don't forget to initialize it and add it toMyFrame, just likepanel. And remove all calls to addcontentPaneto itself (which include adding it toMyFrame, which would be adding it to itself), and remove all setting of layout constaints forcontentPaneas if it is a child ofMyFrame.