Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > clone/copy JLabel & JComboBox objects

Reply
Thread Tools

clone/copy JLabel & JComboBox objects

 
 
albert kao
Guest
Posts: n/a
 
      03-03-2011
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);
 
Reply With Quote
 
 
 
 
Ian Shef
Guest
Posts: n/a
 
      03-04-2011
albert kao <(E-Mail Removed)> wrote in news:b9646f94-f4f4-43f9-a5bb-
http://www.velocityreviews.com/forums/(E-Mail Removed):

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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-04-2011
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
(E-Mail Removed)d
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      03-04-2011
In article
<(E-Mail Removed)>,
albert kao <(E-Mail Removed)> 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>
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-04-2011
On Thu, 3 Mar 2011 14:04:33 -0800 (PST), albert kao
<(E-Mail Removed)> 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.
..

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-04-2011
On Thu, 3 Mar 2011 14:04:33 -0800 (PST), albert kao
<(E-Mail Removed)> 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/compileer...ml#CLONEMETHOD
http://mindprod.com/jgloss/compileer...ml#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.
..

 
Reply With Quote
 
albert kao
Guest
Posts: n/a
 
      03-04-2011
On Mar 3, 9:05*pm, Eric Sosman <(E-Mail Removed)> 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
> (E-Mail Removed)


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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-04-2011
On Mar 4, 9:36*am, albert kao <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Shef
Guest
Posts: n/a
 
      03-04-2011
albert kao <(E-Mail Removed)> wrote in news:587e8c76-85f3-4539-b920-
(E-Mail Removed):

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


 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      03-04-2011
In article <Xns9E9E82B205D55vaj4088ianshef@138.125.254.103> ,
Ian Shef <(E-Mail Removed)> wrote:

> albert kao <(E-Mail Removed)> wrote in news:587e8c76-85f3-4539-b920-
> (E-Mail Removed):
>
> <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/...vent/EventList
enerList.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Why I can't add a JLabel object into JList/JComboBox? RC Java 2 02-22-2006 01:33 PM
Can JLabel have 2 rows of text? Rpng123 Java 6 10-10-2003 09:35 PM
Objects in a JComboBox Ike Java 3 09-13-2003 05:52 PM
JLabel behaves differently printing than in GUI Cindy Huyser Java 1 08-07-2003 09:53 PM



Advertisments