Reputation: 127
I had a problem, im currently in learning process of java swing/awt
I was creating a small program, with 2 jtextfields and 1 jbutton
I used 2 classes, 1 was for GUI and other was for LOGIC/IMPLEMENTATIONS
I declared a button in class1 and applied an actionlistener to it, in class2(extends class1) i created a method that was doing this comparison
MaisamCustom is my class1 and LogicClass is class2
this screen shot is from MaisamCustom(Class1), the button in which this method is being called
Now the real problem, when ever I entered 2 different/same values and press the button, it was displaying the message in IF statement, else was not working
BOTH FIELDS MATCH !!!
so i googled it, after hours of search i found an answer on stackoverflow
this guy called @Azuu answered a similiar question with ease
--> Get value from JPanel textfield in another class
so i did the same thing with my JTEXTFIELDS objects declaration and IT WORKED !! :')
I'm happy , and really want to thank this guy ( @Azuu ).
now the problem
I know what static does and what is public
but how did my program started to work perfect by just making jtextfields public static
can anyone explain this to me please :)
and yes, here is the code ( its really messed up because I'm experimenting different aspects of GUI on it )
package gui.examples;
import java.awt.event.*;
import javax.swing.*;
public class MaisamCustom {
JFrame frame = new JFrame("My Desktop App");
JPanel panel = new JPanel();
public static JTextField txt1 = new JTextField(8),
txt2 = new JTextField(8);
JButton enter_btn = new JButton("Enter");
public void launchFrame() {
JLabel label1 = new JLabel(" "),
label3 = new JLabel(" "),
label4 = new JLabel(" "),
label5 = new JLabel(" "),
label2 = new JLabel(" My Comparision Program");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(label3);
panel.add(label2);
panel.add(label4);
panel.add(txt1);
panel.add(label1);
panel.add(txt2);
panel.add(label5);
enter_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LogicClass obj = new LogicClass();
obj.enterButton();
}
});
panel.add(enter_btn);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MaisamCustom obj = new MaisamCustom();
try {
obj.launchFrame();
} catch (Exception ex) {
System.out.println("Some issue while launching the application...");
}
}
}
package gui.examples;
import javax.swing.JOptionPane;
public class LogicClass extends MaisamCustom {
public void info(String message, String title) {
JOptionPane.showMessageDialog(null, message, "PromptBox: " + title, JOptionPane.INFORMATION_MESSAGE);
}
public void enterButton() {
LogicClass obj = new LogicClass();
if (txt1.getText().equals(txt2.getText())) {
obj.info("Both Fields Match !!!", "Note !!");
} else {
obj.info("Both Fields Do Not Match !!!", "Note !!");
}
}
}
im really sorry , my question is so simple but i have to explain it into detail so that is what making it so long, i actually love to explain my problems step by step and with details
i posted a question in same style months ago ( sometimes users get rude and they scold you really hard so this all explanations are just to prevent that )
Upvotes: 2
Views: 1906
Reputation: 2771
About public
protected
would work just as well. it just needs to be visible from your LogicClass.
About static:
Each time you use the enter button, you create a new LogicClass object. If txt1
and txt2
were not static, you would create fresh JTextField
s with no text. Both fields would always match. They would not be the same fields as the fields in the dialog.
Now that you have made the fields static, you keep using the original fields that you first created in the MaisamCustom object; the actual fields in the dialog.
With public static
you can simplify your program a bit: LogicClass doesn't need to extend from MaisamCustom:
package gui.examples;
import javax.swing.JOptionPane;
public class LogicClass {
public void info(String message, String title) {
JOptionPane.showMessageDialog(null, message, "PromptBox: " + title, JOptionPane.INFORMATION_MESSAGE);
}
public void enterButton() {
LogicClass obj = new LogicClass();
if (MaisamCustom.txt1.getText().equals(MaisamCustom.txt2.getText())) {
obj.info("Both Fields Match !!!", "Note !!");
} else {
obj.info("Both Fields Do Not Match !!!", "Note !!");
}
}
}
The alternative would be to make explicit use of the extended class:
package gui.examples;
import java.awt.event.*;
import javax.swing.*;
public class MaisamCustom {
JFrame frame = new JFrame("My Desktop App");
JPanel panel = new JPanel();
protected JTextField txt1 = new JTextField(8),
txt2 = new JTextField(8);
JButton enter_btn = new JButton("Enter");
public void launchFrame() {
JLabel label1 = new JLabel(" "),
label3 = new JLabel(" "),
label4 = new JLabel(" "),
label5 = new JLabel(" "),
label2 = new JLabel(" My Comparision Program");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(label3);
panel.add(label2);
panel.add(label4);
panel.add(txt1);
panel.add(label1);
panel.add(txt2);
panel.add(label5);
enter_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (MaisamCustom.this instanceof LogicClass){
LogicClass logicClassObj = (LogicClass)MaisamCustom.this;
logicClassObj.enterButton();
}
}
});
panel.add(enter_btn);
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MaisamCustom obj = new LogicClass();
try {
obj.launchFrame();
} catch (Exception ex) {
System.out.println("Some issue while launching the application...");
}
}
}
Upvotes: 2