473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Java GUI panel size and resizing issues...

TR
Hi All:

I have a simple test GUI that has 2 panels (a left side and a right side),
and each panel is displaying an image. The left panel has a hardcoded size
of (600,400) using the Dimension() function. Likewise, the right panel has a
hardcoded size of 300, 200.

Problem 1: I would think that when the GUI is launched, the image in the
right panel would be twice as small as the image on the left. However, they
are the same size. Does anyone know why this might be? Further, is there a
way to set panel and image widths to be a percentage of the width of the
parent panel? For example, the left side set to 67% of the total parent
panel width, and the right side sett to 33% of the parent panel width.

Problem 2: When the window is resized in the horizontal direction, the
panels and images also resize. However, when the window is resized in the
veritcal direction, the panels and images do not resize, and eventually
become covered up as the window becomes very small. Is there any way to get
the panels and images to resize in both directions as the window is resized?

Thanks in advance for any help. Code follows:

-------------------------------

import java.awt.*;
import javax.swing.*;

public class JPanels extends JFrame {
public static void main(String[] args) {
new JPanels();
}

public JPanels() {
super("Test Window");
Container content = getContentPane();
content.setBackground(Color.lightGray);

JPanel bothsides = new JPanel(new GridLayout(1, 2));

JPanel leftside = new JPanel();
leftside.setPreferredSize(new Dimension(600, 400));
ImageIcon icon1 = new ImageIcon ("sun.jpg");
leftside.add(new JLabel(icon1));

JPanel rightside = new JPanel();
rightside.setPreferredSize(new Dimension(300, 200));
ImageIcon icon2 = new ImageIcon ("venus.jpg");
rightside.add(new JLabel(icon2));

bothsides.add(leftside);
bothsides.add(rightside);

content.add(bothsides);

pack();
setVisible(true);
}
}
Jul 17 '05 #1
2 69218
TR wrote:
Hi All:

I have a simple test GUI that has 2 panels (a left side and a right side),
and each panel is displaying an image. The left panel has a hardcoded size
of (600,400) using the Dimension() function. Likewise, the right panel has a
hardcoded size of 300, 200.

Problem 1: I would think that when the GUI is launched, the image in the
right panel would be twice as small as the image on the left. However, they
are the same size. Does anyone know why this might be? Further, is there a
way to set panel and image widths to be a percentage of the width of the
parent panel? For example, the left side set to 67% of the total parent
panel width, and the right side sett to 33% of the parent panel width.

Problem 2: When the window is resized in the horizontal direction, the
panels and images also resize. However, when the window is resized in the
veritcal direction, the panels and images do not resize, and eventually
become covered up as the window becomes very small. Is there any way to get
the panels and images to resize in both directions as the window is resized?


The problem is that you are using GridLayout. The documentation
GridLayout for GridLayout clearly says that each part of the grid will
be equal size. GridBagLayout supports what you would like to do, but be
forewarned; read the documentation carefully and be prepared to
experiment. GridBagLayout is complicated.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
Try this:

content.setLayout(new BorderLayout);
JPanle mainPnl = new JPanel();

mainPnl.add(new JLabel("<html><img src=\"sun.jpg\" width=x height=y>"));
//If you are fimilier about html then you will understand what I did here.

mainPnl.add(new JLabel("<html><img src=\"moon.jpg\" width=x height=y>"));
//If you are fimilier about html then you will understand what I did here.
You only need to change the width and height of html tag

content.add(mainPnl);
"TR" <t@r.t> wrote in message news:<mb********************@rcn.net>...
Hi All:

I have a simple test GUI that has 2 panels (a left side and a right side),
and each panel is displaying an image. The left panel has a hardcoded size
of (600,400) using the Dimension() function. Likewise, the right panel has a
hardcoded size of 300, 200.

Problem 1: I would think that when the GUI is launched, the image in the
right panel would be twice as small as the image on the left. However, they
are the same size. Does anyone know why this might be? Further, is there a
way to set panel and image widths to be a percentage of the width of the
parent panel? For example, the left side set to 67% of the total parent
panel width, and the right side sett to 33% of the parent panel width. Problem 2: When the window is resized in the horizontal direction, the
panels and images also resize. However, when the window is resized in the
veritcal direction, the panels and images do not resize, and eventually
become covered up as the window becomes very small. Is there any way to get
the panels and images to resize in both directions as the window is resized?

Thanks in advance for any help. Code follows:

-------------------------------

import java.awt.*;
import javax.swing.*;

public class JPanels extends JFrame {
public static void main(String[] args) {
new JPanels();
}

public JPanels() {
super("Test Window");
Container content = getContentPane();
content.setBackground(Color.lightGray);

JPanel bothsides = new JPanel(new GridLayout(1, 2));

JPanel leftside = new JPanel();
leftside.setPreferredSize(new Dimension(600, 400));
ImageIcon icon1 = new ImageIcon ("sun.jpg");
leftside.add(new JLabel(icon1));

JPanel rightside = new JPanel();
rightside.setPreferredSize(new Dimension(300, 200));
ImageIcon icon2 = new ImageIcon ("venus.jpg");
rightside.add(new JLabel(icon2));

bothsides.add(leftside);
bothsides.add(rightside);

content.add(bothsides);

pack();
setVisible(true);
}
}

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Sullivan | last post by:
When we run our Java application the Java heap reports at 80MB but the Windows task manager reports 111MB. What accounts for the difference?
2
by: Arindam Roy | last post by:
Hello, The problem mentioned below has been faced while trying to execute a stored procedure. In this case they have deployed an SP and then trying to make changes to the out parameters but...
3
by: bismarkjoe | last post by:
Hello, I am trying to set the widths on the columns of a DataGrid component, and I'm not sure if I'm doing it correctly. My code is below: //load some inital data table = db.GetDataTable(...
0
by: Jenny | last post by:
Hi, how can I get the vertical size without scrollbars of a panel (div) in JScript? Thanks Jenny
2
by: tjfdownsouth | last post by:
I am using a panel inside an asp.net 2.0 page so that I can have the scrolling capabilities. Inside the panel I have a datagrid that I would like to stay within a certain area. On the initial...
1
by: akhilkes | last post by:
i m working on a dynamic query analyzer , which process queries of user , is it possible to attach components in java panel at run time because i want the program process the query and generates the...
5
by: siri11 | last post by:
Hi !!!!!!!!!!! Can any one please suggest me how to change the panel size (i.e. height and width) at run time (dynamically).Please send the code. Thanks in advance bye
1
by: siri11 | last post by:
hi... Is it possible to change the size and location of a panel(In c#.net-Windows Application) dynamically(at run time).If so plzz send the code required. thanks in advance siri
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.