• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I show a JButton?  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm just messing around trying to learn about buttons and stuff. I'm not sure what I'm doing wrong but none of the buttons will show below. I tried using .add with the container name but it kept giving me an error. When I just put add.(); it seems fine but then nothing shows. Is there something I'm missing that's required?

 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ryan,

just note that you are dealing with TWO different JFrames.
The first one, FlowLayoutPractice, is the one that you
define in the constructor. However, this contructor is never called,
so you wouldn't see it.

Then, in you main() method, you create yet another JFrame,
called 'practiceFrame', that you do open.

A construction like this is unfortunately not uncommon on this
site.

Secondly: all your buttons are added to the not-created
'FlowLayoutPractice'. Beware that its contentpane, to which
all buttons are added, uses a BorderLayout by default.
Therefore you will only see the last added button.

So, you must think of the following:

1) do you really want two JFrames?
2) if so, give each frame a decent "title", so that you
know with which of the two you are dealing with
3) for the frame to which you add all your buttons, use
a different LayoutManager. FlowLayout would do for a start.
4) make sure your constructor is called, for instance in main().

Now, see if you can implement these items, and if you are
still facing problems, let us know.

Greetz,
Piet
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I'm at work but will play around with it tonight.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

I came up with the below and it produces what I want for the purposes of messing around. Does it look ok or would you think it can be done much better?

Thanks for your help.

Ryan

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the minor grammatical error, that is a very nice SSCCE. It does what you want, showing the buttons. You can add to it, e.g. a different Layout, and change it, and see what happens. Spot on!
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ryan,

yes, this looks fine, I hope you're fine with it too.

I do have a point, but that is not of immediate importace. It has to do
with delegating the size of your frame to the LayoutManagers of all
the panels within such a frame. You will see the ommand 'frame.pack()'
quite regularly.

The technique you use here is the simple 'frame.setSize', that will work for the time being.
But by the time this will not suffice anymore, you'll have gained enough knowledge
and experiece to handle it.

Therefore: well done!

greetz,
Piet

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another comment. I see you getContentPane() and add stuff to it. However the JFrame itself is a container so you don't necessarily need that, just add the buttons to the frame. But you do need to set the layout in your example.

For your info, a JFrame's default layout is BorderLayout. A JPanel's default layout is FlowLayout.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
Piet Souris
Rancher
Posts: 1984
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:(...)

Correct, and OP did this in his opening post.

Since all the frame.add-methods are forwarded to the contentPane's add-methods,
I prefer to use the contentPane version; it makes it much more transparent.

And I often create a JPanel, add all the components to it, and then make it the
contentPane. It makes it (to me, that is), much more clear what I am doing.

Greetz,
Piet
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to rehash an old thread but I'm confused about something. I tried doing something similar to the above using
a JFrame instead of a Container but I can't get the buttons to appear. Am I able to do this with a JFrame or do
I need to use the Container class? (I don't have a goal in mind other than practicing frames/containers.)

Thanks for any advice.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:I tried doing something similar to the above using a JFrame instead of a Container but I can't get the buttons to appear.


You've already been told this:
Piet Souris wrote:Beware that its contentpane, to which all buttons are added, uses a BorderLayout by default. Therefore you will only see the last added button.


Setting a new BorderLayout is also redundant, as you're just replacing one BorderLayout with another.
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ryan,

as Darryl points out, if you just 'add' a button to a contentPane, then this
button will go in the center of the default BorderLayout. And since that
center can only hold one component, you will only see the last one added.
That's why you used a FlowLayout in your updated program.

And you also do exactly the same thing as in your opening post.
You have your class extend JFrame, but in the constructor you create
another JFrame, add all the buttons to that frame, but then you open
the class frame, to which you have nothing added.

I know it is confusing and I guess it'll take a while before one gets used to
this, but if you have your class extending a JFrame (or a JPanel), then
in your constructor, all you have to do is:

or to make that even more specific, write

Using 'this' has also the advantage that, in an IDE, as soon as
you type 'this.', then up pops a large box with all the available methods.
That always saves me a lot of time, by not having to look into the API's.

So, your LindsayFrame constructor might simply look like:

Greetz,
Piet
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Piet Super helpful!

I ordered Swing by Herbert Schildt so hopefully that will help me. This is all pretty new.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!