簡體   English   中英

從數據庫加載時顯示Jlist中的值

[英]To show values in Jlist from DataBase onload

[從數據庫加載時顯示Jlist中的值] [Swing GUI和數據庫`

初始化代碼:

 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);
         }

    }

在組合框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



    }

單擊時對add(>>)按鈕執行的操作:

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);
         }

    }

對remove(<<)按鈕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);
     }
}     

我已經完成了添加和刪除按鈕的工作。 但是現在我被困在視圖部分。 我正在做的是從數組列表中填充第一個Jlist。 當我從此jlist添加任何元素時,將其添加到第二個JList中並插入到DB中,類似在remove操作上,它的操作相反。

現在我需要的是,我已經在第二個Jlist中添加的那些值,不應在加載時顯示在第一個Jlist中,反之亦然,並且應該根據選擇分別顯示這些值。 選擇框在這里也起着重要的作用,因為我想根據所選值在jlist中顯示值。

例如,如果我將com1存儲為“ prl:1”,那么它應該僅在第二個Jlist onload中顯示。 如果我想更新此列表,則應在數據庫中完成並同樣顯示。

我沒有看到部分的流程。 我已經將所有內容存儲在DB中,我只想映射這些東西並相應地顯示它們。

請幫幫我...因為我無法繼續下一步...`] 2

如果我正確理解了您的問題,則希望獲得兩個JList組件的初始化以使其正常工作。 應該基於數據庫填充目標列表,對嗎? 可以這樣工作( initMethod方法的一部分):

// [...]

//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);

// [...]

源列表中是否應該填充listData變量中的所有內容,數據庫中的組除外? (我將listData變量重命名為下面的allGroups 。)此列表的初始化看起來像這樣(也是initMethod方法的一部分):

// [...]

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

// [...]

我尚不清楚PortSelectionBox的作用。 “ COM1”和“ COM2”是否與數據庫中的某些記錄相關(例如“ prl:1”和“ prl:2”)?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM