Home arrow static arrow Java Programming [Archive] - OOP question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - OOP question
This topic has 30 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:44
Registered: 7/18/04
OOP question  
Jul 22, 2004 11:41 AM



 
I want to create a pre-built JPanel so that I just have to create an instance of it and attach it to the content pane of my application.

Whats the correct way to do this?
I was going to extend JPanel in a new class but I don't have access to the constructors.

Novice Programmer.
 

Posts:65
Registered: 02-05-22
Re: OOP question  
Jul 22, 2004 11:44 AM (reply 1 of 30)



 
What do you mean by pre-built?
 

Posts:89
Registered: 12/3/03
Re: OOP question  
Jul 22, 2004 11:44 AM (reply 2 of 30)



 
I've done this by extending JPanel. What do you mean you don't have access to the constructors?
 

Posts:24,036
Registered: 2/3/03
Re: OOP question  
Jul 22, 2004 11:45 AM (reply 3 of 30)



 
Whats the correct way to do this?
I was going to extend JPanel in a new class but I
don't have access to the constructors.

Sure you do. JPanel has public constructors, including a default, no-arg constructor.

You may also want to consider the Builder design pattern.
 

Posts:957
Registered: 3/31/04
Re: OOP question  
Jul 22, 2004 11:46 AM (reply 4 of 30)



 
Kinda vague without knowing more about your app....
one way might be to write a method that builds and returns you JPanel, then call the method.

private JPanel buildJPanel(){     JPanel jp = new JPanel();    jp.add(new Label("specialized stuff"));    return jp; }
 

Posts:44
Registered: 7/18/04
Re: OOP question  
Jul 22, 2004 11:47 AM (reply 5 of 30)



 
I was thinking of creating my own constructor, don't I need to know whats in the standard JPanel constructor?
 

Posts:349
Registered: 1/8/04
Re: OOP question  
Jul 22, 2004 11:47 AM (reply 6 of 30)



 
What do you mean you don't have access to the constructors? The constructor of JPanel? Your extension of JPanel?

Just to make sure you know, you can do this:
public class MyPanel extends JPanel {    // My constructor    public MyPanel() {        super("JPanel title"); // call JPanel constructor        this.doSomethingToMyPanel();        ...    }}
 

Posts:44
Registered: 7/18/04
Re: OOP question  
Jul 22, 2004 11:48 AM (reply 7 of 30)



 
What is the Builder design pattern?
 

Posts:24,036
Registered: 2/3/03
Re: OOP question  
Jul 22, 2004 11:48 AM (reply 8 of 30)



 
I was thinking of creating my own constructor, don't I
need to know whats in the standard JPanel constructor?

It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of [url=http://java.sun.com/reference/api/index.html]documentation[/url] that you can even [url=http://java.sun.com/docs/index.html]download[/url] for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

http://java.sun.com/reference/api/index.html
http://java.sun.com/j2se/1.4.2/docs/api/
 

Posts:24,036
Registered: 2/3/03
Re: OOP question  
Jul 22, 2004 11:49 AM (reply 9 of 30)



 
What is the Builder design pattern?

http://home.earthlink.net/~huston2/dp/builder.html
 

Posts:21,719
Registered: 98-02-20
Re: OOP question  
Jul 22, 2004 11:49 AM (reply 10 of 30)



 
I'm not following you here, Thomas. My javadocs say JPanel has four public constructors, and you can call whichever one you like:

JPanel()
Creates a new JPanel with a double buffer and a flow layout.
JPanel(boolean isDoubleBuffered)
Creates a new JPanel with FlowLayout and the specified buffering strategy.
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager
JPanel(LayoutManager layout, boolean isDoubleBuffered)
Creates a new JPanel with the specified layout manager and buffering strategy.

%
 

Posts:957
Registered: 3/31/04
Re: OOP question  
Jul 22, 2004 11:50 AM (reply 11 of 30)



 
Yes
To Yawmark and scrotch
you listen

Then you instantiate ie

MyPanel p1 = new MyPanel();MyPanel p2 = new MyPanel();MyPanel p3 = new MyPanel();


She's a real beooooot that Java
 

Posts:44
Registered: 7/18/04
Re: OOP question  
Jul 22, 2004 11:50 AM (reply 12 of 30)



 
Thanks scrotch I didn't know I could call the constructor like that, "super("JPanel title"); // call JPanel constructor"

I mistakenly thought I had to duplicate the code contained in the default constructor.
 

Posts:65
Registered: 02-05-22
Re: OOP question  
Jul 22, 2004 11:51 AM (reply 13 of 30)



 
Duplicate code ... c'mon, that never happens.
 

Posts:37,103
Registered: 3/30/99
Re: OOP question  
Jul 22, 2004 11:52 AM (reply 14 of 30)



 
Thanks scrotch I didn't know I could call the
constructor like that, "super("JPanel title"); // call
JPanel constructor"

I mistakenly thought I had to duplicate the code
contained in the default constructor.

In that case, you probably ought to go through one of more of these:

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)
Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]
Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].
 
This topic has 30 replies on 3 pages.    1 | 2 | 3 | Next »