Search code examples
javaswinginterfaceconstructorjtextfield

pass JFrame.getText() from one class member to other?


In this code , I have used 3 classes all of them which extend the JPanel class , whose instances are added to a JFrame in the JForm3 class's constructor.

I am wondering if there's a way to display the text present in the text field(instance of JTextField declared in TextPanel class) in the printTextOnConsole() method in the ButtonPanel class.

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

public class JForm3 
{
    JFrame frame;
    ButtonPanel bP;
    TextPanel tP;
    LabelPanel lP;
    public JForm3()
    {
        frame = new JFrame("Java Window.");
        bP = new ButtonPanel();
        tP = new TextPanel();
        lP = new LabelPanel();

        frame.setSize(500,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(BorderLayout.CENTER,tP);
        frame.getContentPane().add(BorderLayout.EAST,bP);
        frame.getContentPane().add(BorderLayout.WEST,lP);

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args)
    {
        new JForm3();
    }
}
class ButtonPanel extends JPanel implements ActionListener
{
    JButton quitButton;
    JButton printButton;
    public ButtonPanel()
    {
        quitButton = new JButton("Quit");
        printButton = new JButton("Print");

        quitButton.addActionListener(this);
        printButton.addActionListener(this);


        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        this.add(quitButton);
        this.add(printButton);
    }
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == quitButton)
            System.exit(0);
        else
            printTextOnConsole();
    }
    public void printTextOnConsole()
    {

    }
}
class LabelPanel extends JPanel
{
    JLabel label;
    public LabelPanel()
    {
        this.setLayout(new BorderLayout());
        label = new JLabel("Enter Some Text :");
        this.add(BorderLayout.CENTER,label);
        this.setVisible(true);
    }
}
class TextPanel extends JPanel
{
    JTextField textField;
    public TextPanel()
    {
        this.setLayout(new BorderLayout());
        textField = new JTextField("Enter text here");
        this.add(BorderLayout.CENTER,textField);
        this.requestFocus();
        textField.select(0,textField.getText().length());
        this.setVisible(true);
    }

}

Solution

  • Use an interface to loose couple the text functionality between JPanels.

    interface TextRetriever {
       String getText();
    }
    

    Then pass the instance of the TextRetriever (TextPanel) to ButtonPanel

    class ButtonPanel extends JPanel implements ActionListener {
        private TextRetriever textRetriever;
    
        public ButtonPanel(TextRetriever textRetriever) {
           this.textRetriever = textRetriever
           ...
        }
    
        public void printTextOnConsole() {
            String text = textRetriever.getText();
        }
    }