Hi All,

I have just finished reading fundamental topics in Java and now i have decided to do a New Project in Java which comprises all basic features of Java.

I need help in Understanding/visualizing the architecture / Design model for my Project.

Specification is :

1 . Develop a Standalone GUI Application with which user logs in with Credentials and then if its successfull, he gets a Window , the window has few Labels like name,date,descritpion etc and related text fields under the labels for user to enter the information.

2.A user, if he logs in and enters some information in the text fields and click on Save button, the textfields has to save the data entered by user.

  1. And then if another user logs into the app, it should display all the details entered by the previous user, so that he can modify any contents in the fileds and save it. So at any time if any user logs in he shoud get the last saved information in the application by any user.

Stages Completed :

  1. I have created two GUI's one for Login Window and one for Main Window after login. So if a user logs in, the class checks the user credentials in database and authenticates and he gets the mainwindow.

  2. As previously said, main window contains labels and Textfield.

Challenges facing :

  1. How to make the Main window contents sharable by Users. That is if Peter saves some information in the "Description" Textfiled like for example :

Description : James, please call me when you see this message .

So when James logs in he should see the info in "Description" TextFiled updated by Peter.

  1. Now I have Two Rows in the main Window created by Grid Layout (2*8)

First row showing labels like name, number, description and the second row contains appropriate text fields.

This is for only one Event, I dont know how many events will come in a day. So if user wants to add another event he needs to click a button and then another row has to come with the text fields as row 2 so that he can update the new event in the new textfiled.

Suggestions are greatly appreciated. Thanks in Advance.

Have you tried writing the information in the text fields to a file. You can then read and write to that file to set up the text fields.

OK, we're ready to help... but you need to explain your thoughts so far, then we can make suggestions from there.

Well that is the Question, but where is your efforts?

Hi James,sirlink99

I am thinking like, whenever the user writes something in the textfields it needs to be updated in the database and then when another user logs in the datas need to be pulled from the database so that he can view the details.

Also i want to create Components at runtime, like if user wants to add a new event, a new row has to be created which looks like previous row.

I am also thinking of giving an option called Search with which if user enters the date, it should display the Same Main Window in Read only mode which shows all the events updated by users on that date.

I am wondering how to do all these at runtime, like saving the mainwindow contents on user updation and also on a daily basis for reference, and adding components in my GUI during runtime as per user Action.

You will want to take a look at JDBC. As for the updating the database you could update it in intervals, or whenever the user types, moves the mouse, etc.as for the extending the solution I can think of is to have a panel, and then have an arrayList/Vector containing components, and then when you press a button you add the components to a corresponding arraylist/vector, and then add it to the panel.

If you have a variable list of things to display then use a JList or JTable, both of which have a varibale number of rows. There's no need to create controls on the fly

Thanks all,

im posting the things i have completed so far.

1st is the login class and then comes the class which checks the credentials in database and then opens the main window and now im working on perfecting the view of main window.

please correct me if you think of any suggestion in the code, i mean its syntactically correct and runs without errors, im asking like any improvement which fine tunes it look and feel if there is any room for it.

    public class Login implements ActionListener{

        private JLabel jUser;
        private JLabel jPassword;
        private JLabel jStatus;
        private JTextField jText1;
        private JTextField jText2;
        private JButton jLogin;


        /** This is the login class which the users logs in and 
         * gets the appropriate window from User or Leader class
         * @param args
         */
        public Login(){

            JFrame jFrm = new JFrame("Login");
            jFrm.setSize(400,400);
            jFrm.setVisible(true);
            jFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JFrame jFrm1 = new JFrame();
            jFrm.getContentPane().setLayout(new GridLayout(4,1));

            jUser = new JLabel("UserName",SwingConstants.CENTER);
            jUser.setBorder(BorderFactory.createEtchedBorder());
            jPassword = new JLabel("Password",SwingConstants.CENTER);
            jPassword.setBorder(BorderFactory.createEtchedBorder());
            jStatus = new JLabel("Status",SwingConstants.CENTER);
            jStatus.setBorder(BorderFactory.createEtchedBorder());
            jText1 = new JTextField();
            jText2 = new JTextField();


            jLogin = new JButton("Login");
            jLogin.addActionListener(this);

            JPanel panel = new JPanel(new GridLayout(1,2));
            panel.add(jUser);
            panel.add(jText1);

            jFrm.add(panel);

            panel = new JPanel(new GridLayout(1,2));
            panel.add(jPassword);
            panel.add(jText2);

            jFrm.add(panel);
            jFrm.add(jLogin);
            jFrm.add(jStatus);



        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            String s1;
            String s2;
            int i;

            s1 = jText1.getText();
            s2 = jText2.getText();

            UserService object =new UserService();

            i = object.userService(s1, s2);

            if(i!=0){
            MainWindow reference = new MainWindow();
            }


        }


    }


public class UserService {

    /**
     * @param args
     */
    public int userService(String username, String password) {
        // TODO Auto-generated method stub
        int i=0;
        String s1,s2,s3,s4;
        System.out.println("-------- MySQL JDBC Connection Testing ------------");
        Statement state = null;
        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();

        }

        System.out.println("MySQL JDBC Driver Registered!");


        try {
            connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/test","root", "root");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();

        }

        if (connection != null) {
            //System.out.println("You made it, take control your database now!");
        } else {
            //System.out.println("Failed to make connection!");
        }

        String table = ("select user_id, user_name, password,role_id from user");

        try {
            //state = (Statement) connection.createStatement();

        ResultSet rs =  connection.createStatement().executeQuery(table);

        while(rs.next()){

            s1 = rs.getString("user_id");
            s2 = rs.getString("user_name");
            s3 = rs.getString("password");
            s4 = rs.getString("role_id");

            //System.out.println(s1 +" " + s2+" " + s3+" " + s4+" ");
            //System.out.println();
         i = Integer.parseInt(s4);

        }



        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return i;

      }




    }

It should not be a problem if you designed your database correctly in the first place. I wonder if it is possible for you to avail the database design (lets say the sql code). I think it is best to create appropriate tables in the database and have the program retrieve, update or delete the data as required.

Hi All,

I created one row of data in tablespace and I also created a JTable with one row and had the JTable display the tablespace data.But my problem is in realtime, I don't know how many row of data's are there in the tablespace , so I just cant create in normal way of creating a certain number of rows in JTable which is equal to the no of rows in Database Table.

The question I asked is considering the scenario I faced as below.

The program I wrote below takes the tablespace content and displays in the JTable.

In the program when I created JTable, I used this following statement

table = new JTable (new DatabaseModel(1));

so it created a JTable with one row which displays the one row in my table space.

FYI , I put DatabaseModel(1) since I know that I have only one row in my tablespace.

import javax.swing.table.AbstractTableModel;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DatabaseModel extends AbstractTableModel{

Connection dbConnection = null;
Statement statement = null;
String selectTableSQL = "SELECT index, ticket_no, date, Description, priority, State, follow_up, status from incident";
ResultSet rs; 
int length;
String index,ticket,date,description,priority,state,followup,status;
String heading[] = {"index","ticket","date","description","priority","State","follow up","status"};

public DatabaseModel(int row) {

super();

length = row;

}

/**
* @param args
*/

@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 8;
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return length;
}

public String getColumnName(int c){

return heading[c];

}

@Override
public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub

try {

Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){

System.out.println(e.getMessage());
}

try {

dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
//return dbConnection;
} catch(SQLException e ){

System.out.println(e.getMessage());
}

try {
statement = dbConnection.createStatement();

rs = statement.executeQuery("Select * FROM incident");
while(rs.next()){
index = rs.getString("index");
ticket = rs.getString("ticket_no");
date = rs.getString("date");
description = rs.getString("description");
priority = rs.getString("priority");
state = rs.getString("state");
followup = rs.getString("follow_up");
status = rs.getString("status");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 

if (dbConnection != null) {
//System.out.println("You made it, take control your database now!");
} else {
//System.out.println("Failed to make connection!");
}

if(col==0) return index;
else if(col==1) return ticket;
else if(col==2) return date;
else if(col==3) return description;
else if(col==4) return priority;
else if(col==5) return state;
else if(col==6) return followup;
else return status;
}

}
][/import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class DatabaseView {


JTable table;
public DatabaseView() {
// TODO Auto-generated constructor stub


JFrame jFrm = new JFrame("simple table");
jFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrm.setVisible(true);
jFrm.setSize(500,300);
jFrm.setLayout(new FlowLayout());

table = new JTable (new DatabaseModel(1));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setPreferredScrollableViewportSize(new Dimension(1000,400)); 

JScrollPane scrollpane = new JScrollPane(table);
jFrm.add(scrollpane);

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

DatabaseView reference = new DatabaseView();

}

}
] 

In your DatabaseModel don't set the length until after you have run the SQL select, then you will know how many rows there are.
Your SQL is in the wrong place. getValueAt will be called at least (rows * columns) times, and you don't want to run the SQL every time.
You could run the SQL in the constructor for DatabaseModel, then all the data will be ready and waiting for when getValueAt is called, and you will know how many rows there are.

Member Avatar for 1stDAN

As James already stated there is something gotten mixed up of your code. I also assume there is to much complex code. Her is some simpler pseudocode for your problem:

// 1st Connect to a database
conex = Drivermanager.getConnection (url, user, password); // assuming JDBC

// 2nd Create a statement
statement stat = conex.createStatement();

// 3rd Create a resultset by executing a statement
Resultset resu = stat.executeQuery("SELECT index, ticket_no, date, Description, priority, State, "
 + "follow_up, status from incident");  // taken from your posted code

// here you may print out your head lines (array headings[])

// 4th Accessing the rows of your result set
while (resu.next()) {
  // following has been copied from your posted code:
  index = rs.getString("index");
  ticket = rs.getString("ticket_no");
  date = rs.getString("date");
  description = rs.getString("description");
  priority = rs.getString("priority");
  state = rs.getString("state");
  followup = rs.getString("follow_up");
  status = rs.getString("status");

  // here you can process all just accessed column values of the current row
  // For example add column values to a JTable
}

/* Well, you may simply place above lines in your main(). Ok, if you want to show your results in a JTable-object, there would be something more. However, you can simply test above code on a terminal by outputting the column values using plain println() or for simpler formatting also printf(). */

-- 1stDAN

quite some "ugly" code. just a suggestion. replace this:

if(col==0) return index;
else if(col==1) return ticket;
else if(col==2) return date;
else if(col==3) return description;
else if(col==4) return priority;
else if(col==5) return state;
else if(col==6) return followup;
else return status;

with a switch statement. easier to read and overall better performance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.