Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Limit size of swing frame & or components

  Asked By: Aditi    Date: Nov 28    Category: Java    Views: 2151
  

How do I keep the frame here from blowing off of the Windows Window?


I actually WANT the scrollPane to be two of the panels (p) that are
in it high and to have the panel with the exit button in it outside
the scroll and have any panels more than two be scrolled to.

Everything grows to the point, as it is now, to where the exit button
is barely in the windows window. If I take it out of the scroll, you
can't get to it nohow.

/**
* Opens a window showing the used cards ordered by suit & value
*
*@author john sayre
*@created March 21, 2003
*/
class doUsedGui extends JFrame {

Vector Used;
//Contains all of the hands
Vector Rows = new Vector();
//will contain panels of 5 cards each

//will contain the card images

JPanel p1;
JPanel p;

//panel of 5 images
GridLayout g;
//for the panel to set up the # of rows it needs based on 5
cards per row
JPanel bp = new JPanel(new GridLayout(1, 1, 4, 4));


//Exit button goes here

/**
* Constructor for the doUsedGui object
*
*@param pUsed Cards already used, sorted by suit then value
*/
doUsedGui(Vector pUsed) {
Used = pUsed;
}


/**
* Plugs card image paths into vector of panels
*/
public void getRows() {
setTitle("Used Cards");
setMaximizedBounds(new Rectangle(5, 5));
setResizable(false);


int rows = ((int) Math.ceil(Used.size() / 5));
//as many rows as needed

int image = 0;
//get each image from the vector
System.out.println("Used size " + Used.size());
for (int panels = 0; panels < rows; panels++) {
//add panels to row vector
p = new JPanel(new GridLayout(1, 5, 4, 8));

for (int iconNo = 0; iconNo < 5; iconNo++) {
//5 icons per row
System.out.println("Row " + panels
+ " column " + iconNo);
if (image < Used.size()) {
//images < total in used card
vector
Card card = new Card();
card = (Card) Used.get
(image++);
p.add(new JLabel(new ImageIcon
(

"C:\\java\\dev\\pokerg\\Cards\\" + card.cName +
".gif")));
}
}

Rows.add(p);
//add panel to vector
}

doFrame();
//add the panels to the frame
}


/**
* creates new frame with panels of cards
*/
private void doFrame() {

JButton exitButton = new JButton(" Exit ");
bp.add(exitButton);
JPanel p2 = new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));

p1 = (JPanel) getContentPane();
p1.setLayout(new BorderLayout());
p1.setMaximumSize(new Dimension(100, 5));
//p1 = new JPanel(new BoxLayout(p1,
BoxLayout.Y_AXIS));
exitButton.addActionListener(
new ActionListener() {
public void actionPerformed
(ActionEvent e) {
dispose();
}
});
System.out.println("Rows size " + Rows.size());
for (int i = 0; i < Rows.size(); i++) {
p2.add((JPanel) Rows.get(i));
System.out.println("
Row " + i);
}
p2.add(bp);

JScrollBar jsc = new JScrollBar();
JScrollPane jsp = new JScrollPane(p2);
jsc.setMaximum(5);
jsp.setVerticalScrollBar(new JScrollBar());

p1.add(jsp, BorderLayout.CENTER);

pack();
setVisible(true);
}

}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Willie Gomez     Answered On: Nov 28

Try using fewer methods like setMaximizedBounds, setResizable and let
java control the gui a little more.

Don't add the exitButton to the bp panel. Add it directly to the
contentPane... getContentPane().add(exitButton, BorderLayout.SOUTH);
That way the exitButton is not on the scrollpane, but will always
show up on the window.
Then add the panel  with the cards  into the scrollpane and add the
scrollpane to the contentPane (I think you do that already).

Since the contentPane is already BorderLayout, you don't need these
lines
p1 = (JPanel) getContentPane();
p1.setLayout(new BorderLayout());

Also, the JScrollPane automatically gets both Horizontal and Vertical
scrollbars, therefore you don't need the JScrollBar object

 
Answer #2    Answered By: Pravat Jainukul     Answered On: Nov 28

This still leaves the exit  button invisible & hiding under the
Windows taskbar at the bottom of the screen

*********** REPLY SEPARATOR ***********

On 4/17/2003 at 2:05 PM Michael wrote:
Try using fewer methods like setMaximizedBounds, setResizable and let
java control the gui a little more.

Don't add the exitButton to the bp panel. Add it directly to the
contentPane... getContentPane().add(exitButton, BorderLayout.SOUTH);
That way the exitButton is not on the scrollpane, but will always
show up on the window.
Then add the panel  with the cards  into the scrollpane and add the
scrollpane to the contentPane (I think you do that already).

Since the contentPane is already BorderLayout, you don't need these
lines
p1 = (JPanel) getContentPane();
p1.setLayout(new BorderLayout());

Also, the JScrollPane automatically gets both Horizontal and Vertical
scrollbars, therefore you don't need the JScrollBar object

 
Answer #3    Answered By: Rocco Anderson     Answered On: Nov 28

Try using the method setPreferredSize(Dimension) for objects you want
to control.

JPanel exitPanel = new JPanel(new FlowLayout());
exitPanel.setPreferredSize(new Dimension(200,100));
exitPanel.add(exitButton);
getContentPane().add(exitPanel, BorderLayout.SOUTH);

Placing JButtons on a panel  using FlowLayout shows the buttons in
their preferred size, rather than using the whole panel or area.

 
Didn't find what you were looking for? Find more on Limit size of swing frame & or components Or get search suggestion and latest updates.




Tagged: