I am trying to get the class i added to JPanel and run a function within the class.
I created MyButton
class that extends JButton
, This class i added to JPanel but after i added this class i want to run getText()
on this objects.
I tried this but it does not recognize the function:
panel.getComponent(1).getText();
Main
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(2, 5));
for (int i = 0; i < 10; i++) {
panel.add(new MyButton());
}
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
MyButton
public class MyButton extends JButton {
private String text;
public MyButton()
{
this.text="Hello";
setText("test");
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}