• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GUI swing alligning components  RSS feed

 
Ranch Hand
Posts: 536
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys first off happy holidays I am trying to make a gui that emulates a ticket system for a train station I have just created it,but seem to be having two main issues

the first is the background color of the panel west is white and for some reason the background behind the radioButtons seem to be grey,I want everything to be white.

second I am trying to align the textfields to the left of the labels but they are on the far right,I want them group beside each other

here is my code so far

thanks


 
Ranch Hand
Posts: 257
15
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Adam:
Happy Holidays to you.

Actually an interesting question.  It looks like either a bug in Swing, or some kind of discrepancy in handling.  I found I could work around it by setting the background white on the JRadioButtons.

I have a couple of suggestions if you want to grow as a Java developer.  These are suggestions: no doubt there are other Java developers who deliberately don't do some of these things.  If so, it comes down to style.
* The convention in Java is to capitalize all classes.  Call it "Gui" or "GUI" (acronyms all-upper, well known acronyms can just have first letter capitalized).
* It is best to make your member variables "private" so that if your class is used from without, the intended things are used (i.e., methods are called rather than directly changing the fields), and you can make encapsulation work for you.
* When I try to exit the GUI, it is simply continuing to run.  see the code below.



This workaround for the background does not look like something that should be necessary.  Intuitively, if you inherit a white background for one type of component it should happen for all types.  I may take a second look and see if there is a better way or if there is something I've overlooked.
 
L Foster
Ranch Hand
Posts: 257
15
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, again.

I decided to poke around on the web, and try other things.  Here's another workaround:



If you set opacity to false, the background color is inherited.  It seems (just guessing, here) that the whole button hierarchy is meant to have its own foreground by default.  I tried this same thing with a JCheckBox and got the same behavior.  I tried a JButton but that covered the whole area.  Perhaps it is this that causes the problem--that they all derive from the same class which is "button like"?

One way to get around this if you want to make everything act the same way across your whole project might be to have a factory for your "standardized checkboxes" where you call it with a string for the name, and it gives you one already marked as not opaque.

I hope that helps. 
 
Adam Chalkley
Ranch Hand
Posts: 536
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks L

very detailed and superb answer that makes perfect sense

I'm still just learning swing
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!