简体   繁体   中英

To show values in Jlist from DataBase onload

[To show values in Jlist from DataBase onload][Swing GUI and Database`

Initializing the code:

 public void initMethod()
    {


         try {
             PortSelectionBox.addItem("COM1");
             PortSelectionBox.addItem("COM2");

             String[] listData={"sac : 1", "prl : 2","railway : 3", "railway : 4", "railway : 5", "prl : 6"};
             //SourceJList.setListData(listData);


             //get values from database and show in destlist
             String query="Select * from `test`.`mapgroup`;";
             System.out.println("query for selecttion: "+query);
             ResultSet rs= con.SelectData(query);
             ArrayList al = new ArrayList();
             while(rs.next())
             {
                 group=rs.getString("GroupName");
                 port=rs.getString("PortId");
                 System.out.println("grp: "+group);
                 System.out.println("port: "+port);
                 al.add(group);

             }

             //check if values present in db then show in dest list

             //check which port is selected and then show value from database



             if((PortSelectionBox.getSelectedItem().toString()).equals(port))
             {
                 System.out.println("if condition satisfied"); 
             }
              for (String string : listData) {
                 SorceModel.addElement(string);
             }
             SourceJList.setModel(SorceModel);


         } catch (ClassNotFoundException ex) {
             Logger.getLogger(PanelSMS.class.getName()).log(Level.SEVERE, null, ex);
         } catch (SQLException ex) {
             Logger.getLogger(PanelSMS.class.getName()).log(Level.SEVERE, null, ex);
         }

    }

action performed on combobox onchnage:

 private void PortSelectionBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:

        //Show values in destList according to values stored in DB with there ports ,what to do here to map values with values stored in db



    }

action performed on add(>>) button on click:

private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {            

         try {

            String port=PortSelectionBox.getSelectedItem().toString().trim();
             String group=SourceJList.getSelectedValue().toString();
             String temp=group;
             String query="query to insert into db";
             con.Ins_Upd_Del(query);
             System.out.println("code to add: "+query);

             System.out.println("data added");

            DestModel.addElement(temp);
            DestJList.setModel(DestModel);
            SorceModel.removeElement(group);
            System.out.println("done....");
         } catch (ClassNotFoundException ex) {
             Logger.getLogger(PanelSMS.class.getName()).log(Level.SEVERE, null, ex);
         } catch (SQLException ex) {
             Logger.getLogger(PanelSMS.class.getName()).log(Level.SEVERE, null, ex);
         }

    }

,

action performed on remove(<<) button onclick:

private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    try {
        String port=PortSelectionBox.getSelectedItem().toString().trim();
         String group=DestJList.getSelectedValue().toString();
         String temp=group;
         String query="query to delet";
         System.out.println("code to remove: "+query);
         con.Ins_Upd_Del(query);
         System.out.println("data added");

        SorceModel.addElement(temp);
        SourceJList.setModel(SorceModel);
        DestModel.removeElement(group);
        System.out.println("done....");
     } catch (ClassNotFoundException ex) {
         Logger.getLogger(PanelSMS.class.getName()).log(Level.SEVERE, null, ex);
     } catch (SQLException ex) {
         Logger.getLogger(PanelSMS.class.getName()).log(Level.SEVERE, null, ex);
     }
}     

I have done the work for add and remove buttons. but now i am stuck in the view part. What i am doing is that i am populating first Jlist from an arraylist. when i add any element from this jlist its get added into second JList and inserted into DB, similarly on remove operation it is doing the reverse.

now what i need is, those values which i have already added in second Jlist ,it should not be shown in first Jlist and vice versa on the time of load and values should be shown respectively according to the selection. also the selection box plays an important role here as i want to show the values in the jlist according to the selected value.

For example if i have stored com1 with "prl : 1" then it should be shown only in second Jlist onload. Incase i want to update this lists then it should be done in DB and displayed likewise.

i am not getting the flow for the view part. i already have everything stored in DB, i just want to map the things and show them accordingly .

Please help me out... as i am not able to get what to do next... `] 2

If I understand your question correctly, you want to get the initialization of the two JList components to work properly. The destination list should be filled based on the database, right? This could work like this (part of the initMethod method):

// [...]

//get values from database and show in destlist
String query="Select * from `test`.`mapgroup`;";
System.out.println("query for selection: "+query);
ResultSet rs= con.SelectData(query);
String port = null;
ArrayList<String> groupsInDatabase = new ArrayList<>();
while(rs.next())
{
    String group=rs.getString("GroupName");
    port=rs.getString("PortId");
    System.out.println("grp: "+group);
    System.out.println("port: "+port);
    groupsInDatabase.add(group);
}

//check if values present in db then show in dest list
DestModel = new DefaultListModel<>();
for (final String group : groupsInDatabase) {
    DestModel.addElement(group);
}
DestJList.setModel(DestModel);

// [...]

Should the source list be filled with everything from the listData variable, except groups that are in the database? (I have renamed the listData variable to allGroups below.) Initialization for this list could look like this (also part of the initMethod method):

// [...]

SourceModel = new DefaultListModel<>();
for (String group : allGroups) {
    if (!groupsInDatabase.contains(group)) {
        SourceModel.addElement(group);
    }
}
SourceJList.setModel(SourceModel);

// [...]

The role of the PortSelectionBox is not yet clear to me. Do "COM1" and "COM2" relate to certain records in the database (like "prl : 1" and "prl : 2")?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM