• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFrame/JPanel problem

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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("");
}
}
}
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are always Welcome!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic