-1

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;
}

}

matan
  • 97
  • 13
  • Does this answer your question? [Java get JPanel Components](https://stackoverflow.com/questions/370310/java-get-jpanel-components) – pafau k. May 22 '20 at 16:50
  • 2
    (1-) You were asked for an [mre] in your last question. As soon as you posted one you got your answer. Where is your "MRE". Every question should have an "MRE". We don't know the context of your code we have no idea why or where you actually invoke `panel.getComponent(1).getText();`. A question should be complete with the "MRE" so we can understand the context. Also, why are you creating a custom class. The JButton already has a `setText(…)` method. Don't reinvent the wheel. – camickr May 22 '20 at 16:55
  • Looks like you haven't casted before calling your custom method. String text = ((MyButton) panel.getComponent(1)).getText(); – Chamil May 22 '20 at 17:00
  • 1
    `MyButton extends JButton` I have yet to see a good reason to extend a button. What is yours? Agree with @camickr re MRE. I won't give most questions serious consideration until the code is compiled in my editor. – Andrew Thompson May 22 '20 at 17:09
  • @AndrewThompson, surprised you wrote an MRE for the OP. Not only has the OP not listened to suggestions to post an MRE with every question, the OP completely ignored the answer you provided in his last question by still using both row/column parameters when creating the GridLayout. – camickr May 22 '20 at 19:09

1 Answers1

0
panel.getComponent(1).getText();

This returns a Component, which has no getText() method. It needs to be cast back to a JButton in order to use that method.

import java.awt.*;
import javax.swing.*;

public class ButtonText {

    public static void main(String[] args) {
        Runnable r = () -> {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel(new GridLayout(2, 5));
            for (int i = 0; i < 10; i++) {
                panel.add(new JButton("Text " + (i+1)));
            }
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            Component[] components = panel.getComponents();
            for (Component component : components) {
                JButton b = (JButton) component;
                System.out.println(b.getText());
            }
        };
        EventQueue.invokeLater(r);
    }
}

BTW - having to trawl back though a panel to get the components seems like a poor hack. See What is the XY problem? Whatever the actual goal is here (& what is that goal?) is likely better served by storing the buttons in an array or list structure, when created.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433