HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Start of Login and Address book GUI in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
addressbookloginjavastartandgui

Problem

In GUI that provided with the right username and password lets you open up an address book like GUI where you have crud like operations. I'm still very inexperienced with swing facilities especially with layout managers and placing my components on a panel/frame. I'd like some feedback about how to properly structure these GUI type programs and/or better ways of designing these types of programs.

The program opens up a connection to the MySQL database where a previously made table is being accessed. Then compares against the values of the table and actions are decided from there.

Log-In GUI:

`import javax.swing.*;
import javax.swing.SpringLayout.Constraints;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.*;

public class Login implements ActionListener{

//declare instance variables
private JFrame frame;
Container pane;
JPanel Master, centerInfo, buttonInfo;
JLabel jlb_username, jlb_password, jlb_email;
JTextField jtf_username,jtf_email;
JPasswordField jtf_password;
JButton jb_login, jb_signup;
Connection conn;
private String username, password;

public void init_connection(){
//open connection to database
try
{conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root");
} catch(SQLException ex) { System.err.println(ex); }
}

public void start_gui(){
//setup frame, pane, and panels. put panels into pane
frame = new JFrame("Login to Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = new Container();
pane = frame.getContentPane();
Master = new JPanel();
centerInfo = new JPanel(new GridBagLayout());
buttonInfo = new JPanel(new FlowLayout(FlowLayout.LEADING,10,5));
Master.add(centerInfo, BorderLayout.CENTER);
Master.add(buttonInfo, BorderLayout

Solution

Login and Address Book GUI

  • Please follow the Java code conventions, e.g. no snake_case



  • Hungarian notation is a bad idea (especially for GUI elements)



  • Try to reduce the number of instance variables. E.g. there should be no reason to have a JLabel as instance variable, it can be local



  • Implementing listeners in top-level classes is bad, as this is an implementation detail the user of the class shouldn't see. Use anonymous classes or - when Java 8 is available - lambdas



  • GridBagLayout is a PITA. You can reduce code size substantially if you use something sane (FormLayout, TableLayout, MigLayout... - all not included in Swing, but free)



  • hard-coding connection data == bad idea



  • JFrames should be started with invokeAndWait or invokeLater (see e.g. http://en.wikibooks.org/wiki/Java_Swings/Swing_Layouts ). Most people don't follow this advice, but it's the "official" way. Just sayin'



Especially for Adress Book GUI

  • Separate the GUI from DB access (follow e.g. MVC-Pattern, DAO-Pattern)



  • Of course this means you need an Address class, too



  • Let me stress this: Separate the GUI from DB access



  • Consider higher-level abstractions than JDBC (e.g. JPA...)



  • Prepared statements are made to prepare a query. Don't recreate them every time. Make them once, store them in instance variables, and set only the values before executing them



  • Don't forget to separate the GUI from DB access

Context

StackExchange Code Review Q#78718, answer score: 3

Revisions (0)

No revisions yet.