Re: Action Center problem
One way is to create your JFrame first before making the JDialog, pack() it so that it's rendered, but don't make it visible initially. Then create the JDialog so that it is subordinate to the JFrame (pass the JFrame into the JDialog's constructor). When you make the dialog invisible, make the JFrame visible. For example:
Code java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Bank {
public static void main(String[] args) {
final JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("My Frame", SwingConstants.CENTER));
frame.setPreferredSize(new Dimension(500, 400));
frame.pack();
frame.setLocationRelativeTo(null);
final JDialog dialog = new JDialog(frame, "My Dialog", true);
dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
dialog.setLayout(new FlowLayout());
dialog.setSize(new Dimension(300, 159));
dialog.setTitle("Login");
JButton login = new JButton();
login.setText("Login");
dialog.setLayout(new BorderLayout());
JPanel north = new JPanel(new GridLayout(3, 3));
north.add(new JLabel("Username: "));
north.add(new JTextField());
north.add(new JLabel("Password: "));
north.add(new JTextField());
north.add(new JButton("Opret"));
north.add(login);
dialog.add(north, BorderLayout.NORTH);
// dialog.setVisible(true); // must add actionlistener first
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
frame.setVisible(true);
}
});
dialog.setVisible(true);
}
}
Re: Action Center problem
Thanks for that :)
but i have other objects which i change into when i press different buttons, would it be better if placed all them in my main and then called my method on how to do diffrent stuff from other classes?
Re: Action Center problem
Quote:
Originally Posted by
lf2killer
Thanks for that :)
but i have other objects which i change into when i press different buttons, would it be better if placed all them in my main and then called my method on how to do diffrent stuff from other classes?
No, the above example was just that, a very brief example of how this can be done. You should extract and use the general ideas obtained, but your regardless of what you do, your main method should be *very* small, and in fact do little more than set your program up and start it running and nothing more.
Re: Action Center problem
okay :)
but how do i remove the frame from the code you gave me bacause I tried to cut it own, but the code wont work after that?