java frame resize in GUI

Hi,

I'm having some trouble trying to trying shrink my frame into a specified size.The height of can shrink to a minimum size, but the width cannot. I can't figure out the reason why.

The width is located in the MyFrame class.

Here's my code:

package 620524;

import java.awt.*;

import javax.swing.JFrame;

import javax.swing.JComboBox;

import java.awt.event.*;

import a00620524.view.MyFrame;

publicclass MyGUI

{

public MyGUI()

{

}

publicstaticvoid main(String[] args)

{

MyFrame frame =new MyFrame("Simple Calculator");

//MyFrame.DimensionSize

frame.getMinimumSize();

frame.getMinimumSize2();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(200, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

package 620524.view;

import java.awt.*;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JLabel;

publicclass MyFrameextends JFrame

{

String[] operations ={"+","-","/","x"};

public MyFrame(String title)throws HeadlessException

{

super(title);

content();

}

publicvoid content()

{

Container c = getContentPane();

JPanel panel =new JPanel();

Font font =new Font("Arial", Font.BOLD, 28);//change font

JComboBox combo =new JComboBox(operations);

combo.setSelectedIndex(0);

panel.add(combo);

c.add(panel);

}

public Dimension getMinimumSize()

{

Dimension prefSize = getPreferredSize();

returnnew Dimension(200, prefSize.height);

}

/*?*/

public Dimension getMinimumSize2()

{

Dimension prefSize = getPreferredSize();

returnnew Dimension(200, prefSize.width);

}

}

[3906 byte] By [vopoa] at [2007-11-27 11:01:33]
# 1

public static void main(String[] args) {

MyFrame frame = new MyFrame("Simple Calculator");

//MyFrame.DimensionSize

frame.getMinimumSize();

frame.getMinimumSize2();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* place your frame.setSize method above those getMinimumSize method*/

/* so that it is executed first */

frame.setSize(200, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

Yannixa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 2

Is there be a way to resize the contents such as the JCombobox as I stretch and shrink it?

Because I can't find a way to find the code to perform this action.

I've looked through some example code but I can't figure a way to solve this problem.

vopoa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi u can try the following code

panel.setLayout(new BorderLayout() );

panel.add(combo,BorderLayout.CENTER);

c.add(panel);

HimSSSa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 4

I've tried that, but it causes the other contents in the frame shift their positions. Also the contents don't stretch or shrink.

Here's a link of what I want to do:

http://img358.imageshack.us/img358/1747/picture1av1.png

to this

http://img526.imageshack.us/img526/3889/picture2ri3.png

vopoa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 5

how about re-pack(); the jframe? In fact, I don't see that you call pack() once? Or maybe I'm missing it.

Also, rather than setSize(), should you use setPreferredSize()?

Message was edited by:

petes1234

Nevermind,... This is not what you are looking for.

There is an event (windowevent?) that you must look for (I think) and adjust size accordingly.

Message was edited by:

petes1234

Hmmmm been searching...

how about a componentListener looking for componentResized?

Message was edited by:

petes1234

petes1234a at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 6

In the future Swing related questions should be posted in the Swing forum.

You need to use the appropriate LayoutManager that allows the components to grow and shrink as the space available changes.

Read the Swing tutorial on "Using Layout Managers"

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

camickra at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 7

I never knew there was a Swing forum.

Thanks, I'll go there right now.

vopoa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 8

> Thanks, I'll go there right now.

Thats not what I said. I said "in the future"!

Keep the discussion in one place so everybody knows what has already been suggested.

Plus you where given a suggestion for a solution. So take time to read the tutorial and understand how layout managers work.

camickra at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 9

so something like this?

public void content()

{

Container c = getContentPane();

JPanel panel = new JPanel(new BorderLayout());

Font font = new Font("Arial", Font.BOLD, 28);// change font

JComboBox combo = new JComboBox(operations);

combo.setSelectedIndex(0);

combo.setBorder(BorderFactory.createEmptyBorder());

JPanel innerPane = new JPanel(new BorderLayout());

innerPane.setBorder(BorderFactory.createEmptyBorder(40, 10, 40, 10));

innerPane.add(combo, BorderLayout.NORTH);

panel.add(innerPane, BorderLayout.CENTER);

c.add(panel);

}

petes1234a at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 10

yes something like that.

Thanks

vopoa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 11

Is there a way when I put the labels beside the my combobox and JSprings with have the feature of stretching and shrinking, I can't seem to find a way how to do this:

Here is my current code:

package 620524.view;

import java.awt.*;

import java.awt.BorderLayout;

import javax.swing.*;

import javax.swing.border.Border;

public class MyFrame extends JFrame

{

final boolean RIGHT_TO_LEFT = false;

final boolean shouldFill = true;

final boolean shouldWeightX = true;

String[] labels = {"Number 1", "Operation", "Number 2", "="};

String[] operations = {"+", "-", "/", "x"};

public MyFrame(String title)throws HeadlessException

{

super(title);

//content1();

content();

content1();

}

public JSpinner addLabeledSpinner(Container c, String label, SpinnerModel model)

{

JLabel l = new JLabel(label);

c.add(l);

JSpinner spinner = new JSpinner(model);

l.setLabelFor(spinner);

c.add(spinner);

return spinner;

}

public JComboBox addLabeledComboBox(Container c,String label,JComboBox model)

{

JLabel l = new JLabel(label);

c.add(l);

JComboBox combo = new JComboBox(operations);

//combo.setSelectedIndex(0);

l.setLabelFor(combo);

c.add(combo);

return combo;

}

public void content1()

{

Container c = getContentPane();

JPanel panel = new JPanel(new BorderLayout());

Font font = new Font("Arial", Font.BOLD, 28);// change font

JComboBox combo = new JComboBox(operations);

combo.setSelectedIndex(0);

combo.setBorder(BorderFactory.createEmptyBorder());

JPanel innerPane = new JPanel(new BorderLayout());

innerPane.setBorder(BorderFactory.createEmptyBorder(40, 20, 40, 20));

combo = addLabeledComboBox(c, labels[1], combo);

innerPane.add(combo, BorderLayout.NORTH);

panel.add(innerPane, BorderLayout.CENTER);

//panel.add(combo);

c.add(panel);

}

public void content()

{

int zero = 0;

Container con = getContentPane();

JPanel panel = new JPanel();

JFrame frame = new JFrame();

//c.setLayout(new FlowLayout());

Font font = new Font("Arial", Font.BOLD, 28);//change font

//JSpinner

SpinnerModel nums = new SpinnerNumberModel(zero, zero - 100, zero + 100, 1); //initial value,min,max,step

SpinnerModel nums2 = new SpinnerNumberModel(zero, zero - 100, zero + 100, 1);

JSpinner spinner = new JSpinner(nums);

JSpinner spinner2 = new JSpinner(nums2);

//JComboBox

JComboBox combo = new JComboBox();

//JButton

JButton button = new JButton("Calculate");

button.setPreferredSize(new Dimension(100, 30)); //specified button dimension

//JTextArea

JTextArea textArea = new JTextArea(1, 20);

spinner = addLabeledSpinner(con, labels[0], nums);

panel.add(spinner);

combo = addLabeledComboBox(con, labels[1], combo);

panel.add(combo);

spinner2 = addLabeledSpinner(con, labels[2], nums2);

panel.add(spinner2);

panel.add(textArea);

panel.add(button);

con.add(panel);

}

}

vopoa at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...
# 12

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

camickra at 2007-7-29 12:37:37 > top of Java-index,Java Essentials,Java Programming...