Skip to content

Instantly share code, notes, and snippets.

@mansueli
Created July 8, 2014 15:53
Show Gist options
  • Save mansueli/4808db65aaff81b76684 to your computer and use it in GitHub Desktop.
Save mansueli/4808db65aaff81b76684 to your computer and use it in GitHub Desktop.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LoginPageUI {
private BooleanProperty isLogin = new SimpleBooleanProperty();
public LoginPageUI() {
}
public JPanel LoginUI() {
JPanel loginPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel usernameLabel = new JLabel("username");
JLabel passwordLabel = new JLabel("password");
final JTextField usernameTF = new JTextField(20);
final JTextField passwordTF = new JTextField(20);
JButton loginBtn = new JButton("Login");
c.gridx = 0;
c.gridy = 1;
loginPanel.add(usernameLabel, c);
c.gridx = 1;
c.gridy = 1;
loginPanel.add(usernameTF, c);
c.gridx = 0;
c.gridy = 2;
loginPanel.add(passwordLabel, c);
c.gridx = 1;
c.gridy = 2;
loginPanel.add(passwordTF, c);
c.gridx = 1;
c.gridy = 3;
loginPanel.add(loginBtn, c);
loginBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String u = usernameTF.getText().toString();
String p = passwordTF.getText().toString();
if (u.equals("a") && p.equals("123")) {
isLogin.set(true);
System.out.println("Inside the system");
}
}
});
return loginPanel;
}
public boolean verifyLogin() {
return isLogin.get();
}
public BooleanProperty loginProperty(){
return isLogin;
}
}
public class Main {
public static void main(String[] arg){
new RunApp();
}
}
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageTwo {
public PageTwo() {
}
public JPanel displayPageTwo() {
JLabel label = new JLabel("Welcome to Page Two!");
JPanel pTwoPanel = new JPanel();
pTwoPanel.add(label);
return pTwoPanel;
}
}
package JButton;
import java.awt.CardLayout;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class RunApp {
public RunApp() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("testing");
final JPanel panel = new JPanel();
panel.setLayout(cardLayout);
final LoginPageUI lPage = new LoginPageUI();
PageTwo pageTwo = new PageTwo();
panel.add(lPage.LoginUI(),"1");
panel.add(pageTwo.displayPageTwo(),"2");
cardLayout.show(panel, "1");
lPage.loginProperty().addListener(new ChangeListener<Boolean>(){
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
if(lPage.verifyLogin())
cardLayout.show(panel, "2");
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment