clone/copy JLabel & JComboBox objects

A

albert kao

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

Ian Shef

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?
 
E

Eric Sosman

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

John B. Matthews

albert kao said:
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>
 
R

Roedy Green

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

albert kao

     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.

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

Lew

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

Ian Shef

(e-mail address removed):

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!
 
J

John B. Matthews

Ian Shef said:
(e-mail address removed):


<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":

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

Eric Sosman

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top