Java Forum / GUI / September 2005
layouts layouts layouts..
Frances - 02 Sep 2005 14:47 GMT I know I have asked lot about this here, but I'm baffled by how hard this is.. I've been doing layouts w/HTML and css for years, and never have a problem getting my stuff to look exactly as I want it to, even complex layouts.. but Swing layouts is just a different league....;) Michael Dunn was very helpful and patient the other day, he helped me to get my window to look exactly as I want it (w/setResizeWeight()'s... etc..) and it looks great, but now am trying to add both a button at the bottom of window and a menu bar on top and I simply can't get it to work.. this is what I want my window essentially to look like: http://www.francesdelrio.com/java/chatroom.gif (& might need menu at bottom right too, but am not sure right now, I guess I should add empty menu bar there just in case.. but just don't know how to go about this...)
here's code of what I have tried to do plus screen-shots of results: http://www.francesdelrio.com/java/chat.html
I took the trouble to put this simple page together b/c I really need help.. I can't understand why can't set size of button, (btn.setSize() is being ignored..) HOW do you control size of components? (which sometimes you do need, as in case of this button..) I also need to make that menu bar on top "normal" size for menu bars... just how do you do this?????
would appreciate some help, I spent all day yesterday trying to figure this out and am running of out options.. thank you very much... Frances
Thorsten Suckow-Homberg - 02 Sep 2005 15:23 GMT Your code:
> getContentPane().add(menuBar); Take a look at the JRootPane-API, there is a method called "setJMenuBar". Using this method, the RootPaneLayout (inner class of JRootPane) will handle the correct sizing of the MenuBar. You should _not_ add the JMenuBar to the contentPane.
Regarding your button-problem: You should add the button to a JPanel on which you call setLayout(). If you do not care about resizing and centering the button, you could call the method with null as parameter, which gives you the freedom to resize/position elements via Component.setBounds(x, y, width, heigth). Otherwise, the resizing of components is being delegated to the parent's LayoutManager.
Anyway, you should read carefully through the Layout-Api, as it may provide you with the information you need. This should help, too: http://java.sun.com/docs/books/tutorial/uiswing/layout/
HTH
Thorsten Suckow-Homberg
 Signature PHP Consulting http://www.siteartwork.de
Gerlachstraße 8 52064 Aachen 0241 - 4018454
Andrew Thompson - 02 Sep 2005 15:31 GMT > If you do not care about resizing and centering > the button, you could call the method with null as parameter, which gives > you the freedom to resize/position elements via Component.setBounds(x, y, > width, heigth). Until you understand why that approach is so (completely) broken, please refrain from giving further GUI advice.
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info "Tell 'em a hooka smokin' caterpillar, has given you the call.." Jefferson Airplane 'White Rabbit'
Thorsten Suckow-Homberg - 02 Sep 2005 15:38 GMT > Until you understand why that approach is so (completely) > broken, Why is this approach so (completely) broken? http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html
Andrew Thompson - 02 Sep 2005 15:57 GMT >> Until you understand why that approach is so (completely) >> broken, > > Why is this approach so (completely) broken? > http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html Paragraph 1, sentence 2. "A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales."
Once you have the written code to ensure your GUI will 'adjust to' those things mentioned, you have a (custom) layout.
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info "We're gonna' fuss and fight till daylight." Koko Taylor 'Wang Dang Doodle'
Thorsten Suckow-Homberg - 02 Sep 2005 16:02 GMT > Once you have the written code to ensure your GUI > will 'adjust to' those things mentioned, you have > a (custom) layout.
>> If you do not care about resizing and centering the button... A small and simple application doesn't have to be perfect all the time. However, I agree with you if you say that it is the correct way to use a LayoutManager.
Vova Reznik - 02 Sep 2005 16:09 GMT > A small and simple application doesn't have to be perfect all the time. ???
Thorsten Suckow-Homberg - 02 Sep 2005 16:15 GMT > ??? small and simple && app for your on purposes => setBounds(x, y, width, height) often_easier_to_implement_than setLayout(superduperComplexLayoutManagerForWhichIMayHaveToNest_n_JPanels_whichUseLayoutManagersThemself)
That's all I'm saying ;)
Thomas Fritsch - 02 Sep 2005 16:14 GMT Thorsten Suckow-Homberg schrieb:
>>Until you understand why that approach is so (completely) >>broken, > > Why is this approach so (completely) broken? > http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html The answer to this question is given in the tutorial page you gave (especially in its first paragraph). The few contra-examples given there are quite obvious and of a very special type: (*) The JDesktopPane (managing its JInternalFrame's) has a null layout, because the user moves/resizes the frames at will (*) The JSplitPane has a null layout, because it must honour the resize weight between its 2 children. The GUI, Frances wants to build, is certainly not of this type.
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Thorsten Suckow-Homberg - 02 Sep 2005 16:18 GMT > The answer to this question is given in the tutorial page you gave > (especially in its first paragraph). It was a suggestion, no advice. I told him, that if he doesn't care about resizing and re-centering individual components, he should rather use no LayoutManager, because it spares him some time.
Chris Smith - 02 Sep 2005 17:37 GMT > It was a suggestion, no advice. I told him, that if he doesn't care about > resizing and re-centering individual components, he should rather use no > LayoutManager, because it spares him some time. I don't know what distinction you're drawing between "suggestion" and "advice". My thesaurus says they are synonyms. In any case, it was a bad suggestion.
The number of reasons that a layout manager is important extends well beyond resizing and recentering of things. Layout managers help to:
- Support internationalization - Provide accessibility to users with poor eyesight - Run applications well on multiple platforms
Each of those is a HUGE consideration, and has nothing to do with "re" anything. It's just about getting the controls to display properly on the screen. Layout managers provide a way to logically describe what the screen should look like, and have the environment-specific details handled at runtime when those details are known.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Thorsten Suckow-Homberg - 02 Sep 2005 18:17 GMT >[...] Gosh, do people like you even care about looking at the OP you are referring to?
Chris Smith - 02 Sep 2005 20:27 GMT > >[...] > > Gosh, do people like you even care about looking at the OP you are referring > to? Yes. What's your point?
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Frances - 02 Sep 2005 15:42 GMT >>If you do not care about resizing and centering >>the button, you could call the method with null as parameter, which gives [quoted text clipped - 3 lines] > Until you understand why that approach is so (completely) > broken, please refrain from giving further GUI advice. oh my gosh, even you experts disagree.....;)
Chris Smith - 02 Sep 2005 17:41 GMT > > Until you understand why that approach is so (completely) > > broken, please refrain from giving further GUI advice. > > oh my gosh, even you experts disagree.....;) I'd warn you against assuming that anyone who replies on a newsgroup is an expert. That way lies madness. Instead, you should take the advice you get here, and consider and weigh it against three questions: 1) Does it make sense and seem credible from what you already know? 2) What is the general opinion, based on the responses of many people? 3) If you need to look this far, what is the history of each person responding, and have they shown themselves to be knowledgable in the past?
This is USENET. We don't give out "expert" credentials here, and the person who posts the most confident reply is quite likely to be a teenager who knows less than you!
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Frances - 02 Sep 2005 20:14 GMT >>>Until you understand why that approach is so (completely) >>>broken, please refrain from giving further GUI advice. [quoted text clipped - 12 lines] > person who posts the most confident reply is quite likely to be a > teenager who knows less than you! of course, I do realize all that... (but it IS good sometimes to get more than one opinion..) many many thanks to all who helped/responded.. I have a lot to chew on this weekend....:)
Oliver Wong - 02 Sep 2005 17:46 GMT > oh my gosh, even you experts disagree.....;) I believe that experts disagree in all fields (e.g. Math, Science, Religion, Linguistics, Politics, etc.)
- Oliver
Thomas Fritsch - 02 Sep 2005 15:31 GMT [...]
> I also need to make > that menu bar on top "normal" size for menu bars... just how do you do > this????? [...] Menu bars are kind of special in Swing. You don't add a JMenuBar to the contentPane like you do with all other components. Instead you use JFrame's setJMenuBar(JMenuBar) method.
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Vova Reznik - 02 Sep 2005 15:37 GMT > I know I have asked lot about this here, ... Not only about this. Did you try to think/read your self? You cannot add JMenuBar to content pane, only by JFrame::setJMenuBar() It means - you don't need to use any LayoutManager here.
Looking on your picture: Both components (left and right) should have BorderLayout. Left component: - Your status (JLabel or JTextComponent) should occupate NORTH - Your nested spilt pane should occupate CENTER - Your button component (I think it should be JPanel with default FlowLayout and JButton on it) should occupate SOUTH Right component: - Your JScrollPane with JList inside - CENTER (not NORTH!!!) - Your button should occupate SOUTH (Looks like you want button to occupate all available place)
example: left/rightCompnent.add(what?, BorderLayout.CENTER);
Frances - 02 Sep 2005 17:09 GMT >> I know I have asked lot about this here, ... > [quoted text clipped - 13 lines] > - Your button should occupate SOUTH (Looks like you want button to > occupate all available place) no I don't!! that's just how it's coming out, can't figure out how to control size for it (as I said, setSize() is being ignored.. but of course I know I need to do many things differently.. I HAD read about menus being outside main ContentPane, but since had never used menus before, had forgotten about that..) many thanks for yr help.. will certainly try this (I have been wondering, though, about BorderLayout, I don't see WEST in yr post, I've been wondering if it's ok to use BorderLayout if you don't fill all five "corners".. (NORTH, SOUTH, CENTER, EAST, WEST..)
many many thanks for your help..
Andrew Thompson - 02 Sep 2005 17:17 GMT ..
>> - Your button should occupate SOUTH (Looks like you want button to >> occupate all available place) > > no I don't!! that's just how it's coming out, can't figure out how to > control size for it (as I said, setSize() is being ignored.. 'size' is often ignored, while prefered/minimum/maximum are more likely to be honored. Some layouts will ignore even the latter in some circumstances, especially like BorderLayout.CENTER.
> ..(I have been wondering, though, about BorderLayout, I > don't see WEST in yr post, I've been wondering if it's ok to use > BorderLayout if you don't fill all five "corners".. (NORTH, SOUTH, > CENTER, EAST, WEST..) Yes. BorderLayout allows 5 components, but can accept less.
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info "God said to Abraham - 'Kill me a son'.." Bob Dylan 'Highway 62 Revisited'
Frances - 02 Sep 2005 20:16 GMT > .. > [quoted text clipped - 15 lines] > > Yes. BorderLayout allows 5 components, but can accept less. great.. thank you Andrew, I really had been wondering about this... again many thanks Andrew, to you and to all who responded... I got lots to chew on here for the weekend now...
:) Thomas Fritsch - 02 Sep 2005 17:42 GMT Frances schrieb:
> (I have been wondering, though, about BorderLayout, I > don't see WEST in yr post, I've been wondering if it's ok to use > BorderLayout if you don't fill all five "corners".. (NORTH, SOUTH, > CENTER, EAST, WEST..) It is common to have only 2 or 3 components in BorderLayout. (I can't remember having more than 3)
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Chris Smith - 02 Sep 2005 17:47 GMT > > - Your button should occupate SOUTH (Looks like you want button to > > occupate all available place) > > no I don't!! that's just how it's coming out, can't figure out how to > control size for it (as I said, setSize() is being ignored.. but of > course I know I need to do many things differently.. If you don't want the button to occupy the entire south region of a BorderLayout, then you should nest a panel inside. It looks like this:
JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(theButton); mainPanel.add(buttonPanel, BorderLayout.SOUTH);
Of course, you can set any other alignment that you desire on buttonPanel as well... so the button can be aligned left or right.
To set the size of the button, you should change the "preferred" size. However, I don't think you really want to do that. If you do, then you'll need to take into account concerns like font sizes, borders and other look-and-feel specific space requirements, changes in the text of button because of internationalization, etc. Picking a fixed pixel size for a text button is a bad idea; full stop, no exceptions.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Vova Reznik - 02 Sep 2005 18:04 GMT > JPanel buttonPanel = new JPanel(); > buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); FlowLayout is default layout for JPanel and CENTER is default constraint for FlowLayout
> To set the size of the button, you should change the "preferred" size. JButton will create preferred size for it self using its text and Icon and Insets (margin - default 2, 14, 2, 14)
Chris Smith - 02 Sep 2005 20:26 GMT > > JPanel buttonPanel = new JPanel(); > > buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); > > FlowLayout is default layout for JPanel and CENTER is default constraint > for FlowLayout Yes. I set it explicitly in order to more easily point out the possibility of changing the alignment.
> JButton will create preferred size for it self using its text and Icon > and Insets (margin - default 2, 14, 2, 14) The default Insets for a JButton are dependent on the look and feel. Again, the preferred size depends on a lot of things.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Thomas Fritsch - 02 Sep 2005 15:47 GMT > I know I have asked lot about this here, but I'm baffled by how hard > this is.. I've been doing layouts w/HTML and css for years, and never > have a problem getting my stuff to look exactly as I want it to, even > complex layouts.. but Swing layouts is just a different league....;) Well, you are probably right. Here is a course with several rather complex layout examples, including sources: http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html (Actually I'm quite enthusiastic about this course. It must have been drafted by a genius teacher)
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Jim Sculley - 03 Sep 2005 01:50 GMT >> I know I have asked lot about this here, but I'm baffled by how hard >> this is.. I've been doing layouts w/HTML and css for years, and never [quoted text clipped - 8 lines] > (Actually I'm quite enthusiastic about this course. It must have been > drafted by a genius teacher) It was written by Scott Stanchfield (http://javadude.com/). After reading his description of how to design a GridBagLayout GUI on paper, I finally understood how all the GBL constraints worked.
Jim S.
 Signature Remove my extraneous mandibular appendages to reply via email.
Monique Y. Mudama - 07 Sep 2005 23:26 GMT > It was written by Scott Stanchfield (http://javadude.com/). After > reading his description of how to design a GridBagLayout GUI on > paper, I finally understood how all the GBL constraints worked. I actually worked with him at my first job. He's probably the only reason I ever understood layouts. A great guy to bounce design ideas off of, too =)
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Michael Dunn - 02 Sep 2005 21:04 GMT >I know I have asked lot about this here, but I'm baffled by how hard this >is.. I've been doing layouts w/HTML and css for years, and never have a >problem getting my stuff to look exactly as I want it to, even complex >layouts.. but Swing layouts is just a different league....;) <snip>
as mentioned a few times, it's just an understanding of how the different layout managers work, recognizing the default managers for various containers and combining/nesting them to get the desired effect (i.e. lots of trial and error)
You sound like you're trying to understand this, rather than after a handout, so here's the code from your other post, modified to include the buttons and menu. (I find it easier posting the code and having you work it out, rather than trying to explain it all)
import javax.swing.*; import java.awt.*; class Testing extends JFrame { public Testing() { setSize(700,450); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocation(300,100); JTextPane textAreaTop = new JTextPane(); JScrollPane scrollTop = new JScrollPane(textAreaTop,22,31); JTextPane textAreaBottom = new JTextPane(); JScrollPane scrollBottom = new JScrollPane(textAreaBottom,22,31); JSplitPane splitP = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollTop,scrollBottom); splitP.setResizeWeight(0.65);
JPanel leftPanel = new JPanel(new BorderLayout()); JButton btnSend = new JButton("Send"); JPanel leftSouthPanel =new JPanel(); leftSouthPanel.add(btnSend); leftPanel.add(splitP,BorderLayout.CENTER); leftPanel.add(leftSouthPanel,BorderLayout.SOUTH);
JTextPane textAreaRight = new JTextPane(); JScrollPane scrollRight = new JScrollPane(textAreaRight); JPanel rightPanel = new JPanel(new BorderLayout()); JButton btnOnline = new JButton("Online"); rightPanel.add(scrollRight,BorderLayout.CENTER); rightPanel.add(btnOnline,BorderLayout.SOUTH);
JSplitPane splitP2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel); splitP2.setResizeWeight(0.85); getContentPane().add(splitP2);
JMenu menuUser = new JMenu("User"); JMenu menuMembers = new JMenu("Members"); JMenu menuOwner = new JMenu("Owner"); JMenuBar menuBar = new JMenuBar(); menuBar.add(menuUser); menuBar.add(menuMembers); menuBar.add(menuOwner); setJMenuBar(menuBar); } public static void main( String args[] ){new Testing().setVisible(true);} }
Frances - 04 Sep 2005 01:45 GMT >>I know I have asked lot about this here, but I'm baffled by how hard this >>is.. I've been doing layouts w/HTML and css for years, and never have a [quoted text clipped - 33 lines] > > ........ Michael, this is TOTALLY awesome.. truly what I wanted.. I didn't want you to write it all for me, actually, because as you say I do want mainly to understand how it all works (I mean you have to understand all your you use it seems to me....;) but well, this is perfect, I will study your code, I will know how it's done right.... again many many thanks... :)
(Michael, I just realized I responded to you earlier by e-mail by mistake... sorry....)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|