Jump to content


Photo

Netbeans Login Form With Mysql Database, Got Questions Dont Know How To Form Them..

managing jframe windows

  • Please log in to reply
5 replies to this topic

#1 iNko

iNko

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts

Posted 29 November 2012 - 09:55 AM

Hey, im using NetBeans to make a simple GUI, that would connect to a mysql database. I followed some guides and managed to make something, but now i want to change some features and i dont know what to look for on google/youtube.. Ill try to explain what i need, maybe someone can help me with it or point me in the right direction on what i should be looking for.

So, when i press 'Run Project' in NetBeans, 1st i get this:
Posted Image
1) I would like to put that info message window, where it says that connection to database is established, somewhere in the GUI, is that possible?

After i press 'OK' on the info message window, i can then input my login/password and login into the databse, heres the screen:
Posted Image
2) Here the info message window says that the login information is correct, again, i would like to hide this info window so that it would display somewhere in the GUI and not as a pop-up window.

After i press 'OK' on the login detail window, a new 'JFrame' opens up (witch i will fill in with other things that i need later), heres the screen:
Posted Image
3) The old 'Login form' window didnt dissapear, can i make it so the new 'JFrame' opens up and the login frame dissapears, or can i make it so that the new JFrame window would replace the login frame?


Heres the code just in case:
Settings for database, shows the 1st info message window (if connection was succesfull):
public class mysqlconnect {
Connection conn = null;
public static Connection ConnectDb() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://Database related stuff");
JOptionPane.showMessageDialog(null, "Prisijungimas pavyko");
return conn;
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}

When i click the 'Login' button in the login frame, shows if login info was correct or not, also if correct opens new JFrame:

private void cmdloginMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String sql = "select * from Vartotojas where login = ? and password = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtusername.getText());
pst.setString(2, txtpassword.getText());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys teisingi");
QueryJFrame s = new QueryJFrame();
s.setVisible(true);
}
else
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys neteisingi");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
So yeah, these are my main problems at the moment, i dont really know what to type into google for help, so hoping someone here will clear things for me..

Also got some side questions:
4) After im done with this project, can i make it so it would be installable program?
5) If i can install it, can i make it start-up automatically every time i turn on the computer? (for example, like any antivirus program or even skype)
6) If i can install it, can it have its own icon near the clock in the bottom right corner?


Thx and sorry if this was posted in the wrong seciton.

#2 Adam

Adam

    Advanced Member

  • Gurus
  • 5,680 posts
  • LocationSheffield / UK

Posted 29 November 2012 - 04:47 PM

Moved to Other Programming Languages; Java != Javascript.

#3 Maq

Maq

    Advanced Member

  • Moderators
  • 9,315 posts
  • LocationPennsylvania, USA

Posted 29 November 2012 - 06:16 PM

To answer questions 1, 2, & 3 when you create your GUI you should be creating a separate JPanel and adding it to your JFrame. You can update them dynamically in your Listener methods. If you want specifics post your code with the main JFrame container and where these methods reside.
Electronic Cigarette Reviews - Smoking alternatives, find YOUR ecig! | tjmothy
ini_set ("display_errors", "1");
error_reporting(E_ALL);

#4 iNko

iNko

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts

Posted 30 November 2012 - 03:54 AM

To answer questions 1, 2, & 3 when you create your GUI you should be creating a separate JPanel and adding it to your JFrame. You can update them dynamically in your Listener methods. If you want specifics post your code with the main JFrame container and where these methods reside.


Thx for helping, i have only 2 jframes now, ill post both of their codes because im not sure witch one is the main one

This is the one that opens after succesfull login:
public class QueryJFrame extends javax.swing.JFrame {

public QueryJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")

private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new QueryJFrame().setVisible(true);
}
});
}
}


And this is the code from the login frame

import java.sql.*;
import javax.swing.*;
public class loginJFrame extends javax.swing.JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;


public loginJFrame() {
initComponents();
}

@SuppressWarnings("unchecked")


private void formWindowOpened(java.awt.event.WindowEvent evt) {
conn = mysqlconnect.ConnectDb();
}

private void cmdloginMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String sql = "select * from Vartotojas where login = ? and password = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtusername.getText());
pst.setString(2, txtpassword.getText());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys teisingi");
QueryJFrame s = new QueryJFrame();
s.setVisible(true);
}
else
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys neteisingi");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}

public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new loginJFrame().setVisible(true);
}

});
}


private javax.swing.JButton cmdlogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField txtpassword;
private javax.swing.JTextField txtusername;
}


And this is connection to database file

import java.sql.*;
import javax.swing.*;
public class mysqlconnect {
Connection conn = null;
public static Connection ConnectDb() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://database info");
JOptionPane.showMessageDialog(null, "Prisijungimas pavyko");
return conn;
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}

This is my entire code, ill go look into that JPanel attachment to JFrame thing now.

Edited by iNko, 30 November 2012 - 03:55 AM.


#5 iNko

iNko

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts

Posted 30 November 2012 - 04:15 AM

Hmm idk why i cant edit my earlier post twice, so making a new post now..

Ok so i managed to get what i want with the info message windows by just adding a new label field into the GUI and changing:

This
//JOptionPane.showMessageDialog(null, "Prisijungimo duomenys teisingi"); 

Into this
logincheckLabel.setText("Prisijungimo duomenys teisingi");

Still cant find anything with the JFrame..

#6 iNko

iNko

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts

Posted 30 November 2012 - 04:52 AM

.. Cant edit my previous post, so making new one again.....

Solved the jframe thingy with by making new method in the login JFrame:

   public void closeLogin() {
       WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
       Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
   } 

And then called this method after the login button is pressed:

try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, txtusername.getText());
            pst.setString(2, txtpassword.getText());
            rs = pst.executeQuery();
            if (rs.next()){
                closeLogin();
                QueryJFrame s = new QueryJFrame();
                s.setVisible(true);

            }

Thx guys :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com