5 Attachment(s)
how to open a new JFrame from a JFrame
Hi, so I want to make this kind of "webpage" using GUI for a standalone program. this program contains a login page, and if login successful, it will go to a panel of buttons. one of the buttons to open my existing program called FileExplorer which displays my comp files and folders. problem is, i don't know how to open up that JFrame from FileExplorer by clicking the button.
i have tried adding action listener to the button but i encountered an error of :
"java.lang.IllegalArgumentException: adding a window to a container "
may i know how to do it correctly?
My login page:
Code:
//username : nasya , password : nasya
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Login extends JFrame {
private static final String text = "<html> <center><b><font size=+3>Central</font></b> <br><b><font size=+3>Manager</font></b></center> </html>";
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JPanel jPanel2 = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField loginTextField = new JTextField(20);
JPasswordField passwordTextField = new JPasswordField(20);
JPanel jPanel3 = new JPanel();
JButton exitButton = new JButton();
JButton enterButton = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
public Login() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void login(ActionEvent e) {
String login = loginTextField.getText();
String password = new String(passwordTextField.getPassword());
//validate login and password here. validity will be done by sending login/password to the server
if (login.equals("nasya") && password.equals("nasya")) {
System.out.println("login successful");
showButtonsView();
}
else {
JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
loginTextField.setText("");
passwordTextField.setText("");
loginTextField.requestFocusInWindow();
}
}
private void showButtonsView() {
getContentPane().removeAll();
ButtonView button = new ButtonView();
getContentPane().add(button);
getContentPane().validate();
}
private void exit(ActionEvent e) {
setVisible(false);
}
public static void main(String[] args) {
Login f = new Login();
f.setLocation(200,200);
f.pack();
f.show();
}
private void jbInit() throws Exception {
jPanel1.setLayout(borderLayout1);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText(text);
jPanel2.setLayout(gridBagLayout1);
jLabel2.setText("Password:");
jLabel3.setText("Login:");
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
exit(e);
}
});
enterButton.setText("Enter");
enterButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
login(e);
}
});
jPanel3.setLayout(flowLayout1);
flowLayout1.setAlignment(FlowLayout.RIGHT);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jPanel1.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(loginTextField, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 11, 0, 0), 0, 0));
jPanel2.add(jLabel2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11, 0, 0, 0), 0, 0));
jPanel2.add(passwordTextField, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 11, 0, 0), 0, 0));
jPanel2.add(jLabel3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
jPanel1.add(jPanel3, BorderLayout.SOUTH);
jPanel3.add(enterButton, null);
jPanel3.add(exitButton, null);
}
}
My Buttons Page:
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ButtonView extends JFrame
{
GridLayout gridLayout1 = new GridLayout(2,2);
BorderLayout borderLayout1 = new BorderLayout();
//JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
public ButtonView() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.setLayout(borderLayout1);
jButton1.setText("View FileSystem");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
new FileExplorer();
setVisible(true);
}
});
jButton2.setText("Example");
jButton3.setText("Example");
jButton4.setText("Example");
this.add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(gridLayout1);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
jPanel1.add(jButton4, null);
}
}
The FileExplorer code spans 7 classes..so i will just attach them here if you wanna look into it...
Please give me some insights and thanks in advance :)