-
Object Confusion
This is a simple problem to fix I'm sure. I have a class that makes a button and formats it (doesn't do that in my example) and i want to add it to my main Frame class. Here is my code, but it doesn't work. Did i do something wrong, or do i just have a wrong idea of how the object works. Sorry, I'm brand new to java.
Code:
package testframe;
import javax.swing.*;
public class TestFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
makeButton button = new makeButton();
button.addButton("My Button!");
frame.getContentPane().add(button); //<-- this line incorrect
}
}
package testframe;
import javax.swing.*;
public class makeButton {
public void addButton(String name){
JButton button = new JButton(name);
}
}
-
Code:
makeButton button = new makeButton();
Just because you call your variable "button" it doesn't make it a JButton object, it is a makeButton object.
Code:
frame.getContentPane().add(button);
So when you try to do this ^ you are attempting to add the makeButton object to the frame and not a JButton. Only objects that extend Component can be added and your makeButton class does not extend Component.
What you can do instead is have your addButton method (which is a bad name, createButton or generateButton would be better) return the JButton to the main method and then add that to the frame.
By the way if you are only new to Java then you should not be touching GUI's until you have a better grasp of the basics.
-
I meant to say im new to GUI in Java. extends works great. Thanks
-
NOOOOOOOOOOOOOOoooooooooooooooooooooooooooo!
Having your makeButton class extend Component is NOT the correct solution.
-
-
Your makeButton class is not a Component. Just like a Factory is not a Box just because it makes cardboard boxes. When you ran your code did the button appear?
-
it did, but i couldn't format it. I figured it out. does this look better?
Code:
package testframe;
import javax.swing.*;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
makeButton button = new makeButton();
JButton button1 = button.createButton("My Button");
frame.getContentPane().add(BorderLayout.WEST, button1);
}
}
package testframe;
import javax.swing.*;
public class makeButton {
public JButton createButton(String name){
JButton button = new JButton(name);
return button;
}
}
-
i know my variable names aren't the best, but i was just testing. don't worry, in my main project, the names are much better. not just button and button1
-
Now I'm confused. You said "extends works great". Yet the code above does not extend anything. Did you change things or did you just use incorrect terminology?
-
no after you said extends wasn't the correct way i changed it.
-
The most up to date code is fine, but it's a long route to take for such a simple task.
-
It gets more complex, i just made a simple to test. Thanks though!
-
-
Also "makeButton" is more of a method name than a class name. It's not a Thing, it's something that's done.
ButtonFactory would be better, on the assumption that you're doing a load of standard stuff for defining your buttons in there (rather than simply doing new JButton()).