GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Window Size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Window Size" Watch "Window Size" New topic
Author

Window Size

Steve Dunn
Greenhorn

Joined: Dec 30, 2003
Posts: 7
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;
}


}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Dec 30, 2003
Posts: 7
The last of the 5 jpanels.
Steve Dunn
Greenhorn

Joined: Dec 30, 2003
Posts: 7
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

Joined: Jun 09, 2003
Posts: 4632
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

"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

Joined: Dec 30, 2003
Posts: 7
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

Joined: Jun 09, 2003
Posts: 4632
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."
 
GeeCON Prague 2014
 
subject: Window Size