JPanel/resizing/ problems

javacmpalmer
javacmpalmer used Ask the Experts™
on
Hello!

I have 3 JPanels. Each JPanel has components on it. The bottom panel and its components should be only as big as its needs to be. The middle panel should be only as big as it needs to be and the top panel and its components should expand to take the rest of the space. I am having problems getting this to work. Here is the code I am starting with. I thought I could fix it with a grid bag layout but had problems. I would really appreciate some help.

Chris



  private void init() {
    setSize(600,600);

    // Load an icon for applicaiton.
    URL u = this.getClass().getClassLoader().getResource(Constants.APP_ICON);
    setIconImage(Toolkit.getDefaultToolkit().getImage(u));

    _mview = new MViewPane();
    _mview.setParams(Constants.MARVIN_DEFAULT);
    _mview.setEditable(2);
    _mview.setDetachable(true);
    _mview.setSize(450,400);
    _mview.setMinimumSize(new Dimension(450,400));
    JPanel jpMol = new JPanel(new BorderLayout());
    JPanel jpAction = new JPanel(new GridLayout(4,2,5,5));
    jpMol.setBorder(BorderFactory.createTitledBorder("Current Molecule:"));
    jpAction.setBorder(BorderFactory.createTitledBorder("Actions:"));

    jpMol.add(_mview,BorderLayout.CENTER);
    // Notes/Instructions//////////////////////////////////////////////////
    JPanel jpNotes = new JPanel();
    jpNotes.setBorder(BorderFactory.createTitledBorder("Instructions:"));
    JTextArea marvinNote = new JTextArea();
    marvinNote.setEditable(false);
    // Setup the notes /////////////////////////////////////////////////
    StringBuffer note = new StringBuffer("1. Double click on the molecule to open Marvin Draw.");
    note.append("\n2. Edit the molecule.");
    marvinNote.setText(note.toString());
    jpNotes.add(marvinNote);
    marvinNote.setBackground(jpNotes.getBackground());
    ////////////////////////////////////////////////////////////////////

    JLabel editor = new JLabel("External Sketcher:",JLabel.RIGHT);
    JLabel edit = new JLabel("Edit:",JLabel.RIGHT);
    JLabel save = new JLabel("Save:",JLabel.RIGHT);
    JLabel close = new JLabel("Close:",JLabel.RIGHT);

    // Create the combo box, select item at index 4.
    // Indices start at 0, so 4 specifies the pig.
    sketchList = new JComboBox(Constants.SKETCHERS);
    // Only one right now change it later....
    sketchList.setSelectedIndex(0);
//    sketchList.setSelectedIndex(getDefaultSketcher());
    CloseMolWorkBenchAction c = new CloseMolWorkBenchAction();
    registerAction(c);
    EditMolAction ema = new EditMolAction();
    registerAction(ema);
    jbEdit = new JButton(ema);
    SaveMolAction sma = new SaveMolAction();
    registerAction(sma);
    jbSave = new JButton(sma);
    jbSave.setEnabled(false);

    jbClose = new JButton(c);

    jpAction.add(editor);
    jpAction.add(sketchList);
    jpAction.add(edit);
    jpAction.add(jbEdit);
    jpAction.add(save);
    jpAction.add(jbSave);
    jpAction.add(close);
    jpAction.add(jbClose);

//    GridBagLayout gridbag = new GridBagLayout();
//    GridBagConstraints con = new GridBagConstraints();
//    this.getContentPane().setLayout(gridbag);
//    con.fill = GridBagConstraints.BOTH;
//    con.weightx = 1.0;
//    con.weighty = 1.0;
//    con.gridwidth = GridBagConstraints.REMAINDER;
//    con.gridheight = 1;
//    con.ipady = 300;
//    gridbag.setConstraints(jpMol,con);
//    this.getContentPane().add(jpMol);
//    con.ipady = 0;
//    con.weighty = 1.0;
//    con.fill = GridBagConstraints.HORIZONTAL;
//    gridbag.setConstraints(jpNotes,con);
//    this.getContentPane().add(jpNotes);
//    con.fill = GridBagConstraints.HORIZONTAL;
//    con.weighty = 1.0;
//    con.anchor = GridBagConstraints.SOUTH;
//    gridbag.setConstraints(jpAction,con);
//    this.getContentPane().add(jpAction);
    getContentPane().add(jpMol,BorderLayout.NORTH);
    getContentPane().add(jpNotes,BorderLayout.CENTER);
    getContentPane().add(jpAction,BorderLayout.SOUTH);

  }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
Use BorderLayout for all panels, plus create a new panel and try the following:

newpanel.add(BorderLayout.CENTER, top);
newpanel.add(BorderLayout.SOUTH, middle);
add(BorderLayout.CENTER, newpanel);
add(BorderLayout.SOUTH, bottom);

Author

Commented:
I don't think I completely understand. I have three panels. The above is a solution when you have two. I need to wedge one in the middle but not do what normal something set with .CENTER does.

Chris
Java Developer
Top Expert 2010
Commented:
I know you have three, in my comment they are top, bottom and middle.

Author

Commented:
You are correct.....just got off a plane....zzzzzzz Thank you.

Chris
Mick BarryJava Developer
Top Expert 2010

Commented:
Chris,

Happy to help :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial