• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

panel does not show up

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I am trying the first small code example from Java Head First and unfortunately it does not seem to work respectively does not show a panel, like it is shown in the book.

Do you have any suggestions, what is wrong with the code?

Thank you in advance

Best regards
Marius
Screenshot-(3)_LI.jpg
[Thumbnail for Screenshot-(3)_LI.jpg]
Code and Console
 
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid screen snapshots whenever possible, they are hard to view. Please cut and paste the code text into a post.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing a call to


[edit] I missed the fact that you aren't using Swing but AWT instead.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the AWT library. That is ancient and you shouldn't be using anything less than Swing.
 
Marius Hille
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You are using the AWT library. That is ancient and you shouldn't be using anything less than Swing.



so it is not possible at all to even you the AWT library? or should it work nonetheless just with a work around?
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You are missing a call to
[edit] I missed the fact that you aren't using Swing but AWT instead.


Looks like AWT Frame also uses this method so give it a try. Call it as the last thing you do after coding the layout (i.e. adding buttons, labels, etc.).
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marius Quizmaster wrote:

Carey Brown wrote:You are using the AWT library. That is ancient and you shouldn't be using anything less than Swing.


so it is not possible at all to even you the AWT library? or should it work nonetheless just with a work around?


Yes, you can still use AWT but Swing makes for a more responsive user interface. The designers of Swing went out of their way to make the Swing code as easy as possible for an AWT programmer to transition to. Where AWT has a Button, Swing has JButton. A Frame is now a JFrame. Label, a JLabel, etc..
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Bartender
Posts: 2291
95
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marius Quizmaster wrote:so it is not possible at all to even you the AWT library? or should it work nonetheless just with a work around?



Hi Marius,
As others mentioned, AWT is an ancient technology. In-fact, swing too is quite old, javafx is now used to make desktop based applications.

Here's an example code that is similar to your screenshot:


Please paste your code in code tags instead of screenshot. A screenshot has the disadvantage that someone reading your code cannot run it without typing it all-over again. You are literally asking the person to help you to re-type the code for you. Issues such as misplaced commas, etc. cannot be easily guessed in a screenshot.

 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:As others mentioned, AWT is an ancient technology. In-fact, swing too is quite old, javafx is now used to make desktop based applications.


I need some clarification (from anybody) on the direction Oracle is taking with JavaFX. I sense that JavaFX is somewhat of an orphaned module that will not be getting any updates. I suppose the same could be said of AWT and Swing. I like to think of Swing as "mature", not ancient, though it is pretty old at this point. I also gather (correctly?) that JavaFX will now be a separate download/install from the normal JDK package whereas AWT and Swing are in the "desktop" module of the Java-11 JDK distribution.

The reasons to move from AWT to Swing are compelling, it makes much better use of system resources which results in a more responsive feel. As it matured, Swing ended up with additional capabilities not supported by AWT. AWT is still very much a part of Swing's underpinnings, though now, somewhat hidden from view.

I haven't heard a similar argument for JavaFX though I would certainly invite one from an experienced JavaFX person.
 
Marshal
Posts: 64639
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . JavaFX is somewhat of an orphaned module that will not be getting any updates. . . . JavaFX will now be a separate download/install . . . .

That looks like the pre‑Java8 situation when FX was a separate download; it was included in Java8.
 
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct that JavaFX is not included in the version 11 JDK and has been spun-off into a separate project.  JavaFX has been open source for a while and will be vigorously developed there by companies like Gluon.

It's true that one of the promises of JavaFX was that it could be a desktop and web app has not panned out.  However, Gluon is using JavaFX to create mobile apps that run on Android and iOS.
 
Marius Hille
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:You are missing a call to
[edit] I missed the fact that you aren't using Swing but AWT instead.


Looks like AWT Frame also uses this method so give it a try. Call it as the last thing you do after coding the layout (i.e. adding buttons, labels, etc.).



I tried this but still just getting an empty non-functional frame. So probably just using AWT-technology is not sufficient anymore and I should use JavaFX or Swing, right.



Unbenannt.PNG
[Thumbnail for Unbenannt.PNG]
result
 
salvin francis
Bartender
Posts: 2291
95
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marius Hille wrote:I tried this but still just getting an empty non-functional frame.


Your code still misses the part where you add the panel to your frame, did you try the code I shared ?

Marius Hille wrote:So probably just using AWT-technology is not sufficient anymore and I should use JavaFX or Swing, right.


"sufficient" would probably not be the right word to describe it,  I would say its outdated. I would leave it to others to suggest about Swing vs JavaFx since I do not know much about JavaFx.

As a side note, thanks for sharing your code this time instead of a screenshot of code.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marius Hille
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Your code still misses the part where you add the panel to your frame, did you try the code I shared ?



Yep, I did and though I do not understand it completely yet, it works. So I imagine it like layers or objects to remain in OO-speak resp. Java-speak. First creating some GUI-objects (Label, Button, Panel, Frame, etc.) and secondly putting them all together by using the add()-method considering the hierarchy (a Button-object should be added commonly to a Panel-Object and not directly to a Frame-Object and so on).



salvin francis wrote:"sufficient" would probably not be the right word to describe it,  I would say its outdated. I would leave it to others to suggest about Swing vs JavaFx since I do not know much about JavaFx.



Allright, thanks. I will take a look at both technologies and see what fits best for me .

salvin francis wrote:As a side note, thanks for sharing your code this time instead of a screenshot of code.



Yep, I am kean to follow the rules and learn something, so thank you for your feedback about the "Ranch-Conventions".
 
Knute Snortum
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I would leave it to others to suggest about Swing vs JavaFx since I do not know much about JavaFx.  


I've created a new thread where we can discuss this.
 
salvin francis
Bartender
Posts: 2291
95
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marius Hille wrote:Yep, I did and though I do not understand it completely yet, it works. So I imagine it like layers or objects to remain in OO-speak resp. Java-speak.


Just as a side note, if you are new to java, GUI coding is not the best way to learn. You would end up spending more time getting a component to work the way you want it to and less time understanding how java works.

I think the paradigm of a Panel / Container is common to most UI frameworks. You add a set of components to some lightweight container which acts as a "section" on the screen. This is not specific to java. Even languages like Visual Basic (I used it almost 14 years ago, so my memory might be a bit hazy) had the concept of containers to add your components with and then add that to a main window.

Swing (and AWT) has this cool concept of Layouts which manage the way a component is laid out and resized. Certain containers such as JFrame and JPanel have a default layout (but you can change it)
JFrame's default layout is a BorderLayout. A border layout has 5 areas where you can add a component  : NORTH, SOUTH,EAST,WEST and CENTER.
By default, the add() method adds a component to it's CENTER and the it 'stretches' to fill the full container if there are no components to NORTH, SOUTH, EAST,WEST
Hence, when you add a panel it stretches to the full JFrame, try adding using frame.add(p, BorderLayout.NORTH); and you'll see the difference.

Next, JPanel's default layout is FlowLayout. A FlowLayout does not have a restriction on the number of components. It's behavior is to add components one by one and when there's no more place, wrap it to the next line.

You can change a container's layout using setLayout method. If you want to learn GUI coding in Java, you need to know layouts. You can get away using fully absolute positioning using: setLayout(null); but that's no fun.

I strongly suggest learning core java before diving into GUI coding. Hope this helps.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!