I want to modify the size of the JPanel while the program is running, for example with a menu item. How can I reach it? Or how should I modify my program to get acces to the live JPanel in the content pane?
Edit: If I make a contentpane for a gamefield that's 400x400 in the start as default. But what if I want to ad an option in the menu to change the size to 500x500, but without losing all of the content already in progress by the player.
JFrame class:
package me.test;
import javax.swing.JFrame;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
public Main() {
setContentPane(new MyJPanel());
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(100,50);
setResizable(false);
setVisible(true);
}
}
My modified JPanel:
package me.test;
import java.awt.Dimension;
import javax.swing.JPanel;
public class MyJPanel extends JPanel {
public MyJPanel() {
setPreferredSize(new Dimension(400,400));
}
}
Have a look over this code for some ideas. It can change sizes according to actions placed either in a toolbar in the component itself, or menu items of the frame.
Important points of the code are:
pack()
the top level container.Playing around....