473,323 Members | 1,550 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,323 software developers and data experts.

setting width JPanel

144 100+
hi,

how to set the width of a JPanel with a GridLayout as the layout manager.

thanks.
May 23 '08 #1
7 6919
JosAH
11,448 Expert 8TB
hi,

how to set the width of a JPanel with a GridLayout as the layout manager.

thanks.
It depends: the total width of the JPanel is partly determined by the prefered
width of the widest component in the GridLayout and the total amount of space
allocated to the JPanel. What do you want to accomplish?

kind regards,

Jos
May 23 '08 #2
thesti
144 100+
well,
the panel seems to be 'wider' . i mean that the components inside each row could still be fit in if the width of the jpanel is smaller.

because it is wider, it looks not so nice, the buttons is wider and there is unused extra space.

so i want to make the jpanel narrower if i could.

thanks
May 23 '08 #3
JosAH
11,448 Expert 8TB
You can make a JPanel smaller of course (just use its setPreferedSize method)
but then another component next to it needs to become wider etc. etc. all the
way up to your JFrame top level container. A GridLayout is probably not the
LayoutManager you want.

kind regards,

Jos
May 23 '08 #4
BigDaddyLH
1,216 Expert 1GB
A GridLayout is probably not the
LayoutManager you want.
Or, if the GridLayout works for the components in that panel, you may need to nest containers and layouts more. Take a look at the layout section of the Swing tutorial:

http://java.sun.com/docs/books/tutor...out/index.html

You can also post the smallest possible sample program that demonstrates your problem. Please don't post all the original code -- most of it will be irrelevant. And please use code tags.
May 23 '08 #5
JosAH
11,448 Expert 8TB
Or, if the GridLayout works for the components in that panel, you may need to nest containers and layouts more.
Yep true; in my village lives a woman; she's an old fashioned book printer. Yes
those people still do exist. I sometimes consult her for my GUIs. She hmms
a bit points with her thumb to the screen and starts off: "we need more rest
in this section, put more white in there" or "this may need a few points of
nothing overhere" or "that doesn't align properly, shame on you". I take notes
and back home I put my carefully crafted panels in yet other panels and add
glue or struts to them. It works fine 'till now ;-)

kind regards,

Jos
May 23 '08 #6
thesti
144 100+
thanks for your willing to help.

i'll post the code that may give you description

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class MainForm extends JFrame {
  3.  
  4.          //these classes are extended from the JPanel
  5.          //and will be placed at the frame
  6.          private Game game;    
  7.          private UserPanel usrPanel1, usrPanel2;
  8.          private DicePanel dicePanel;
  9.          private TidePanel tidePanel;
  10.  
  11.         public MainForm() {
  12.                /*instantiation of the panels
  13.                .....
  14.                */
  15.  
  16.                //right panel container
  17.                JPanel rightPanel = new JPanel(new GridLayout(4, 1, 10, 10));
  18.                   rightPanel.add(usrPanel1);
  19.                   rightPanel.add(usrPanel2);
  20.                   rightPanel.add(dicePanel);
  21.                   rightPanel.add(tidePanel);
  22.  
  23.                //main panel for  the frame
  24.                JPanel mainPanel = new JPanel(new BorderLayout());
  25.                  mainPanel.add(bjackPanel, "North");
  26.                  mainPanel.add(game, "Center");
  27.                  mainPanel.add(rightPanel, "East");
  28.  
  29.                   //add mainPanel to the frame 
  30.                   add(mainPanel);
  31.  
  32.                   //set some property of the frame 
  33.                   setSize(935, 700);
  34.                   setResizable(false);
  35.                   setLocationRelativeTo(null);
  36.                   setVisible(true);
  37.         }
  38. }
  39.  
  40.  
the right panel with 1 column gridLayout, consists of some smaller panels.
it is the width of the rightPanel that i want to resize.

you're right that if i use the setPreferredSize, it will make the panel smaller but not the overall frame.
i've tried to wrap the right panel into another panel, but i just don't know what layout manager should i use for the wrappe panel. it makes the rightPanel looks more terrible.

thanks in advance.
May 24 '08 #7
JosAH
11,448 Expert 8TB
If you get rid of the horizontal gaps you lose 20 pixels already.

kind regards,

Jos
May 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: borgus | last post by:
Can someone tell me if there is a way to invoke an include statement to parse code and set a width percentage in terms of the syntax? thanks -- You heard me, deal with it.
4
by: yb | last post by:
Hi, is the following html markup: <img src="xyz.jpg" width="100" height="100" alt="whatever" /> equivalent to: <img src="xyz.jpg" style="width : 100px; height : 100px;" alt="whatever" />
2
by: TT (Tom Tempelaere) | last post by:
Hi l&g It's weird but I can't find an easy way to set the width of a column in a DataGrid. It must be hidden somewhere. I searched google but it's all vb-ish and not to the point. Can someone...
2
by: Jenny | last post by:
Hi all, I have a page with 3 panels. PanelA at the top, PanelB in the middle and PanelC at the bottom. Can I size PanelA and PanelC to a width of 10 pixel whereas the PanelB in the middle takes...
2
by: Tina | last post by:
I can't set a textbox or label .width = 441 because of a type error. How can I set a width in code? My larger task is to make some headings in textboxes always the same width as the datagrid...
0
by: SteMc | last post by:
I have one browser named "Main_Browser" and a second named "Secondary_Browser". I can set the height and width of the first wonderfully. I have a menu item that when pressed attempts to set the...
50
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
4
by: Karl | last post by:
How do you set the width of a control in the detail section of a report using the On Format event for the detail section?
0
by: Mufasa | last post by:
I know how to set the width of the entire ASP:Menu. Is there anyway to set the width of a specific menu item? TIA - Jeff
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.