Search code examples
javaswinguser-interfacenetbeansswingworker

Java -How to implement code into GUI made in Netbeans gui builder?


I made a GUI using NetBeans GUI builder and it looks great! However, I've got no idea how to go about implementing code to change values inside of the GUI.

My program connects to a database and pulls information such as sex, room number, bed number, name and referral source for each individual patient. The purpose of the GUI is to represent any changes to that information every so often, so I want it to automatically sync with the database every 60 seconds or so. I can write the code for the database sync and I've already made the GUI.

enter image description here My question is how do I update the information displayed in the GUI made in NetBeans?

public class ConnectMSSQLServer {

static int bedCount;

public static int getBedCount(){


    return bedCount;

}

public void setBedCount(int number){
    bedCount = number;

}
public void dbConnect(String db_connect_string, String db_userid, String db_password) {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
        System.out.println("connected");
        Statement statement = conn.createStatement();
        String queryString = "select f2 from beds.dbo.Sheet1$";
        String queryString1 = "select f3 from beds.dbo.Sheet1$";
        String queryString2 = "select f4 from beds.dbo.Sheet1$";
        String queryString3 = "select f5 from beds.dbo.Sheet1$";
        String queryString4 = "select f6 from beds.dbo.Sheet1$";
        String queryString5 = "select f7 from beds.dbo.Sheet1$";
        String queryString6 = "select f8 from beds.dbo.Sheet1$";
        String queryString7 = "select f9 from beds.dbo.Sheet1$";
        String queryString8 = "select f10 from beds.dbo.Sheet1$";
        String queryString9 = "select f11 from beds.dbo.Sheet1$";
        String queryString10 = "select f12 from beds.dbo.Sheet1$";

        List<String> locationList = new ArrayList<String>();
        List<String> patientList = new ArrayList<String>();
        List<String> refferallList = new ArrayList<String>();
        List<String> DateList = new ArrayList<String>();
        // List<String> locationList = new ArrayList<String>();
        // List<String> locationList = new ArrayList<String>();
        // List<String> locationList = new ArrayList<String>();
        ResultSet rs = statement.executeQuery(queryString);

        while (rs.next()) {

            locationList.add(rs.getString(1));
            locationList.removeAll(Collections.singleton(null));

        }
        ResultSet rs1 = statement.executeQuery(queryString1);

        while (rs1.next()) {

            patientList.add(rs1.getString(1));
            patientList.removeAll(Collections.singleton(null));

        }

        Set<String> uniqueLocationList = new HashSet<String>(locationList);
        System.out.println(uniqueLocationList);
        setBedCount(uniqueLocationList.size());
        Set<String> uniquePatientList = new HashSet<String>(patientList);
        System.out.println(uniquePatientList);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    ConnectMSSQLServer connServer = new ConnectMSSQLServer();
    connServer.dbConnect("jdbc:sqlserver://******", "***", "********");
}

}

So basically I want to write code within the provided example that appends the data inside the GUI. Like bed1A.setIcon(emptyIcon); something like that, but I don't know how to change the values of the GUI from code that exists outside of it.


Solution

    • Keep a Controller class, the application, the boss.
    • Keep a DataModel class, field of the controller, for all the data, loaded from the database used to create the GUI; done by the controller.
    • And then there is the View, your JFrame, a field of the controller.

    Now in the GUI builder you want not to repeat yourself (DRY principly). All those room components should be a List of rooms. After using the GUI builder for adding some element, you can adapt the code for arrays/lists.

    The controller can set things in the JFrame: you add to the JFrame: addPatient(int room, int bed, String name) and can fetch the component from a list and use Ctrl-Space to elect from setText("Jane") to whatever.

    Make data classes like Room, Bed, Patient.

    (Use version control.)