Reputation: 1179
This is my code
public Main_panel() {
initComponents();
setLocationRelativeTo(null);
tf_type.setVisible(false);
String normal = tf_type.getText();
String ntext = "normal";
if(normal.equals(ntext)) {
cmb_report.setVisible(false);
cmb_cu.setVisible(false);
}
And As For additional information, the tf_type is set to public static via customize code in netbeans. But the cmb_reports and cmb_cu are not made invisible, i.e if statement is not executed. why is that?
Upvotes: 1
Views: 1130
Reputation: 285405
You're calling the if blocks in the program's constructor, before the user has had time to enter data into any JTextField. If you want this change to occur during program run, you will need to use a listener of some sort such as by adding an ActionListener to a JTextField.
Regarding this statement of yours:
the tf_type is set to public static via customize code in netbeans
You don't want to do this. Don't make your fields static just so you can access them in a main without an instance. This will break all OOP principles and make your code very difficult to maintain or update. A better way is to change the state of your instances via non-static public methods.
Edit: You state
this was snippet from main_panel.java...in login jframe this code sets the value for tf_type by Main_panel.tf_type.setText(txt_type.getText()); fyi....after logged in, main panel appears...
I would use a modal JDialog rather than a JFrame to log in, since the modal JDialog will easily let you know when it has been fully dealt with, and I would get the state of the fields of the log in dialog by calling public methods on it, not by using static fields.
Edit 2: For example,
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class LogInDialogTest {
private static void createAndShowGui() {
JTextField textField1 = new JTextField(10);
textField1.setEditable(false);
textField1.setFocusable(false);
JPanel mainPanel = new JPanel();
mainPanel.add(textField1);
mainPanel.add(new JButton(new AbstractAction("Exit") {
@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
// frame.setVisible(true);
JTextField textField2 = new JTextField(10);
JPanel mainPanel2 = new JPanel();
mainPanel2.add(textField2);
mainPanel2.add(new JButton(new AbstractAction("Submit") {
@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
JDialog dialog = new JDialog(frame, "Dialog", ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(mainPanel2);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
textField1.setText(textField2.getText());
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit 3: A better example,
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class LogInDialogTest {
private static void createAndShowGui() {
final MainJPanel mainPanel = new MainJPanel();
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
// frame.setVisible(true);
LoginJPanel loginPanel = new LoginJPanel();
JDialog dialog = new JDialog(frame, "Dialog",
ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(loginPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
mainPanel.textFieldSetText(loginPanel.textFieldGetText());
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class LoginJPanel extends JPanel {
private JTextField textField = new JTextField(10);
public LoginJPanel() {
add(textField);
add(new JButton(new AbstractAction("Submit") {
@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
}
public String textFieldGetText() {
return textField.getText();
}
}
class MainJPanel extends JPanel {
private JTextField textField = new JTextField(10);
public MainJPanel() {
textField.setEditable(false);
textField.setFocusable(false);
add(textField);
add(new JButton(new AbstractAction("Exit") {
@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
}
public void textFieldSetText(String text) {
textField.setText(text);
}
}
Upvotes: 3