• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Window Size

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm building a gui using a jframe as the base window and adding jpanel's to the frame using a gridbaglayout manager. The gridbaglayout is a single column with 5 rows; a jlabel with image and four jpanel's.The first jpanel is a nested panel with 2 borderlayout panels each with 2 flowlayout panels. My problem is the last panel is not visible when i run the application. If i resize the window the panel is there. I've tried adjusting the jframe height, row & column weights in the gridbagconstraints & removing/repositioning panels. If i remove or reposition the jlabel w/image then only the first two jpanels are visible; the same happens if i remove or reposition any of the other jpanels.

Can someone provide some assistance! Any help would be greatly appreciated. Following is some of my code.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import javax.swing.JPanel;



public class RequestWindow extends JFrame {

private JPanel jContentPane = null;

/**
* This is the default constructor
*/
public RequestWindow() {
super();
initialize();
}



public static void main(String[] args) {

RequestWindow rw = new RequestWindow();

rw.setTitle("Request Window");

rw.pack();
rw.setVisible(true);

}


private void initialize() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}

catch(ClassNotFoundException cnfe) {

}
catch (IllegalAccessException ilae) {

}
catch (UnsupportedLookAndFeelException ulfe) {

}
catch (InstantiationException ie) {

}

JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());

}


private JPanel getJContentPane() {
if (jContentPane == null) {

jContentPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;

JLabel ip = new JLabel(new ImageIcon("c:\\HeaderImage.jpg"));
jContentPane.add(ip,c);

c.gridy = GridBagConstraints.RELATIVE;
c.anchor = GridBagConstraints.CENTER;
c.gridheight = GridBagConstraints.RELATIVE;

jContentPane.add(getRequestPanel(),c);

jContentPane.add(getFieldSelectPanel(),c);

jContentPane.add(getRequestFilePathPanel(),c);

}
return jContentPane;
}


}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> My problem is the last panel is not visible when i run the application.

which one?

the last of the 5 added to the contentPane, or the last of this one
> The first jpanel is a nested panel with 2 borderlayout panels each with 2 flowlayout panels.

when using gridbaglayout, you are much better off supplying a working sample
program that demostrates the problem.
 
Steve Dunn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last of the 5 jpanels.
 
Steve Dunn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the rest of my code, all of the jpanels are render correctly. The only problem is the last panel is not visible when the window is displayed.


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import javax.swing.JPanel;



public class RequestWindow extends JFrame {

private JPanel jContentPane = null;

/**
* This is the default constructor
*/
public RequestWindow() {
super();
initialize();
}



private JPanel getRequestPanel() {

JPanel requestPanel;
String[] statusValues = {"*ALL","Assigned","Unassigned","Open","Completed","Cancelled"};

JLabel projectLbl = new JLabel("Project");
JLabel applAreaLbl = new JLabel("Application Area");
JLabel statusLbl = new JLabel("Status");
JLabel dteRangeLbl = new JLabel("Date Range");
JLabel dteToLbl= new JLabel("To");

JComboBox project = new JComboBox();
JComboBox applArea = new JComboBox();

JComboBox status = new JComboBox(statusValues);

JTextField dteFrom = new JTextField(10);
JTextField dteTo = new JTextField(10);

requestPanel = new JPanel(new BorderLayout());
requestPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Request Selection"));

JPanel rp_North = new JPanel(new FlowLayout(FlowLayout.CENTER));
rp_North.add(projectLbl);
rp_North.add(project);
rp_North.add(applAreaLbl);
rp_North.add(applArea);
rp_North.add(statusLbl);
rp_North.add(status);

JPanel rp_South = new JPanel(new BorderLayout());
JPanel rp_SouthInner = new JPanel(new FlowLayout(FlowLayout.CENTER));
rp_South.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Date Select"));

rp_SouthInner.add(dteRangeLbl);
rp_SouthInner.add(dteFrom);
rp_SouthInner.add(dteToLbl);
rp_SouthInner.add(dteTo);

rp_South.add(getDateSelectPanel(), BorderLayout.NORTH);
rp_South.add(rp_SouthInner, BorderLayout.SOUTH);

requestPanel.add(rp_North, BorderLayout.NORTH);
requestPanel.add(rp_South, BorderLayout.SOUTH);

return requestPanel;
}

private JPanel getDateSelectPanel() {

JRadioButton none = new JRadioButton("None",true);
JRadioButton entered = new JRadioButton("Entered");
JRadioButton assigned = new JRadioButton("Assigned");
JRadioButton completed = new JRadioButton("Completed");

ButtonGroup bg = new ButtonGroup();
bg.add(none);
bg.add(entered);
bg.add(assigned);
bg.add(completed);

JPanel dateSelectPanel = new JPanel(new GridLayout(1,0));

dateSelectPanel.add(none);
dateSelectPanel.add(entered);
dateSelectPanel.add(assigned);
dateSelectPanel.add(completed);

return dateSelectPanel;
}

private JPanel getFieldSelectPanel() {

JPanel fieldSelectPanel;

JCheckBox reqNbr = new JCheckBox("Request Nbr");
JCheckBox reqType = new JCheckBox("Type");
JCheckBox reqRank = new JCheckBox("Rank");
JCheckBox reqDate = new JCheckBox("Date");
JCheckBox reqDesc = new JCheckBox("Description");
JCheckBox reqNotes = new JCheckBox("Notes");
JCheckBox reqResolution = new JCheckBox("Resolution");
JCheckBox reqAssigned = new JCheckBox("Assigned To");
JCheckBox reqEstHrs = new JCheckBox("Estimate");
JCheckBox reqActHrs = new JCheckBox("Actual");

fieldSelectPanel = new JPanel(new GridLayout(5,2,0,0));
fieldSelectPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Field Selection"));
fieldSelectPanel.add(reqNbr);
fieldSelectPanel.add(reqType);
fieldSelectPanel.add(reqDate);
fieldSelectPanel.add(reqRank);
fieldSelectPanel.add(reqDesc);
fieldSelectPanel.add(reqNotes);
fieldSelectPanel.add(reqResolution);
fieldSelectPanel.add(reqAssigned);
fieldSelectPanel.add(reqEstHrs);
fieldSelectPanel.add(reqActHrs);

return fieldSelectPanel;
}

private JPanel getRequestFilePathPanel() {

JLabel fileLbl = new JLabel("File");
JTextField file = new JTextField(30);
JButton browse = new JButton("Browse");

JPanel requestFilePath = new JPanel(new FlowLayout(FlowLayout.LEFT));
requestFilePath.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Output"));

requestFilePath.add(fileLbl);
requestFilePath.add(file);
requestFilePath.add(browse);

return requestFilePath;
}



/**
* @param args
*/
public static void main(String[] args) {

RequestWindow rw = new RequestWindow();

rw.setTitle("Request Window");

rw.pack();
rw.setVisible(true);

}


private void initialize() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}

catch(ClassNotFoundException cnfe) {

}
catch (IllegalAccessException ilae) {

}
catch (UnsupportedLookAndFeelException ulfe) {

}
catch (InstantiationException ie) {

}

JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());

}


private JPanel getJContentPane() {
if (jContentPane == null) {

jContentPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;

JLabel ip = new JLabel(new ImageIcon("c:\\HeaderImage.jpg"));
jContentPane.add(ip,c);

c.gridy = GridBagConstraints.RELATIVE;
c.anchor = GridBagConstraints.CENTER;
c.gridheight = GridBagConstraints.RELATIVE;

jContentPane.add(getRequestPanel(),c);

jContentPane.add(getFieldSelectPanel(),c);

jContentPane.add(getRequestFilePathPanel(),c);

}
return jContentPane;
}


}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"intravler", welcome to the JavaRanch.

We're friendly here, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with incorrect display names will be deleted.
 
Steve Dunn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,

Thanks for your assistance, the code you provided worked. However, it would be helpful if you could explain why the GridBag RELATIVE setting for c.gridy &/or c.gridheight did not work. According to "Complete Java 2 Certification" if I'm filling the layout from top-left to bottom-right one row at a time, I should be able to set my Constraints one time using the RELATIVE/REMAINDER values.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the docs say it should work, but go on to say:

"We recommend specifying the gridx and gridy values for each component; this
tends to result in more predictable layouts."
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic