clone/copy JLabel & JComboBox objects

Discussion in 'Java' started by albert kao, Mar 3, 2011.

  1. albert kao

    albert kao Guest

    Is there a way to clone/copy JLabel & JComboBox objects?
    Is this the only way to clone/copy JLabel?
    String s = "test";
    JLabel label = new JLabel(s);
    pane.add(label, c);
    JLabel label3 = new JLabel(s);
    pane.add(label3, c);

    The following test code has these compile errors:
    The method clone() from the type Object is not visible
    Type mismatch: cannot convert from Object to JLabel
    String s = "test";
    JLabel label = new JLabel(s);
    pane.add(label, c);
    JLabel label2 = label.clone(); // error
    pane.add(label2, c);
    albert kao, Mar 3, 2011
    1. Advertisements

  2. albert kao

    Ian Shef Guest

    You could duplicate a JLabel via

    pane.add(new JLabel(label.getText()), c) ;

    but of course this is an oversimplification of a true clone.

    Object.clone() is protected and thus not visible by default. A class that
    wants to be cloneable generally overrides clone(), makes clone() public,
    and implements Cloneable. Neither JLabel, its superclass JComponent, its
    superclass Container, its superclass Component, nor (of course) its
    superclass Object do any of these things.

    Furthermore, Object.clone() returns an Object. If this (cloning) was
    provided for JLabel, you would have to cast the result to a JLabel.

    A JLabel is Serializable. It may be possible but not advisable to
    serialize and deserialize a JLabel to create a shallow clone. Also, it
    will be slow. If you choose to use serialization as a pseudoconstructor,
    please lock the doors and close the blinds, I don't want to see what you
    are doing! :)

    Are you certain that you need to clone/copy/duplicate JLabel and JComboBox
    objects? What are you really trying to accomplish?
    Ian Shef, Mar 4, 2011
    1. Advertisements

  3. albert kao

    Eric Sosman Guest

    They cannot be cloned in the sense of clone(). As for copying,
    it depends what you mean by "copy."

    The larger question, I think, is "Why do you want duplicates?"
    For a JLabel I can almost see it: If you've got a UI with lots and
    lots of repetitive text, you might try to re-use "the same" JLabel in
    many different positions simultaneously. But I think it's a rare (and
    over-busy) UI that will clutter itself with fifty occurrences of the
    same label! Just make N JLabels, all with the same text, and be
    prepared for unenthusiastic reviews ...

    Duplicating a JComboBox makes no sense at all to me. Why in the
    world would you want N identical combo boxes, all manipulating the
    exact same ComboBoxModel and hence the same "setting object" -- well,
    I can't think why you'd want this. Please explain.
    Eric Sosman, Mar 4, 2011
  4. As an alternative, consider the flyweight pattern [1], which is used
    throughout Swing in the implementation of list- and table-cell
    renderers and editors [2, 3].

    John B. Matthews, Mar 4, 2011
  5. albert kao

    Roedy Green Guest

    Roedy Green Canadian Mind Products
    Refactor early. If you procrastinate, you will have
    even more code to adjust based on the faulty design.
    Roedy Green, Mar 4, 2011
  6. albert kao

    Roedy Green Guest


    Prior to generics/covariance, clone had to return an Object which you
    had to cast to the correct type. With covariance, you can write a
    clone method that returns the correct type.

    In your particular case, JLabel does not implement a public clone. So
    just use new twice.

    Roedy Green Canadian Mind Products
    Refactor early. If you procrastinate, you will have
    even more code to adjust based on the faulty design.
    Roedy Green, Mar 4, 2011
  7. albert kao

    albert kao Guest

    I want to make the same Swing component (e.g JLabel) appear in
    different tabs of a JTabbedPane.
    e.g. I want to make label4 to appear in both panel1/tab1 and panel2 in
    the following test program.
    I am still experimenting (by cloning JLabel, etc) to make it work.
    Please help.
    public class TestPanel extends JPanel {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    JTabbedPane tabPane = new JTabbedPane();

    GridBagLayout gbl = new GridBagLayout();

    GridBagConstraints c = new GridBagConstraints();

    JPanel panel1 = new JPanel(new GridBagLayout());
    JPanel panel2 = new JPanel(new GridBagLayout());

    String s = "copy";
    JLabel label = new JLabel(s);

    c.gridx = 0;
    c.gridy = 0;
    panel1.add(label, c); // appear in tab 2

    JLabel label2 = new JLabel(label.getText());
    JLabel label3 = new JLabel("label3");
    JLabel label4 = new JLabel("label4");

    c.gridy = 1;
    panel2.add(label3, c);
    c.gridy = 2;
    panel2.add(label, c);
    c.gridy = 3;
    panel2.add(label2, c); // appear in panel2

    c.gridy = 4;
    panel2.add(label4, c); // do not appear in panel2
    panel1.add(label4, c); // appear in panel1

    tabPane.add(panel1, "1");
    tabPane.add(panel2, "2");

    albert kao, Mar 4, 2011
  8. albert kao

    Lew Guest

    Cloning or copying the component is a bad idea.

    Follow the advice upthread vis-à-vis JLabel. For the others, you
    might share a model but don't share the widget.
    That won't make it work.
    Lew, Mar 4, 2011
  9. albert kao

    Ian Shef Guest



    Aha, now I think that I understand what you are after!

    Swing uses (a variant of) an architecture called MVC (Model - View -
    Controller). You can read about it elsewhere, I won't go into details.
    However, unless you have a really good reason to fight against it, it is best
    to work along with it. In your case, here is what it means:

    Data exists in one place (the Model), and there are one or more ways to view
    the data (the View).

    For your JLabel in this case, you have data in a String s. You have multiple
    JLabel_s that you are going to create, based on the same s. Just use
    new JLabel(s) as many times as needed and you will be fine.

    Similarly for your JComboBox, there is a model of class ComboBoxModel. The
    ComboBoxModel holds your data, and each JComboBox should be constructed using
    this model. If your data changes, update the model. All of the listeners
    (the various JComboBox_es) will be notified of the change and will display

    One issue with the JLabel: if s changes, the JLabel_s don't automatically
    get updated. You have to do this yourself, or perhaps you should be using a
    JTextField (and its associated model, a Document).

    Good luck!
    Ian Shef, Mar 4, 2011
  10. albert kao: More details may be found under "Separable model

    albert kao: For modeling your labels' content, I see a couple of choices
    for event handling:

    1) Have your model extend Observable, and let your view implement
    Observer, as suggested here:


    2) Create you own event type using the EventListenerList contained by
    every JComponent:

    John B. Matthews, Mar 4, 2011
  11. albert kao

    Eric Sosman Guest

    Pfui: Use `new'. Your JLabels and so on are not "the same"
    component at all, since they have (or can have) different positions
    and even different sizes in each tab. Just make multiple JLabels
    with the same text/font/color/whatnot. For the JComboboxes, make
    independent JComboBox instances that use the same ComboBoxModel

    Don't imagine that two visual gadgets with the same appearance
    are somehow the same gadget. Ever met any sets of "identical" twins?
    WYSINATI = What You See Is Not All There Is.
    Eric Sosman, Mar 5, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.