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 InputClass
and 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); } }
You have any number of possible solutions, all with pros and cons.
If I was facing an issue like this, I would be tempted to create a interface
which would describe what could be changed and how and maybe even provide event notification when the internal state of the interface
changes.
I would then create an instance of this interface
and pass it to each of the classes. Those classes that need values from it, would read the values they need from it and, if available, attach some kind of change/property listener, so that they can monitor changes to the interface
.
Those classes that need to make changes to the interface
would do so as they need to.
As it's changed, the interface
would fire updates letting any one who is listening know that changes have been made.
In this way, you decouple the classes from each and reduce the unnecessary exposure of other classes.
This is the "Model" in the MVC paradigm and describes the observer pattern
You are right to avoid making it static - that would be a bad idea.
The solution here is to pass a reference to your preview class into your input class when you create the input class.
The input class stores that reference and then can do preview.inputRecieved(str)
when the text field changes
Create the inputRecieved
method which can then update the JLabel
and/or do whatever other processing is needed.
Note that this also means that you can change how your preview window displays and organizes itself without having to change the input window. That encapsulation is an important Object Oriented design principle.
Pass the reference of previewClass to the constructor of Inputclass to set the desired value.
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);
}
}
In InputClass, access the parent JFrame and get it's preview object and set the string value:
@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());
}
In PreviewClass (expose the setter method for changing the text):
public void setValue(String s){
namePreview.setText(s);
}
In MainClass (expose the getter method for accessing the Preview panel object):
public PreviewClass getPreviewPanel(){
return previewPanel;
}
You can set InputClass
an abstract
class, override the actionPerformed
method at instantiation and create a setter in your 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());
}
};
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.