Re: Newbie: What's wrong with my code?

From: Sebastian Scheid (sebastian_scheid_at_web.de)
Date: 05/21/04


Date: Fri, 21 May 2004 03:15:53 +0200


"Mike" <mike.bosschaert@hccnet.nl> schrieb im Newsbeitrag
news:619df627.0405201411.3174ab98@posting.google.com...
> 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) {

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());
> 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");

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



Relevant Pages

  • Proof of Randomness
    ... public class HelloWorld { ... public static void main{ ... Object) and initializes it by calling the constructor. ... the interpreter skips to "catch" to see what it should do. ...
    (sci.math)
  • Re: what is the RIGHT THING to make a constructor of a subclass, using super()
    ... If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to superfrom your constructor. ... public class TestDerived extends TestBase ... public Customer(String firstName, String lastName) { ... public class SlashSeparatedCustomer extends Customer { ...
    (comp.lang.java.programmer)
  • Re: Do some computation before calling super constructor.
    ... I want to do some computation in my constructor before invoking the ... Now for some manipulations of automata, ... public class StateSet extends State { ...
    (comp.lang.java.help)
  • RE: Generics - A question on generics - delegates - runtime binding.
    ... public class Foowhere T: ... turn we promise that whenever we instantiate a Foo we will pass a T that ... > public static int GetValue ... > public static float GetValue(Avar) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: getting used to Java - question about "style"
    ... > "Subclasses may override nonfinal methods and Java will dispatch a call to ... > before executing the derived class constructors. ... > constructor." ... public class B extends A ...
    (comp.lang.java.programmer)