Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>But now i need it to dispose the Login frame and access another method in my main.java. How can i achieve this from inside a private method in the Login.java?</p> </blockquote> <p>Here you have a design problem. You cannot call any frame's method because your <code>ActionListener</code> has its scope limited to <code>Login</code> panel. How to solve this? Implementing an <code>ActionListener</code> that has <em>visibility</em> enough to dispose the frame.</p> <p><strong>Note:</strong> try to avoid NetBeans GUI Builder (or any GUI builder). It's easy but you miss a lot of things than making it by your own hand. You can even write a cleaner code. But it's necessary learn about <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html" rel="nofollow">Layout Managers</a></p> <p>Example of Code: this example illustrates the fact that you can achieve the same with less than half of lines of code.</p> <pre><code>import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Demo { private void initGUI(){ final JTextField textField = new JTextField(20); final JFrame frame = new JFrame("Login"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JButton button = new JButton("Accept"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if("user".equals(textField.getText())){ frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); // or simply frame.dispose() } else { JOptionPane.showMessageDialog(null, "Wrong user! Keep trying.", "Login failed", JOptionPane.WARNING_MESSAGE); } } }); JPanel login = new JPanel(new FlowLayout()); login.add(new JLabel("User")); login.add(textField); login.add(button); frame.getContentPane().add(login); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Demo().initGUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload