Sorry if this is another stupid-idiotic questions for you, but I'm still a newbie in Java Programming Language.
I have 3 classes: InputClass, PreviewClass, and MainClass.
MainClass contains the main method to run the program. InputClass contains a private JTextField for input and a JButton for setting a text into the JTextField in the PreviewClass. PreviewClass contains a private JTextField to show the inputted text in the InputClass.
How exactly can I do that (assigning value to JTextField in PreviewClass) without creating an instance of the InputClassand then using getter-method-like to obtain the value it has, or, without making JTextField in the InputClass a static variable so I can access it with some static method?
Just to show you my point, 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); } }