1

I am trying to add a new label to my jFrame at the click of a button and I just want it to be a rectangle with a blue background and no text.

I am running the following code:

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { 
   JLabel lable23 = new JLabel("Loop Label");
   lable23.setLocation(570, 60);
   lable23.setOpaque(true);
   lable23.setBackground(Color.BLUE);
   add(lable23);
   validate();
   repaint();
}  

how ever when i click on the button nothing works. I have tried adding JFrame.add() and JFrame.validate and repaint but this just gives me a syntax error. Any pointers would be great.

Thanks

Tom
  • 51
  • 2
  • 10
  • It would be better to add the label at start-up and change the text, opacity and color on button click. `lable23.setLocation(570, 60);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 26 '16 at 13:42
  • Use custom painting instead, it'd be easier and faster – MadProgrammer Feb 26 '16 at 21:40

1 Answers1

0

try adding a size to your component and it will work

    JLabel lable23 = new JLabel("Loop Label");
    lable23.setLocation(1, 1);
    lable23.setSize(10, 10);
    lable23.setOpaque(true);
    lable23.setBackground(Color.BLUE);
    add(lable23);
    validate();
    repaint();

Of course using the null layout is strongly discouraged. I would suggest you use jPanels to organize your components and use Layout managers to position them on your frame!

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • cheers, get so annoyed at myself when its little things like that i always miss! I have just found Jlabel.setBounds(); would you recommend using this? – Tom Feb 26 '16 at 12:52
  • 2
    @Tom: No! that means that you're using a `null` layout, something that you should studiously avoid. The key here is to understand and use the layout managers to your advantage. – Hovercraft Full Of Eels Feb 26 '16 at 12:55
  • http://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone – MaVRoSCy Feb 26 '16 at 12:56
  • Yes, @HovercraftFullOfEels is totaly right, your approach should be avoided. Try using jPanels to organise your components and use Layout managers to position them on your frame – MaVRoSCy Feb 26 '16 at 12:58
  • @Tom to my understanding, null layout is the only way to ensure your components are arranged in exact pixel to pixel position you want them to be(you will still need to measure relative to what of course). In many situations this is necessary. However, in Java community everyone heavily frowns upon using it, in favor of things that scale with window size. – Dmytro Feb 26 '16 at 12:59
  • 3
    @Dmitry: "pixel perfect" is a fiction. If you use null layouts you guarantee that your GUI will look terrible on all but one platform, and you will create a GUI that is a nightmare to update and debug. – Hovercraft Full Of Eels Feb 26 '16 at 13:00
  • @HovercraftFullOfEels and it is common to design specifically for that platform, or for specific optimal window size. In simpler games you may also want fixed positioning to limit conflict of dealing with doing awt graphics and awt/swing components on the same window. Both are clumsy but if you are not willing to be flexible when these are your only options in the time given, you will just not get your task done. If you have time to research alternative means, you are probably better off using them though, yes. But as I've said, null layout is generally frowned upon. – Dmytro Feb 26 '16 at 13:02
  • 2
    @Dmitry *"it is common to design specifically for that platform"* Not when programming desktop apps with Java. The obvious choice for Windows desktop apps would be .NET, for OS X use the Aqua GUI toolkit, for *nix something else again (but also probably not Java).. 'Platform agnostic' apps is one of Java's greatest strengths. If you toss that out the window, it is hard to see the advantage of sticking with Java for the programming language. – Andrew Thompson Feb 26 '16 at 13:50
  • @AndrewThompson I refuse to argue for the sake of winning. You are right. All I am saying is that taking advantage of null layout for simple draft apps/demos/for very specific fixed apps is beneficial, and downright not using it ever ever is silly. Yes it is a poor practice, and generally bad and frowned upon design, but it can greatly improve productivity for simple apps you only write once that you want to be layed out very rigidly(doing so is hard in java without null layout). Regardless, it is off topic so the discussion is over. – Dmytro Feb 26 '16 at 13:55
  • 1
    @Dmitry Until you meet that boss that thinks there is no such thing as a prototype, only what they can see to their customers...and yes, I've worked for that person before – MadProgrammer Feb 26 '16 at 21:40
  • @MadProgrammer i'm not talking bosses here. I'm talking you'd be surprised how lifechanging it can be to show somebody who doesn't know programming they can move a label with a sprite on it up, down, left, and right on a null layout. Or want to mix awt graphics panel together with a button over it without the layout manager hogging the whole panel/preventing overlap. Yes these are horrible things to do in real code, and there are real solutions that let you do those more elegantly, but while drafting a concept, it really helps to know simplest means to achieve things. – Dmytro Feb 28 '16 at 07:37
  • @Dmitry Good, then demonstrate what the API can do when used properly, not what can be done when you use bad approaches – MadProgrammer Feb 28 '16 at 07:41
  • @MadProgrammer If these alternative approaches were tought more often, and were more popularized/simple to get into, we would not even need to have null layout anymore. Unfortunately learning these alternative means is daunting. As it stands, it is extremely difficult to find answers to how to merge UI elements with graphic elements, and unfortunately the only means to do so that is accessible to beginners is null layout. I've been writing Java for over 7 years and I still don't know solutions to this yet, I guess i'll spend some time right now to try to find them. – Dmytro Feb 28 '16 at 07:52
  • @Dmitry With all due respect, that's a poor excuse to use poor approaches to the problem. Don't wish for things to be easier, wish for you to be better. There are enough poor examples on the internet, we don't need to propagate more. Based on your question, you shouldn't even be using a components, but should be using custom painting directly – MadProgrammer Feb 28 '16 at 07:55
  • @Dmitry Given the limited context of your problem, it's difficult to know what to suggest, for example, you could use a [`JXLayer`/`JLayer`](http://stackoverflow.com/questions/25274566/how-can-i-change-the-highlight-color-of-a-focused-jcombobox/25276658#25276658) to add paint effects, you could use [different layout managers](http://stackoverflow.com/questions/17962171/multiple-jpanels-completely-on-top-of-each-other/17962832#17962832) or [combinations of each](http://stackoverflow.com/questions/14286484/putting-marks-on-imageicon-in-jlabel/14287485#14287485) or the `glassPane` – MadProgrammer Feb 28 '16 at 08:01
  • I've done that approach, and successfully written custom painted components, but it's been really difficult to write a full scale system to manage it. Should everyone be forced to write their own painted components? I mean there's probably libraries that do that but they're not popularized enough. I'll check those out thanks for the links. My point is that people shouldn't be taught "not to use null layout" but what they can use instead of it to accomplish the same result, fixed predictable pixel-based positioning which is often necessary. It's why most people still use bad practices. – Dmytro Feb 28 '16 at 08:02
  • @Dmitry *"My point is that people shouldn't be taught "not to use null layout" but what they can use instead of it to accomplish the same result"* - Yes, *"fixed predictable pixel-based positioning which is often necessary"* - I'd argue that, in my experience, the flow through the UI has been far more important than the physical pixel position, but I don't have the context to your problem to suggest otherwise. – MadProgrammer Feb 28 '16 at 08:26
  • @Dmitry In the very few occasions where I need more control (I can count about 2), I wrote a layout manager to accomplish it, as an [example](http://stackoverflow.com/questions/22394845/space-button-size-on-my-layout-keyboard-wont-resize/22395483#22395483), because of this, the font could be changed, based on the look and feel, the platform or the user, and the layout would continue to hold together – MadProgrammer Feb 28 '16 at 08:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104770/discussion-between-dmitry-and-madprogrammer). – Dmytro Feb 28 '16 at 08:29