views:

100

answers:

2

This is more like a package/import test. We'll start with my base folder at .../javaf/test.java

My goal is to create subcategory and create a class with a button that I can import to test.java when I need a button. I feel like I've done it right, I know that the button doesn't do anything as of now, but I just want to make the whole thing work and expand the code thereafter. So here goes - This is test.java

import paket.*;  // importing classes from subcategory paket!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class test {

    public test() {
        JFrame myFrame;
        JPanel myPanel;
        myFrame = new JFrame("Hello FramWorld");
        myPanel = new JPanel();

        // Here I want to add the object created in paket/myButts.java
        // The problem is how to make these two lines work.
        myButts myButton = new myButts();
        myPanel.add(myButton);


        myFrame.setVisible(true);
        myFrame.getContentPane().add(myPanel, BorderLayout.CENTER);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.pack(); 
    }

    public static void main(String args[]) {
        new test();
    }
}

And here is my .../javaf/paket/myButts.java

package paket; // Here is the package function (ought to work like a link)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// This class should only create a button.

public class myButts {  
    public myButts() {
        JButton myButt = new JButton();
    }
}

I've compiled myButts.java with no errors. But then I compile test.java and it gives me the following error:

test.java:19: cannot find symbol symbol : method add(paket.myButts) location: class javax.swing.JPanel myPanel.add(myButton);

Thanks for reading, Z

A: 

I think you want:

public class myButts extends JButton {  

}

If you want to add instances of your class directly to Swing controls, it must extend a Swing or AWT type (in this case JButton).

Before, you just created a local JButton in the constructor, which was unused and inaccessible everywhere else.

As a note, it's best to try to follow the Java style guide, in particular naming conventions. Classes are recommended to be mixed case, e.g. MyButton.

Matthew Flaschen
Thank you mate! So easy and I was so frustrated over this for a whole day! So, whenever I want to use swing controls for package/import I must tell it to extend?
Zopyrus
Yes, if you want your class to *be* a swing control that is the way to do it.
Matthew Flaschen
Understood! I will definitely go through that guide, thank you!
Zopyrus
A: 

The error is:

test.java:19: cannot find symbol symbol : method add(paket.myButts) location: class javax.swing.JPanel myPanel.add(myButton);

It means that the class JPanel doesn't have a method add that takes a paket.myButts parameter. Let's look at the JPanel documentation, to see what add methods it has. There are 5 add methods inherited from Container, and one from Component. This method, like most of them, requires you to pass a Component object. This one requires you to pass a PopupMenu object.

So it seems that your paket.myButts object isn't a Component or a PopupMenu, and needs to be. It might be tempting to make your myButts extend Component. So take a look at the Component documentation, to see what you'll be getting. Notice that there are several subclasses that have already been provided:

Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent

Now since you actually want a button, you might guess it would make sense for myButts to extend Button. That would work, and you'd get an awt.Button. However, if you explore further, some of the subclasses of Container are the swing classes, including JButton. I would recommend using these swing classes.

Once you do that, your constructor should take this form:

public class myButts extends JButton {  
  public myButts() {
    super();
    // here put any myButts-specific construction code, such as:
    setText("Press me!");
  }
}

For a more general introduction to using swing components, I strongly recommend the excellent swing tutorial.

John