I am trying to change the size of the JPanel
which is to be returned in createComponent()
and adjust it to the size of the Frame/ the screen using getParent()
. but the panels size remains unchanged. The code for my CustomPanel follows:
class CustomPanel extends JPanel {
final Image backImage = getBackImage();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backImage, 0, 0, null);
}
@Override
public Dimension getPreferredSize() {
Container c = this.getParent();
Dimension d = new Dimension(c.getWidth(), c.getHeight());
return d;
}
}
and the sample code inside the createComponent()
method follows:
JPanel buttonPanel = new CustomPanel();
buttonPanel.add(button);
return buttonPanel;
I have tried to do it directly when instantiating the buttonPanel
which gave me error. The sample code for this is:
JPanel buttonPanel = new JPanel();
Container c = buttonPanel.getParent();
Dimension d = new Dimension(c.getWidth(), c.getHeight());
buttonPanel.setSize(d);
buttonPanel.add(button);
return buttonPanel;
and the error log is:
Caused by: java.lang.NullPointerException
at com.test.customcode.SampleScreenNew.createComponent(SampleScreenNew.java:101)
at com.install4j.runtime.installer.frontend.ScreenEnvelope.addScreenContent(ScreenEnvelope.java:372)
at com.install4j.runtime.wizard.StandardScreen.createMainPanel(StandardScreen.java:136)
at com.install4j.runtime.wizard.StandardScreen.setupComponent(StandardScreen.java:78)
at com.install4j.runtime.wizard.StandardScreen.initScreen(StandardScreen.java:50)
at com.install4j.runtime.installer.frontend.ScreenEnvelope.init(ScreenEnvelope.java:61)
at com.install4j.runtime.installer.frontend.ScreenEnvelope.<init>(ScreenEnvelope.java:46)
at com.install4j.runtime.installer.frontend.WizardScreenExecutor$1.run(WizardScreenExecutor.java:199)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
In your screen implementation, override
@Override
public boolean isFillVertical() {
return true;
}
@Override
public boolean isFillHorizontal() {
return true;
}
Then the panel returned by createComponent()
will fill all the available space.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.