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
    1. Advertising

  2. albert kao

    Ian Shef Guest

    albert kao <> wrote in news:b9646f94-f4f4-43f9-a5bb-
    :

    > 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);
    >


    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
    #2
    1. Advertising

  3. albert kao

    Eric Sosman Guest

    On 3/3/2011 5:04 PM, albert kao wrote:
    > Is there a way to clone/copy JLabel& JComboBox objects?


    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
    d
    Eric Sosman, Mar 4, 2011
    #3
  4. In article
    <>,
    albert kao <> wrote:

    > Is there a way to clone/copy JLabel & JComboBox objects?
    > Is this the only way to clone/copy JLabel?


    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].

    [1]<http://en.wikipedia.org/wiki/Flyweight_pattern>
    [2]<http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editrender>
    [3]<http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Mar 4, 2011
    #4
  5. albert kao

    Roedy Green Guest

    On Thu, 3 Mar 2011 14:04:33 -0800 (PST), albert kao
    <> wrote, quoted or indirectly quoted someone who
    said :

    >The method clone() from the type Object is not visible


    see http://mindprod.com/jgloss/clone.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Refactor early. If you procrastinate, you will have
    even more code to adjust based on the faulty design.
    ..
    Roedy Green, Mar 4, 2011
    #5
  6. albert kao

    Roedy Green Guest

    On Thu, 3 Mar 2011 14:04:33 -0800 (PST), albert kao
    <> wrote, quoted or indirectly quoted someone who
    said :

    >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


    see
    http://mindprod.com/jgloss/compileerrormessages.html#CLONEMETHOD
    http://mindprod.com/jgloss/compileerrormessages.html#CANTCONVERT
    http://mindprod.com/jgloss/clone.html

    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
    http://mindprod.com
    Refactor early. If you procrastinate, you will have
    even more code to adjust based on the faulty design.
    ..
    Roedy Green, Mar 4, 2011
    #6
  7. albert kao

    albert kao Guest

    On Mar 3, 9:05 pm, Eric Sosman <> wrote:
    > On 3/3/2011 5:04 PM, albert kao wrote:
    >
    > > Is there a way to clone/copy JLabel&  JComboBox objects?

    >
    >      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 inthe
    > 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
    >


    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();
    container.setLayout(gbl);

    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");
    container.add(tabPane);
    frame.pack();
    frame.setVisible(true);
    }

    Thanks.
    albert kao, Mar 4, 2011
    #7
  8. albert kao

    Lew Guest

    On Mar 4, 9:36 am, albert kao <> wrote:
    > I want to make the same Swing component (e.g JLabel) appear in
    > different tabs of a JTabbedPane.
    >


    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.

    > I am still experimenting (by cloning JLabel, etc) to make it work.
    >


    That won't make it work.

    --
    Lew
    Lew, Mar 4, 2011
    #8
  9. albert kao

    Ian Shef Guest

    albert kao <> wrote in news:587e8c76-85f3-4539-b920-
    :

    <snip>
    > I want to make the same Swing component (e.g JLabel) appear in
    > different tabs of a JTabbedPane.

    <snip>

    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
    appropriately.

    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
    #9
  10. In article <Xns9E9E82B205D55vaj4088ianshef@138.125.254.103>,
    Ian Shef <> wrote:

    > albert kao <> wrote in news:587e8c76-85f3-4539-b920-
    > :
    >
    > <snip>
    > > I want to make the same Swing component (e.g JLabel) appear in
    > > different tabs of a JTabbedPane.

    > <snip>
    >
    > 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.


    albert kao: More details may be found under "Separable model
    architecture":

    <http://java.sun.com/products/jfc/tsc/articles/architecture/>

    > 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 appropriately.
    >
    > 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).


    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:

    <http://sites.google.com/site/drjohnbmatthews/threadwatch>

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

    <http://download.oracle.com/javase/6/docs/api/javax/swing/event/EventList
    enerList.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Mar 4, 2011
    #10
  11. albert kao

    Eric Sosman Guest

    On 3/4/2011 9:36 AM, albert kao wrote:
    > On Mar 3, 9:05 pm, Eric Sosman<> wrote:
    >> On 3/3/2011 5:04 PM, albert kao wrote:
    >>
    >>> Is there a way to clone/copy JLabel& JComboBox objects?

    >> [...]

    > I want to make the same Swing component (e.g JLabel) appear in
    > different tabs of a JTabbedPane.


    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
    instance.

    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
    d
    Eric Sosman, Mar 5, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Tyler
    Replies:
    28
    Views:
    6,228
    Words13100
    Aug 8, 2010
  2. RC
    Replies:
    2
    Views:
    7,927
    Paul Hamaker
    Feb 22, 2006
  3. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    716
    lordy
    Jul 7, 2006
  4. xz
    Replies:
    16
    Views:
    2,368
  5. Rick Johnson
    Replies:
    0
    Views:
    155
    Rick Johnson
    Jan 16, 2013
Loading...

Share This Page