Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2004

Tip: Looking for answers? Try searching our database.

Newbie: What's wrong with my code?

Thread view: 
Mike - 20 May 2004 23:11 GMT
I try to create a JTabbedPane object, but it does not work. What is wrong??
Mike

======Codelist.java========
import java.awt.*;
import javax.swing.*;

public class Codelist extends JPanel {
    public JTabbedPane Codelist(String var) {
        JTabbedPane pn = new JTabbedPane();
        JPanel p1 = new JPanel(new GridBagLayout());
            p1.add(new JButton(var));
            pn.addTab("tab1",p1);
        return pn;
    }
    public static void main(String[] args){
        final JFrame frame = new JFrame("test");
        frame.addWindowListener(new WindowAdapter()
            {public void windowClosing(WindowEvent e) {System.exit(0);}});
        JPanel p = new JPanel(new GridBagLayout());
      Codelist c = new Codelist("but1");
        p.add(c);
        frame.getContentPane().add(p);
        frame.pack();
        frame.show();
}
}
Sebastian Scheid - 21 May 2004 02:15 GMT
> I try to create a JTabbedPane object, but it does not work. What is wrong??
> Mike
[quoted text clipped - 5 lines]
> public class Codelist extends JPanel {
> public JTabbedPane Codelist(String var) {

here is your problem. This is not the constructor. A constructor does not
return any value.
public Codelist(String var) {} is what you wanted to do, right?

> JTabbedPane pn = new JTabbedPane();
> JPanel p1 = new JPanel(new GridBagLayout());
[quoted text clipped - 8 lines]
> JPanel p = new JPanel(new GridBagLayout());
>    Codelist c = new Codelist("but1");

you call the default constructor here. On c you could invoke
Codelist("but1") and get what you want. But that is NOT the way you should
do such a thing! See below...

> p.add(c);
> frame.getContentPane().add(p);
> frame.pack();
> frame.show();
> }
> }

Try:

import everything.you.need;

public class TestCodeList {

   // do not subclass Swing components for a work that this method can do
   private static createCodeList(String var) {
       JTabbedPane pn = new JTabbedPane();
       JPanel p1 = new JPanel(new GridBagLayout());
       p1.add(new JButton(var));
       pn.addTab("tab1",p1);
       return pn;
   }

   public static void main(String[] args){
       JFrame frame = new JFrame("test");
       frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
       JPanel p = new JPanel(new GridBagLayout());
       JTabbedPane c = createCodeList("but1");
       p.add(c);
       frame.getContentPane().add(p);
       frame.pack();
       frame.show();
   }
}

Sebastian
Mike - 31 May 2004 21:51 GMT
> > I try to create a JTabbedPane object, but it does not work. What is
>  wrong??
[quoted text clipped - 61 lines]
>     }
> }

OK, thanks that works!
Mike
Roedy Green - 21 May 2004 17:06 GMT
>I try to create a JTabbedPane object, but it does not work.

You get people's backs up when you say "doesn't work".  It sound like
you are playing dumb blond. Be more specific.  Give an error message.
Say what it did and what you wanted it to do.

see http://mindprod.com/jgloss/newsgroups.html

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.