• 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

Layout problems!!

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have a (hopefully simple) question all I am trying to do is create a single JButton in the bottom center of my GUI but I don't know which layout manager to use. Am I right to assume I need to create a new JPanel (with some sort of layout) then create the JButton?? But how to make it bottom center??

Heres the code I have if it helps any critiquing of my code is most welcome

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//import java.awt.image.*;

public class myFrame extends JFrame implements ActionListener
{
// instance variables - replace the example below with your own
private JPanel pane = new JPanel();
private JPanel pane1 = new JPanel();
private JPanel pane2 = new JPanel(new FlowLayout());
private JPanel pane3 = new JPanel(new FlowLayout());

private JButton button1,button2,button3,button4,button5,button6,
button7,button8,button9,button10,button11,button12,
button13,button14,button15,button16,button17,button18,
button19,button20,button21,button22,button23,button24,
button25,button26;
String rlabels[] = {"$1,000","$1,500","$2,000","$3,000","$5,000",
"$7,500","$10,000","$15,000","$25,000","$50,000","$75,000","$100,000","$200,000"};
String rlabels1[] = {" $0.50 "," $1 "," $2 "," $5 "," $15 "," $25 "
," $50 "," $75 "," $100 "," $150 "," $250 "," $500 "," $750 "};
private JButton[] buttons = {button1,button2,button3,button4,button5,button6,button7
,button8,button9,button10,button11,button12,button13,button14,button15,
button16,button17,button18,button19,button20,button21,button22,
button23,button24,button25,button26};
/**
* Constructor for objects of class myFrame
*/
myFrame()
{
super("My First frame");
setBounds(50,100,600,450);
Container con = this.getContentPane();
pane.setLayout(new GridLayout(13,1,0,0));
pane1.setLayout(new GridLayout(13,1,0,0));

for (int i=0;i<rlabels.length;i++){
JLabel lab = new JLabel(rlabels[i]);
JLabel lab1 = new JLabel(rlabels1[i]);
lab.setForeground(Color.white);lab1.setForeground(Color.white);
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p.setBackground(Color.blue);p1.setBackground(Color.blue);
p.add(lab); p1.add(lab1);
pane.add(p);pane1.add(p1);
}
con.add("East",pane);con.add("West",pane1);
for (int i=0;i<buttons.length;i++){
buttons[i] = new JButton("Briefcase " + (i+1));
pane2.add(buttons[i]);}
pane2.setBackground(Color.white);
con.add(pane2);
// initialise instance variables

}
public void actionPerformed(ActionEvent ae)
{
// put your code here

}
public static void main(String[] args){myFrame app = new myFrame();app.setVisible(true);};
}
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use BorderLayout manager in a JPanel with your button in it and place that JPanel at the bottom of your GUI.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am lazy! Very lazy!


private JButton button1,button2,button3,button4,button5,button6,
button7,button8,button9,button10,button11,button12,
button13,button14,button15,button16,button17,button18,
button19,button20,button21,button22,button23,button24,
button25,button26;

private JButton[] buttons = {button1,button2,button3,button4,button5,button6,button7
,button8,button9,button10,button11,button12,button13,button14,button15,
button16,button17,button18,button19,button20,button21,button22,
button23,button24,button25,button26};


I think I would write
private JButton[] buttons = new JButton[25];

and in my code any reference to say the fifteenth button as
buttons[14] instead of button15 would be ok to me. But, as I said, I am lazy.
 
Paul de Wit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ill do that should have thought of that myself (embarassed)
 
Paul de Wit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Sunny Kumar/anyone else

Thanks for the reply Im sorry dumb question how do you create a JPanel at the bottom of the gui is there a set position or something similar?
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul de Wit:
any critiquing of my code is most welcome



Well button1 and its 25 brothters are initialized
to null and are never set otherwise, so you might
as well just get rid of them. You could just as
well have written
private JButton[] buttons = { null, null, null, ... };
though of course Ko Wey's suggestion is superior.

You use new new GridLayout(13, 1, 0, 0) where I
might have used 0 instead of the 13. That way you don't
have to change your layout code if you change the size of
the rlabels/rlabels1 arrays, but this is hardy a big deal.

Instead of
con.add("East", pane);
con.add("West", pane1);
con.add(pane2);
I would have written
con.add(pane, BorderLayout.EAST);
con.add(pane1, BorderLayout.WEST);
con.add(pane2, BorderLayout.CENTER);

The Container.add(String, Component) method has been
declared "obsolete as of 1.1" though not actually
deprecated. And I think it's better to be explicit
about adding to CENTER, even though your code works fine.

I am trying to do is create a single JButton in the bottom
center of my GUI but I don't know which layout manager to use.



The reason we haven't helped you here is we don't know
exactly what you are asking. There seems to be no attempt
to do this in your code, except for the unused pane3.

Probably easiest is to do something like this
JButton BottomButton = new JButton("bottom button");
pane3.add(BottomButton);
con.add(pane3, BorderLayout.SOUTH);
but perhaps you have already tried that and are unhappy that
your blue p/p1 panes no longer go all the way to the bottom
of the frame?? Please elaborate.
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot indeed guess what your gui should look like: it's hidden in the vaults of your phantasy. Did you try it on paper?


Do your coding more step4step: it will help you to rearrange if things don't suit you. Using comment and indents will keep your witts together.

Perhaps something like this will suit you, although I think the centerbuttons will not be your fancy....


 
Paul de Wit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for your help I appreciate it greatly!! Apologies if my code is a little vague I have been coding with Java for only 6 months.

I have managed to figure out my layout problems by nesting panes inside of panes is there a better way? I am used to languages where you can easily set exactly where you want to place any graphic objects and still trying to get my head around the panel thing.
 
Paul de Wit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yea

Also my code isn't layed out properly because I copied then pasted from bluej and it didn't seem to layout my code the same way. Should have realised the edit box I was pasting into did not recognize the formatting.
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


... nesting panes inside of panes ...




That is the best way, Paul!
reply
    Bookmark Topic Watch Topic
  • New Topic