How to change or assign a value to a private variable JTextField from another class?

Sorry if this is another stupid idiotic question for you, but I'm still new to the Java programming language.

I have 3 classes: InputClass, PreviewClassand MainClass.

MainClasscontains main methodto run the program. InputClasscontains private JTextFieldfor input and a JButtonfor setting text in JTextFieldc PreviewClass. PreviewClasscontains closed JTextFieldto display the entered text in InputClass.

How exactly can I do this (by assigning a value JTextFieldto PreviewClass) without creating an instance InputClass, and then using getter-method-liketo get the value that it has, or without creating JTextFieldin the variable InputClassa staticso that I can access it with some static method?

Just to show you your thought, here is the code:

  • Inputclass

     import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*;
    
     public class InputClass extends JPanel implements ActionListener{
    
         private JTextField inputName;
         private JButton inputButton;
    
         public InputClass() {
             setLayout(new FlowLayout());
    
             inputName=new JTextField(15);
             inputButton=new JButton("INPUT");
             inputButton.addActionListener(this);
    
             add(inputName);
             add(inputButton);
         }
    
         @Override
         public void actionPerformed(ActionEvent event) {
             // How do I change/assign a text to the PreviewClass from here?
         }
    
     }
    
  • PreviewClass

     import javax.swing.*;
     import java.awt.*;
    
     public class PreviewClass extends JPanel{
         private JTextField namePreview;
    
         public PreviewClass() {
              setLayout(new FlowLayout());
    
              namePreview=new JTextField(15);
              namePreview.setEditable(false);
    
              add(namePreview);
         }
    
     }
    
  • Mainclass

    import javax.swing.*;
    import java.awt.*;
    
    public class MainClass extends JFrame{
        private static final int FRAME_WIDTH=250;
        private static final int FRAME_HEIGHT=150;
        private static final int FRAME_X_ORIGIN=400;
        private static final int FRAME_Y_ORIGIN=300;
    
        private InputClass inputPanel;
        private PreviewClass previewPanel;
    
        private JTabbedPane tabbedPane;
        private Container contentPane;
    
        public MainClass() {
            contentPane=getContentPane();
            contentPane.setLayout(new BorderLayout());
    
            setTitle("How to Assign Value from Another Class");
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    
            inputPanel=new InputClass();
            previewPanel=new PreviewClass();
    
            tabbedPane=new JTabbedPane();
            tabbedPane.add("Input Name", inputPanel);
            tabbedPane.add("Preview Name", previewPanel);
    
            contentPane.add(tabbedPane, BorderLayout.CENTER);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            MainClass frame=new MainClass();
            frame.setVisible(true);
        }
    }
    
+4
source share
5 answers

You have several possible solutions, all with pluses and minuses.

, interface, , , , , , interface.

interface . , , , , , , - /, interface.

, interface, .

, interface , , .

.

"" MVC

+2

, - .

.

, preview.inputRecieved(str)

inputRecieved, JLabel / .

, , , . encapsulation - - .

0

previewClass Inputclass, .

InputClass

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

public class InputClass extends JPanel implements ActionListener{

    private JTextField inputName;
    private JButton inputButton;

    public InputClass(final PreviewClass perviewClassObj) {
        setLayout(new FlowLayout());

        inputName=new JTextField(15);
        inputButton=new JButton("INPUT");
        inputButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                perviewClassObj.setNamePreview(inputName.getText());
            }
        });

        add(inputName);
        add(inputButton);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        // How do I change/assign a text to the PreviewClass from here?
    }

}

PreviewClass

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

public class PreviewClass extends JPanel{
    private JTextField namePreview;

    /**
     * @param namePreview the namePreview to set
     */
    public void setNamePreview(String textContent) {
        this.namePreview.setText(textContent);
    }

    public PreviewClass() {
         setLayout(new FlowLayout());

         namePreview=new JTextField(15);
         namePreview.setEditable(false);

         add(namePreview);
    }

}

MainClass

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

public class MainClass extends JFrame{
    private static final int FRAME_WIDTH=250;
    private static final int FRAME_HEIGHT=150;
    private static final int FRAME_X_ORIGIN=400;
    private static final int FRAME_Y_ORIGIN=300;

    private InputClass inputPanel;
    private PreviewClass previewPanel;

    private JTabbedPane tabbedPane;
    private Container contentPane;

    public MainClass() {
        contentPane=getContentPane();
        contentPane.setLayout(new BorderLayout());

        setTitle("How to Assign Value from Another Class");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

        previewPanel=new PreviewClass();
        inputPanel=new InputClass(previewPanel);

        tabbedPane=new JTabbedPane();
        tabbedPane.add("Input Name", inputPanel);
        tabbedPane.add("Preview Name", previewPanel);

        contentPane.add(tabbedPane, BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        MainClass frame=new MainClass();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
0

InputClass JFrame :

@Override
public void actionPerformed(ActionEvent event) {
    // How do I change/assign a text to the PreviewClass from here?
    ((MainClass)SwingUtilities.getWindowAncestor(this)).getPreviewPanel().setValue(inputName.getText());
}

PreviewClass ( setter ):

public void setValue(String s){
    namePreview.setText(s);
}

MainClass ( getter ):

public PreviewClass getPreviewPanel(){
    return previewPanel;
}
0

InputClass a abstract, actionPerformed PreviewClass:

PreviewClass

public class PreviewClass extends JPanel{

    ...

    public void setNamePreview(String name) {
        inputName.setText(name);
    }
}

InputClass

public abstract class InputClass extends JPanel implements ActionListener{

    private JTextField inputName;
    private JButton inputButton;

    public InputClass() {
        setLayout(new FlowLayout());

        inputName=new JTextField(15);
        inputButton=new JButton("INPUT");
        inputButton.addActionListener(this);

        add(inputName);
        add(inputButton);
    }

    public String getInputNameValue(){
        return inputName.getText();
    }
}

MainClass

    ...

    previewPanel=new PreviewClass();
    inputPanel=new InputClass() {
        @Override
        public void actionPerformed(ActionEvent event) {
             previewPanel.setNamePreview(inputPanel.getInputNameValue());
        }
    };

    ...
0

All Articles