0

what modifiers should I give the variable, label, in this code example below? I am new to Java, but the first thing I wanted to do was break out all the classes I can use in swing like JLabel and make it so I can call them dynamically. I hope I did that below. But yeah I was not sure about private static JLabel label; because ultimately I want to be able to declare all of the swing classes in a master file somewhere. Thanks.

package base;

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class CreateLabel extends JLabel {
    private static final long serialVersionUID = 1L;
    private static JLabel label;

    public Component createLabel(JFrame frame, String text) {
        label = new JLabel(text);
        return frame.getContentPane().add(label);
    }
}
JoJo
  • 4,643
  • 9
  • 42
  • 65
  • just public and static is fine if you're planning on initialising it from another class. You could use a setter method but its rather pointless. – tom Jul 19 '12 at 23:45
  • 3
    Why does `CreateLabel` extend JLabel? It looks like you are confusing inheritance with the Builder pattern – SJuan76 Jul 19 '12 at 23:47
  • 4
    my first questionwould be *why*? And why are you extending JLabel to do this? did you intend to create a factory class? then you'd want createlabel to not extend anything, be final, and the createLabel method would be static. And ideally you'd never hold a reference to it either... – John Gardner Jul 19 '12 at 23:48
  • 2
    Why does `createLabel` need to store it in `CreateLabel`'s `label`? – Steve Kuo Jul 19 '12 at 23:48
  • @ SJuan76, I am not sure why I am doing that, that makes all of the methods of JLABEL available I suppose I wont be needing it. @JohnGardner, yes I want to create a factory class I think, where I can just use this class all the time creating Labels on the fly etc. I will take out extends JLabel in my code. Thanks. – JoJo Jul 19 '12 at 23:51
  • 1
    Ignore @tommo's comment. I see nothing here that should be static other than constants. – Hovercraft Full Of Eels Jul 20 '12 at 00:06
  • @HovercraftFullOfEels i'm pretty sure he edited the question or something because he said he wanted to access it from another class iirc hence the static modifier in his code – tom Jul 20 '12 at 02:14
  • @tommo: that's not a valid reason to make a field static. – Hovercraft Full Of Eels Jul 20 '12 at 02:23

1 Answers1

2

What I understand is, you want to your createLabel() method to add a JLabel to passed JFrame. What you can do is:

public class LabelCreator /*extends JLabel*/ {
    /*private static final long serialVersionUID = 1L;*/
    /*private static JLabel label;*/

    public Component createLabel(JFrame frame, String text) {
        JLabel label = new JLabel(text);
        return frame/*.getContentPane()*/.add(label);
    }
}

Note the change of class name to respect naming convention.

Mohayemin
  • 3,841
  • 4
  • 25
  • 54
  • Thank you this is also working fine. Why are you saying to change the class name from CreateLabel to LabelCreator though? Thanks. – JoJo Jul 20 '12 at 10:25
  • @JoJo: Because class name should be a Noun. Methods should be verb. Also, if it works, you should accept the answer. – Mohayemin Jul 20 '12 at 10:28
  • i'm also not sure why you'd want to pass the frame to the create method. this means that the label always gets added to the center of the content pane, which is pretty much almost never what you actually want. – John Gardner Jul 20 '12 at 16:26
  • @JohnGardner: Because OP's method was like that. – Mohayemin Jul 20 '12 at 17:10
  • You mean I just need to pass the String text and be done with it? Thanks. – JoJo Jul 20 '12 at 18:37
  • 1
    @JoJo, yes. You're mixing a couple things: the creation of a control, and its layout. i presume you're going to want to put more than one control in any frame... – John Gardner Jul 20 '12 at 20:03