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);
Related
I've seen similar questions answered but could not find an answer to my question. I have a Main Class, which has it's own JFrame. However, I've created a different Class where I've created another JFrame that prompts the user for some data. The Main Class is the main app. The secondary class is supposed to pop up before the main class GUI runs. I've created 2 different packages for each one of the Classes.
So, I'm trying to call an Object of the secondary Class from Main Class but the interface does not appear. I do not get any errors in the code and the App runs as if the Object of secondary Class is not being called at all. I am new to Java and would appreciate some lights on this.
My code is as follows:
Main Class
public class TempConverter extends javax.swing.JFrame {
public TempConverter() {
initComponents();
}
// More code
public static void main(String args[]) {
DemoUserData test = new DemoUserData();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test.setVisible(true);
new TempConverter().setVisible(true);
}
});
}
Secondary Class
public class DemoUserData extends javax.swing.JPanel {
public DemoUserData() {
initComponents();
}
}
Your JFrame is the main window. Before it is shown at the very early start a splash screen maybe shown, normally a small rectange with a logo.
It however seems, you want some input dialog, like say a login. That cannot be a JPanel, but must be a top-level window: JFrame or JDialog. Or one of the JOptionPane dialogs (asking string input, or whatevever).
Maybe you should make a JFrame for your current JPanel, run that.
.
DemoUserDataFrame test = new DemoUserDataFrame(this);
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test.setVisible(true);
}
});
public class DemoUserDataFrame extends JFrame {
//private final JFrame tempConverter;
public DemoUserDataFrame(final JFrame tempConverter) {
//this.tempConverter = tempConverter;
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
tempConverter.setVisible(true);
}
});
}
...
}
In the above, closing test, will make the main JFrame visible.
In order to have a better overview, have the classes not refer one to another, you might look into the Model-View-Controller concept. Then there is one global "Controller" class as intermediator for all business logic. It holds the data (Model), and so on.
I am making a Java gui project and it consists of two frames.
The problem is that when I call the secondframe from the firstframe, I have set it such that the firstframe visibility is set to false. The problem is how do I make the firstframe visible again by using a button from the second frame.
should i ditch this method and create a new jpanel instead??? Does jpanel have similar capabilities as jframe?
Consider using CardLayout. This way you can switch via multiple UIs without needing another frame. Here's how to use it.
Edit: As Guillaume posted in his comment, this answer from Andrew also covers how to use the layout.
Edit2:
As you requested a little more information about my latest post, here's how such a class may look like:
import javax.swing.JFrame;
public abstract class MyFrameManager {
static private JFrame startFrame,
anotherFrame,
justAnotherFrame;
static public synchronized JFrame getStartFrame()
{
if(startFrame == null)
{
//frame isnt initialized, lets do it
startFrame = new JFrame();
startFrame.setSize(42, 42);
//...
}
return startFrame;
}
static public synchronized JFrame getAnotherFrame()
{
if(anotherFrame == null)
{
//same as above, init it
}
return anotherFrame;
}
static public synchronized JFrame getJustAnotherFrame()
{
//same again
return justAnotherFrame;
}
public static void main(String[] args) {
//let's test!
JFrame start = MyFrameManager.getStartFrame();
start.setVisible(true);
//want another window
JFrame another = MyFrameManager.getAnotherFrame();
another.setVisible(true);
//oh, doenst want start anymore
start.setVisible(false);
}
}
This way you would only instantiate every JFrame once, but you could always access them via your manager class. What you do with them after that is your decision.
I also just made it thread-safe, which is crucial for singletons.
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);
}
This is my applet
public class TestApplet extends Applet{
public void init(){
}
public void start(){
Swsmall b = new Swsmall();
}
}
This is my Swsmall file
public Swsmall() {
JFrame frame = new JFrame ("Stand alone");
JButton jl = new JButton("Exits properly");
frame.getContentPane().add(jl);
frame.setSize(180,80);
frame.setVisible(true);
jl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);}});
}
this my jsp file
<body>
<applet code="TestApplet.class" width="400" height="400"></applet>
</body>
I am able to run applet successfully but I can't get any responce on button click event
When I run same application on java console it works perfect
Calling System.exit(0) from Java applet will not destroy an applet. Try calling something else from action listener (i.e. System.out.println("something"); would print in Java applet console), and you'll see that it's called correctly, but in this case it probably doesn't work as you expected it to work.
frame.dispose();
Code for creating the JFrame should be placed in the init() method. You should also be using the invokeAndWait() method.
Read the section from the Swing tuorial on How to Make Applets for more information and a working example.
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.