简体   繁体   English

从数据库加载时显示Jlist中的值

[英]To show values in Jlist from DataBase onload

[To show values in Jlist from DataBase onload][Swing GUI and Database` [从数据库加载时显示Jlist中的值] [Swing GUI和数据库`

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: 在组合框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: 单击时对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);
         }

    }

,

action performed on remove(<<) button onclick: 对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);
     }
}     

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. 我正在做的是从数组列表中填充第一个Jlist。 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. 当我从此jlist添加任何元素时,将其添加到第二个JList中并插入到DB中,类似在remove操作上,它的操作相反。

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. 现在我需要的是,我已经在第二个Jlist中添加的那些值,不应在加载时显示在第一个Jlist中,反之亦然,并且应该根据选择分别显示这些值。 also the selection box plays an important role here as i want to show the values in the jlist according to the selected value. 选择框在这里也起着重要的作用,因为我想根据所选值在jlist中显示值。

For example if i have stored com1 with "prl : 1" then it should be shown only in second Jlist onload. 例如,如果我将com1存储为“ prl:1”,那么它应该仅在第二个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 . 我已经将所有内容存储在DB中,我只想映射这些东西并相应地显示它们。

Please help me out... as i am not able to get what to do next... `] 2 请帮帮我...因为我无法继续下一步...`] 2

If I understand your question correctly, you want to get the initialization of the two JList components to work properly. 如果我正确理解了您的问题,则希望获得两个JList组件的初始化以使其正常工作。 The destination list should be filled based on the database, right? 应该基于数据库填充目标列表,对吗? This could work like this (part of the initMethod method): 可以这样工作( 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);

// [...]

Should the source list be filled with everything from the listData variable, except groups that are in the database? 源列表中是否应该填充listData变量中的所有内容,数据库中的组除外? (I have renamed the listData variable to allGroups below.) Initialization for this list could look like this (also part of the initMethod method): (我将listData变量重命名为下面的allGroups 。)此列表的初始化看起来像这样(也是initMethod方法的一部分):

// [...]

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. 我尚不清楚PortSelectionBox的作用。 Do "COM1" and "COM2" relate to certain records in the database (like "prl : 1" and "prl : 2")? “ COM1”和“ COM2”是否与数据库中的某些记录相关(例如“ prl:1”和“ prl:2”)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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