Resizing of JFrame while the font on buttons increases

Hey,

I would like to ask you how to resize my JFrame ,when the size of buttons put in this frame increases.

I mean the font on the buttons increases by 1 point every time I press the button. My goal is to make the two buttons visible all the time. Therefore I need to resize my JFrame. I tried in the following method, but it did not work :(

(cp is contentPane() of my JFrame - frame)

public void actionPerformed(ActionEvent e){

Component c = (Component) e.getSource();

Integer nazwa= c.getFont().getSize()+1;

int width = (int) cp.getPreferredSize().getWidth();

if(cp.getPreferredSize().getWidth()>cp.getWidth()) {

cp.setSize(new Dimension(width, cp.getHeight()));

cp.validate();

}

int height = (int) cp.getPreferredSize().getHeight();

if(cp.getPreferredSize().getHeight()>cp.getHeight()) {

cp.setSize(newDimension(cp.getWidth(),height));

cp.validate();

}

c.setFont( new Font(c.getName(), c.getFont().getStyle(), nazwa));

frame.validate();

}

[1074 byte] By [Pabloooa] at [2007-11-27 4:53:47]
# 1
You need to pack() the frame.
camickra at 2007-7-12 10:08:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

pack() does not help, as I have already used it.

Pls find below the whole code of the program:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class GUI implements ActionListener{

final int BNUM = 2;

static JFrame frame = new JFrame()

static Container cp = frame.getContentPane();

GUI() {

cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));

for (int i = 1; i <= BNUM; i++) {

JButton b = new JButton("Przycisk " + i);

b.addActionListener(this);

cp.add(b);

}

frame.pack();

frame.show();

}

public void actionPerformed(ActionEvent e){

Component c = (Component) e.getSource();

Integer nazwa= c.getFont().getSize()+1;

int width = (int) Integer.parseInt(cp.getPreferredSize().getWidth().toString());

if(cp.getPreferredSize().getWidth()>cp.getWidth())

cp.setSize(new Dimension(width, cp.getHeight()));

if(cp.getPreferredSize().getHeight()>cp.getHeight()) {

cp.setSize(new Dimension(cp.getWidth(),cp.getPreferredSize().getHeight()));

cp.validate();

c.setFont( new Font(c.getName(), c.getFont().getStyle(), nazwa));

}

public static void main(String[] a) {

//cp.setPreferredSize(new Dimension(200,200));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

new GUI(); }

}

Pabloooa at 2007-7-12 10:08:19 > top of Java-index,Desktop,Core GUI APIs...
# 3
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.You have to invoke the pack() method again in you actionPerformed() method after you change the font.
camickra at 2007-7-12 10:08:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

Instead of validating cp validate frame:

cp.validate(); // becomes frame.validate(); //or pack() which does the same thing after calculating preferred size of components.

BTW. Polish names are nice but it is generally easier for people if they can undestand what they are reading ;p

hellbindera at 2007-7-12 10:08:19 > top of Java-index,Desktop,Core GUI APIs...
# 5

validate() and pack() do not do the same thing.

validate() wil recalculate the preferedSize of a component, and lay out the child components in the component, but if the frame isn't large enough to display the entire component then the component will need to be truncated in some form.

pack() will recalulate the preferred size of the components on the frame and then resize the frame to that every component is displayed at its preferred size.

camickra at 2007-7-12 10:08:19 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thank you very much camickr. Now it works :) and sorry for non using the code in the proper format. It has been my first post ever - that's the reason why. Now I know :)Message was edited by: Pablooo
Pabloooa at 2007-7-12 10:08:19 > top of Java-index,Desktop,Core GUI APIs...