0

I have Added two JPanels into a JPanel with GridBagLayout. Now the issue is :

When the main panel loads it shows the upper panel bit above the actual place it should be and when data from db loads into the JTextfields and JComboBox then panel goes to its actual size.

sample code:

public class JPanelIssue extends JPanel { public JPanelIssue() { JPanel mainPanel = new JPanel(new GridBagLayout()); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); mainPanel.add(panel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.6, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); mainPanel.add(panel2, new GridBagConstraints(0, 1, 1, GridBagConstraints.REMAINDER, 1.0, 0.4,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); add(mainPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); } } 

panel1 contains fields related to search criteria and panel2 contains JTable dispalying reults.

Is there any problem of AWT event queue or any other problem?

Thanks in advance.

5
  • Sounds perfectly normal to me. Got a screen shot or bet yet some sample code? Commented Apr 26, 2013 at 9:48
  • A bit more detail please. What state are the panels and fields being populated? Shown? Hidden? Are the sizes set? Does the user have to click something before these fields are populated? Commented Apr 26, 2013 at 9:49
  • @MadProgrammer I have put sample code. Commented Apr 26, 2013 at 10:11
  • @Skepi I have given some details as well. Commented Apr 26, 2013 at 10:12
  • 1
    @eatSleepCode Showing some very small amount of code that will not display precisely your issue is not of much help for us. Consider posting an SSCCE instead. Commented Apr 26, 2013 at 10:47

1 Answer 1

2

comment only

not reproducible from this code snipped, because you forgot to add something about JTextfields and JComboBox then panel goes to its actual size.

When the main panel loads it shows the upper panel bit above the actual place it should be and when data from db loads into the JTextfields and JComboBox then panel goes to its actual size.

GBC is based on PreferredSize came from JComponents back to the contianer (by assuming that about JPanel)

there are two ways

  1. (proper)set proper PreferredSize for JTextField and JComboBox too

  2. (sizing hack)override getPreferredSize for JPanel contains JTextField and JComboBox

  3. then all another parent(s) / containers should be notified without override any setSize, setBounds, setPreferredSize, only by JFrame.pack()

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution but I am not getting 3rd point and what to write in the overriden getPreferredSize method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.