Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window sizing trouble

Status
Not open for further replies.

jeremytaffy

Technical User
Sep 6, 2001
75
US

I have an application that loads in a small window. When a selection is made, I clear the small window, and want to make it full screen sized, with a big button filling most of the screen. I have been able to resize the window, but the button only fills the size of the area that was originally there. I cant figure it out. code included

thanks in advance
j

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.border.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.lang.*;

public class go extends JPanel {
JLabel label;
JFrame frame1;
String simpleDialogDesc = "The choices:";
JLabel space = new JLabel("");
final JButton yesbutton = new JButton("Go");
final JButton yesbutton2 = new JButton("Go");

final JButton voteButton = new JButton("Choose Wisely");
ButtonGroup selection = new ButtonGroup();
JRadioButton rbutton1 = new JRadioButton("A", true);
JRadioButton rbutton2 = new JRadioButton("B", false);
public go(final JFrame frame) {


yesbutton.setBackground(Color.green);
yesbutton.setFont(new Font("sansserif", Font.PLAIN, 600));
yesbutton2.setBackground(Color.green);
yesbutton2.setFont(new Font("sansserif", Font.PLAIN, 600));
final JLabel title = new JLabel(" Click the \"Choose\" button"
+ " once you have selected a choice. ",
JLabel.CENTER);
selection.add(rbutton1);
selection.add(rbutton2);
JLabel label2 = new JLabel(" ");
final JPanel buttons = new JPanel();
GridLayout flow1 = new GridLayout(9,1);
buttons.setLayout(flow1);
buttons.add(rbutton1);
buttons.add(rbutton2);
buttons.add(voteButton);

label = new JLabel("", JLabel.CENTER);


// Set the layout.
setLayout(new BorderLayout());
add(title, BorderLayout.NORTH);

add(buttons, BorderLayout.CENTER);
add(label2, BorderLayout.SOUTH);
add(label2, BorderLayout.WEST);

////////////////////////////////
voteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (rbutton1.isSelected()){
removeAll();
setVisible(false);
setVisible(true);
//System.exit(0);
//frame1.setVisible(true);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int wdFrame = screenSize.width;
int hgFrame = screenSize.height;
System.out.println(wdFrame);
System.out.println(hgFrame);
int space = 5; frame.setSize(wdFrame,hgFrame);
frame.setBounds(5,5,wdFrame,hgFrame);
setLayout(null);
yesbutton.setBounds(5,5,900, 900);
add(yesbutton);
setVisible(false);
setVisible(true);
}
}
});
}
public static void main(String[] args) {
final JFrame frame = new JFrame("Go");

Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1,1));
contentPane.add(new go(frame));

// Exit when the window is closed.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();
frame.setVisible(true);
}
}


 
i was wondering about a similar thing the other day. I couldnt figre it out either
 
OK, well i don't know if that helps your problem, but there are some strange thingies in your source:

1) Kill that "final" stuff, as far as i see there is no sense to make anything final in your program. At least not local variables...

2) Don't set layouts to null, simply set it to the layout you need, null layout messes most things up

3) When you want to clear a panel/frame use removeAll(), afterwards add new items to it!

4) Don't write class names (like go) in lower-case, commonly the first letter is upper-case. Just to be fussy ;)

So, my suggestion what could help:
Replace the code of method
public void actionPerformed(ActionEvent e)
with this:
Code:
if (rbutton1.isSelected())
{
   Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
   int wdFrame=screenSize.width;
   int hgFrame=screenSize.height;
   frame.setBounds(5, 5, wdFrame, hgFrame);
   frame.removeAll();
   frame.add(yesButton);
   frame.doLayout();   
}
[\code]
If that doesn't help, set the layout of frame in your main method to BorderLayout and add components to the CENTER

Christoph
 
thanks for the help. I got it to work by modifying it to this:

if (rbutton1.isSelected()){
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int wdFrame=screenSize.width;
int hgFrame=screenSize.height;
frame.setBounds(0, 0, wdFrame, hgFrame);
removeAll();
add(yesbutton);
frame.doLayout();

however, i want to be able to add one more smal cancel button just to the side of big button. i tried adding the button the same way, but it added it over the top as a huge button.
any help would be appreciated
thanks in advance!
j
 
did you try using GridBagLayout to work things out? That is very powerful, can do anything you want.
 
Don't try GridBagLayout at least you have some REALLY complicated layout to do. I have solved all layout problems w/o it up to now.

How you add the small button to the side depends on how the small button should look like afterwards.

When it's something like
Code:
+--------+-+
|        | |
|        | |
|        | |
+--------+-+
[\code]
Then simply use my best friend, the BorderLayout, put the big one in CENTER and the small one in EAST (or WEST for the other side).

But if it shall be not as high, something like this
[code]
+--------+
|        |
|        +-+
|        | |
+--------+-+
[\code]
you should nest to BorderLayouts, like this:
[code]
p1=new Panel(new BorderLayout());
p2=new Panel(new BorderLayout());
p1.add(big, BorderLayout.CENTER);
p1.add(p2, BorderLayout.EAST);
p2.add(small, BorderLayout.SOUTH);
[\code]
Maybe you need some padding stuff, but that should do it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top