Java Package - To create a button and import one when needed

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

My goal is to create subcategory and create a class with a button that I can import to 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

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/
        // The problem is how to make these two lines work.
        myButts myButton = new myButts();

        myFrame.getContentPane().add(myPanel, BorderLayout.CENTER);

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

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

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 with no errors. But then I compile and it gives me the following error: cannot find symbol symbol : method add(paket.myButts) location: class javax.swing.JPanel myPanel.add(myButton);

Thanks for reading, Z


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.

The error is: 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() {
    // 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.

Need Your Help

extracting text from a href in a website using python and beautlfulsoup

python web-scraping beautifulsoup

I am trying to scrape data from a website and I need the text titles.