wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JFrame/JPanel problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame/JPanel problem" Watch "JFrame/JPanel problem" New topic
Author

JFrame/JPanel problem

Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
ok I have a little problem. Im writing an app for a project at school. When i run the project the panel2 and panel3 over supersized, and i cant figure out why. i tried playing around with it, but cant get the second panel to be the width of field1 or the panel3 to be the width of label1. I set the background color of the two panels to show what i mean. If anyone can give a reason and possibly an answer to fix this, that would be great.

Sincerely,
Chris Dancy

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WriterTest
{
public static void main(String [] args)
{
JFrame f = new JFrame();
f.add(new FrameSetUp());
f.setLocation(300,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
}
}
class FrameSetUp extends JPanel
{
private JPanel panel1,panel2,panel3;
private JTextArea area1,area2;
private JLabel label1;
private JTextField field1;
private JScrollPane s1,s2;
public FrameSetUp()
{
super.setLayout(new GridLayout(3,1));

panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel1.setBorder(BorderFactory.createEtchedBorder());
area1 = new JTextArea(18,30);
area1.setLineWrap(true);
area1.setEditable(false);
s1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
area2 = new JTextArea(18,10);
area2.setEditable(false);
area2.setLineWrap(true);
s2 = new JScrollPane(area2);
panel1.add(s1);
panel1.add(s2);
super.add(panel1);

panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel2.setBackground(Color.red);
field1 = new JTextField(30);
field1.addActionListener(new field1Listener());
panel2.add(field1);
super.add(panel2);

panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel3.setBackground(Color.black);
label1 = new JLabel("over here");
label1.setBackground(Color.red);
label1.setForeground(Color.green);
panel3.add(label1);
super.add(panel3);
}
class field1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
area1.append(String.format("%s\n",field1.getText()));
field1.setText("");
}
}
}


("Anger is not an emotion, its a symptom of fear.")
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Hi Chris,
The issue with the program was that it was using a Grid Layout. The Grid Layout will make all the Panels, added to it, of the same size. Thus you will see that the bottom two panels are also made of the same size as the first one, the biggest.
The solution is to add the three panels to a Box layout and add the Box to the JPanel (FrameSetUp).
I have made a little changes (coz i do not have a jdk5.0) and it seems to work.
If you want this box to resize according to the JFrame size then you may have to set the contentPane of the JFrame as that Box itself.

Hope that helps. If you have further queries please feel free to contact




-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
oh ic ic. I didnt know GridLayout does that. thanks for the feedback, and thanks for the code sample.

Sincerely,
Chris Dancy
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
you are always Welcome!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame/JPanel problem
 
Similar Threads
Regarding panel Revalidate Method
another .ttf question
JPanels with different content should have the same size
ActonListener
How to copy from 4 JTextFields to 1 JTextArea