Help!!! Need help on JPanel
Hi guys,
I am currently using a JPanel with the borderLayout. I want to have the east and west sections of the borderLayout to have a block of a certain size so that the center component can be center correctly when you maximized the window. Any ideas on how I could do that?
Thank you in advance!
: )
:-D One more question. Does anyone know how to minimize/maximize the window browser in java? Do we have access to the browser through an applet?
> to have a block of a certain size
?
You could add a component with a set preferred size to the east and another to the west:
import java.awt.*;
import javax.swing.*;
public class Example implements Runnable {
public void run() {
JFrame f = new JFrame("Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(createBlock(), BorderLayout.EAST);
f.getContentPane().add(createBlock(), BorderLayout.WEST);
f.getContentPane().add(new JScrollPane(new JTextArea(20,50)), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
JComponent createBlock() {
JLabel label = new JLabel();
label.setPreferredSize(new Dimension(100,200));
return label;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Example());
}
}
> > to have a block of a certain size
>
> ?
> You could add a component with a set preferred size
> to the east and another to the west:
> > import java.awt.*;
> import javax.swing.*;
>
> public class Example implements Runnable {
>public void run() {
>JFrame f = new JFrame("Example");
>
> .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> f.getContentPane().add(createBlock(),
> BorderLayout.EAST);
> f.getContentPane().add(createBlock(),
> BorderLayout.WEST);
> f.getContentPane().add(new JScrollPane(new
> JTextArea(20,50)), BorderLayout.CENTER);
>f.pack();
> f.setLocationRelativeTo(null);
>f.setVisible(true);
>
>JComponent createBlock() {
>JLabel label = new JLabel();
>label.setPreferredSize(new Dimension(100,200));
>return label;
> }
>
>public static void main(String[] args) {
>EventQueue.invokeLater(new Example());
> }
> }
>
You can also create a rigid area using createRigidArea(Dimension d)
You're right, Box.createRigidArea is simpler.
> :-D One more question. Does anyone know how to
> minimize/maximize the window browser in java? Do we
> have access to the browser through an applet?
You can't maximize the browser window through Java, but you can in Javascript.
http://www.programmersheaven.com/2/FAQ-JavaScript-Maximize-Browser-Window-To-Screen
But the thing is the op needs to use BorderLayout and not BoxLayout and i missed that point.
@op you can use createRigidArea if you are using BoxLayout.
Anyways i would never use BorderLayout when i have GridBagLayout and SpringLayout but i feel that if we have set setResizable(false) then we can use null layout if we want to perfectly place the components.But however it's always better to use Layout Managers.
> You can't maximize the browser window through Java, but you can in Javascript.I'm not an applet writer, but isn't there also the possibility for applet <-> JavaScript communication?
> I'm not an applet writer, but isn't there also the possibility > for applet <-> JavaScript communication?Indeed, upon futher investigation: http://www.codeproject.com/jscript/javatojs.aspI bow to your superior Java kung-fu. :-)
> But the thing is the op needs to use BorderLayout and
> not BoxLayout and i missed that point.
>
> @op you can use createRigidArea if you are using
> BoxLayout.
The component returned by createRigidArea can be used with any layout manager -- it is a valid component, after all, just with the specified dimension as its min, max and preferred size.
> Anyways i would never use BorderLayout when i haveGridBagLayout and SpringLayout
Okay. No one is trying to start a layout manager war here.
> > But the thing is the op needs to use BorderLayout
> and
> > not BoxLayout and i missed that point.
> >
> > @op you can use createRigidArea if you are using
> > BoxLayout.
>
> The component returned by createRigidArea can be used
> with any layout manager -- it is a valid component,
> after all, just with the specified dimension as its
> min, max and preferred size.
A lightweight container that uses a BoxLayout object as its layout manager. Box provides several class methods that are useful for containers using BoxLayout -- even non-Box containers. Courtesy:JAVA DOCS
> > Anyways i would never use BorderLayout when i
> haveGridBagLayout and SpringLayout
>
> Okay. No one is trying to start a layout manager war
> here.
I just drank a glass of __ .