hi i am new to Java i developed many forms but i am unable to connect those forms please any one tell me how can connect one form to another form for example after login screen my application form should be open
It sounds like you're coming from a Visual Basic background and you are trying to have some kind of procedure to display a login window, then a main program window.
There are many different ways to do this, but the two most common would be:
Display login dialog
Retrieve login information from closed dialog
Validate or exit/redisplay login
Display main window
and
Display login window
On 'OK' being pressed, validate or exit/display error
Hide self
Show main window
The first would be implemented something like this:
public static void main(String[] args) {
LoginDialog dlg = new LoginDialog();
dlg.setVisible(true);
LoginCredentials cred = dlg.getCredentials();
if ( ! valid(cred)) {
System.exit(1);
}
MainWindow wnd = new MainWindow(cred);
wnd.setVisible(true);
}
The second would look more like this:
public static void main(String[] args) {
LoginWindow app = new LoginWindow();
app.setVisible(true);
}
LoginWindow.actionPerformed(ActionEvent e) {
if ( ! validCredentials()) {
System.exit(1);
}
setVisible(false);
dispose();
MainWindow wnd = new MainWindow();
wnd.setVisible(true);
}
I recommend the first, so you can reuse the LoginDialog in other places, as it does not start the main window of this specific application itself.
Related
I have a problem because I have the next code in my main class:
SelectCalculatorWindow selectCalculatorWindow = new SelectCalculatorWindow();
CalcWindow calcWindow;
if (selectCalculatorWindow.getOption() == SelectCalculatorWindow.BASIC_OPTION) {
calcWindow = new CalcWindow(0);
} else if (selectCalculatorWindow.getOption() == SelectCalculatorWindow.PSEUDOSCIENTIFIC_OPTION) {
calcWindow = new CalcWindow(1);
}
And, in other class (SelectCalculatorWindow), I have this:
public SelectCalculatorWindow() {
initComponents();
instantiateListener();
}
private void instantiateListener() {
acceptBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(basicCalculatorRbtn.isSelected()) {
setOption(BASIC_OPTION);
} else if (pseudoscientificCalculatorRbtn.isSelected()) {
setOption(PSEUDOSCIENTIFIC_OPTION);
}
setVisible(false);
}
});
}
So, I want that condition sentences that I wrote in the main class execute only if user click the button, and I don't know how to do it
You haven't posted a valid minimal reproducible example program yet, and so I can only guess, but having said that, my guess is that SelectCalculatorWindow creates and displays a JFrame which is a non-modal application window, which is not what you want. Instead you will want to display a modal child-window, or dialog, such as a modal JDialog. When you use this, it pauses application code flow in the calling code until the dialog has been dealt with, and so allows your program to pause waiting for the user to make their selection, and then resume the code once the selection has been made.
A JOptionPane is an example of a type of modal dialog, but using a JDialog, you can create windows as varied and flexible as a JFrame, but with the advantages noted above.
I'm on Windows using NetBeans IDE and lanterna. I try to create a SwingTerminal, but it won't show.
public static void main(String[] args) throws Exception {
SwingTerminal t = TerminalFacade.createSwingTerminal();
while (true) {
Thread.sleep(100);
}
}
I also tried displaying the JFrame, but I get null from SwingTerminal.getJFrame().
t.getJFrame().setVisible(true);
I also tried running the program from the command-line, thinking it might be an issue with NetBeans, but it didn't work either (cygwin). How can I make the SwingTerminal show?
I should've look at the Google Discussions first. Hacked together from a bunch of snippets:
public static void main(String[] args) {
// Create a Terminal and Screen.
SwingTerminal terminal = new SwingTerminal();
Screen screen = new Screen(terminal);
screen.startScreen();
// Add listener(s) for the Window. The JFrame won't shut
// down itself when Alt+F4 or the like is pressed or the
// Window is closed by pressing the X button.
terminal.getJFrame().addWindowListener(
new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
screen.stopScreen();
}
}
);
}
I am creating a small GUI java application that it will store some user credentials in a file.
If the file is missing or has the wrong properties then I want a pop to get brought up that will inform the user to register his credentials (so a new file can be created with the proper ones).
I have nailed down the logic of when the file is incorrect and/or missing but what I can't figure out (due to my inexperience with JFrame) is where exactly in the code to check if the user needs to enter his credentials so he can be prompted.
Let's say that the function showWarning() is the one that will check and display the popup if needed and this is my main JFrame function (this was generated from Netbeans mostly):
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
Do I put the showWarning() function inside the main function? If yes, do I put it right after new GUI().setVisible(true);? What is the proper way of doing this?
EDIT: I am stumbling to the same problem I did before. This is my showWarning() that I drafted quickly for testing purposes:
public void showWarning(){
File propertiesFile = new File("config.properties");
if (propertiesFile.exists() && propertiesExist(propertiesFile)) {
JOptionPane.showMessageDialog(rootPane, "Creds are ok");
} else {
JOptionPane.showMessageDialog(rootPane, "Creds are not ok");
}
}
The problem that I am having is that I can't make this method static in order to use it without an object because of the rootPane which is a non-static object. The problem that this caused is that I can't just write:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
showWarning();
}
});
}
I can't use showWarning() like that since it's a non-static method.
Do I need to have the GUI object in a variable properly or is there a way to make the showWarning() a static method?
If you want the check to run right when the program starts, you would want to put your function call after the main JFrame gui is set visible. See edited code below. Of course, I'm using the ambiguous showWarning() function here, but you should talor that line of code to your need. If calling a function, then right the function, but if wanting to call a new popup jframe you will need to do more lines of code there.
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
LoginForm login = new LoginForm();
login.setVisible(true);
}
});
}
Now here you would want to change the variables accordingly. The LoginForm is a Jframe already created.
You probably want your popup dialog to be modal, so the program does not continue until the user has handled and fixed the problem. To do this, do not use a JFrame but a JDialog for your popup dialog and make it modal. Then you can simply put the showWarning() call everywhere you want. I think I would put it inside the main.
Use JDialog for creating the pop up.
And either you add the showWarning() method call in main like this :
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
showWarning();
}
});
}
or better you can invoke the method showWarning() when the user credentials have to be enetered in the file. If checked just before it, it would be optimal.
I try build the 2 Class: LoginScreen Class and MainScreen Class
When I run program it will show the login screen first then I use username and password to login the Mainscreen are pop-up but the login screen doesn't disappear.I am not sure how to handle it correctly.
Because the method I use is
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (OK.equals(cmd)) { //Process the password.
char[] input = passwordField.getPassword();
if (isPasswordCorrect(input)) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrameExample.main(null);
}
});
} else {
JOptionPane.showMessageDialog(controllingFrame,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
//Zero out the possible password, for security.
Arrays.fill(input, '0');
passwordField.selectAll();
resetFocus();
} else { //The user has asked for help.
JOptionPane.showMessageDialog(controllingFrame,
"You can get the password by searching this example's\n"
+ "source code for the string \"correctPassword\".\n"
+ "Or look at the section How to Use Password Fields in\n"
+ "the components section of The Java Tutorial.");
}
}
I know this is the stupid code and wrong way to implement it but can you guide me to make the appropriate one.
I guess this method is a method of your first screen, which must be a JDialog or a JFrame. Just call setVisible(false) to hide the frame (you may also call dispose() if the dialog won't be used anymore).
Also, you shouldn't call the main method on JFrameExample. A main method is normally used to start a new application. Just do what the main method does from your action listener (probably new JFrameExample().setVisible(true)).
Finally, an event listener is always invoked in the event dispatch thread (EDT), so there is no point in using SwingUtilities.invokeLater from an event listener.
To recap, here's how the code should look like:
if (isPasswordCorrect(input)) {
setVisible(false); // or dispose();
JFrame mainFrame = new JFrameExample();
mainFrame.setVisible(true);
}
im creating a java application with netbeans. i have two jframes for login and the main application. what i want to do is to load the login jframe at runtime, then load the main application jframe when user authentication is correct. the instance of the login jframe must be destroyed after the main application jframe has already loaded. also, i want the user information from the login jframe to be passed to the main application jframe. how do i acheive this?
I suggest the following simple approach, whereby you create classes to represent your Login panel and main application frame. In the example I've created a login panel rather than a frame to allow it to be embedded in a modal dialog.
// Login panel which allows user to enter credentials and provides
// accessor methods for returning them.
public class LoginPanel extends JPanel {
public String getUserName() { ... }
public String getPassword() { ... }
}
// Main applicaiton frame, initialised with login credentials.
public class MainFrame extends JFrame {
/**
* Constructor that takes login credentials as arguments.
*/
public MainFrame(String userName, String password) { ...}
}
// "Bootstrap" code typically added to your main() method.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
LoginPanel loginPnl = new LoginPanel();
// Show modal login dialog. The code following this will
// only be executed when the dialog is dismissed.
JOptionPane.showMessageDialog(null, loginPnl);
// Construct and show MainFrame using login credentials.
MainFrame frm = new MainFrame(loginPnl.getUserName(), loginPnl.getPassword());
frm.setVisible(true);
}
});
Extend JFrame to create the Main Frame. Add a constructor in this to accept the user information.
From login screen, when authentication succeeds, create an instance of the Main frame by passing the login information. Invoke dispose() on the login frame and invoke setVisible(true) on the main frame.
MainFrame mainFrame = new MainFrame(userInput.getText());
this.dispose();
mainFrame.setVisible(true);