Troubles with clone  
Author Message
Prosper





PostPosted: 2004-2-25 22:22:00 Top

java-programmer, Troubles with clone Hello

i'm having troubles with the clone function of
object. I can't clone a JPanel or a JScrollPane.


import javax.swing.*;
import java.lang.*;

public class test extends javax.swing.JApplet {

public void init() {
JScrollPane scrollptmp = new JScrollPane();
JPanel paneltmp = new JPanel();

try {
JScrollPane sptmp = (JScrollPane)scrollptmp.clone();
JPanel ptmp = (JPanel)paneltmp.clone();
} catch (Exception e) {
}
}
}

it won't compile with this error:

test.java [29:1] clone() has protected access in
java.lang.Object
JScrollPane ptmp =
(JScrollPane)paneltmp.clone();
 
Barry White





PostPosted: 2004-2-25 22:58:00 Top

java-programmer >> Troubles with clone It is not given that you can clone all Java objects. A class must
implement the Cloneable interface and the developer must decide what
cloning an object of that class should result in.

Look at the Object.clone() and Cloneable API docs.

Barry

Prosper wrote:

> Hello
>
> i'm having troubles with the clone function of object. I can't clone a
> JPanel or a JScrollPane.
>
>
> import javax.swing.*;
> import java.lang.*;
>
> public class test extends javax.swing.JApplet {
>
> public void init() {
> JScrollPane scrollptmp = new JScrollPane();
> JPanel paneltmp = new JPanel();
>
> try {
> JScrollPane sptmp = (JScrollPane)scrollptmp.clone();
> JPanel ptmp = (JPanel)paneltmp.clone();
> } catch (Exception e) {
> }
> }
> }
>
> it won't compile with this error:
>
> test.java [29:1] clone() has protected access in java.lang.Object
> JScrollPane ptmp = (JScrollPane)paneltmp.clone();
 
Prosper





PostPosted: 2004-2-25 23:08:00 Top

java-programmer >> Troubles with clone Barry White wrote:

> It is not given that you can clone all Java objects. A class must
> implement the Cloneable interface and the developer must decide what
> cloning an object of that class should result in.
>
> Look at the Object.clone() and Cloneable API docs.
>
> Barry
>

okay


is there a way to make all GUI components look the
same style?
(like a CSS in HTML)

I think i found something called setUI, hope i'm
on the right track this time...

 
 
Barry White





PostPosted: 2004-2-25 23:19:00 Top

java-programmer >> Troubles with clone Hi again,

i depends what you mean by style. They should be all using the same look
& feel by default (or you have got problems). Look and Feel as in -
Windows L&F, Motif L&F, MacOS L&F, etc.

You can create your own look and feel but from what I've read that's no
small task. I recommend you extend Swing components and give them the
style that you want. If you want all your text boxes to have the same
style (semi-pseudo code):

public class MyTextField extends JTextField {
public MyTextField() {
setFont(...);
setMargins(...);
setColors(...);
}
}

Barry

Prosper wrote:

> Barry White wrote:
>
>> It is not given that you can clone all Java objects. A class must
>> implement the Cloneable interface and the developer must decide what
>> cloning an object of that class should result in.
>>
>> Look at the Object.clone() and Cloneable API docs.
>>
>> Barry
>>
>
> okay
>
>
> is there a way to make all GUI components look the same style?
> (like a CSS in HTML)
>
> I think i found something called setUI, hope i'm on the right track this
> time...
>
 
 
Thomas Weidenfeller





PostPosted: 2004-2-26 0:18:00 Top

java-programmer >> Troubles with clone Prosper wrote:
> is there a way to make all GUI components look the same style?
> (like a CSS in HTML)

Depends on what you mean with "look the same style". If you use Metal,
you maybe want to have a look at Themes. Or you change the UIDefaults
for your PLAF via the UIManager. Or you load another PLAF. Or you
subclass the components you want, change their default settings in the
constructor, and only use the subclasses (not a good idea).

We can also discus if it is really good idea to change the defaults
(maybe to something that looks good on your machine and platform, but
bad and ugly on your customer's computers). We can also discuss if you
shouldn't better leave some things like aligning sizes to layout
managers (you should).

> I think i found something called setUI, hope i'm on the right track this
> time...

You are well advised to read a little bit about the fundamentals of the
Java GUI framework. Have a look at the TSC architecture articles.
Pointers are in the FAQ for the comp.lang.java.gui group.

Digging in the mud, dragging out some random method and then hoping it
does what you mean is painful and a waste of time.

/Thomas