2016-04-13 14 views
0

コンボボックスで構成されるネットビーンのフォームを設計しました。私はコンボボックスにあらかじめ設定された値が必要です。私はメソッドから文字列のarraylistの形式でこれらの値を取得しています。今私はコンボボックスでそれらを追加しようとしています。設定されていますが、表示されません。私はそれに問題が何かを得ていない。netbeansのJcomboboxで項目を設定する方法

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.digitate.agent.bp.ui; 

import com.digitate.agent.bp.ingestor.translation.service.TranslationSpecificationProducer; 
import java.util.List; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 

/** 
* 
* @author 125529 
*/ 
public class MainFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame1 
    */ 
    public MainFrame() { 
     initComponents(); 
     //addTemplatesToComboBox(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 
    private void initComponents() { 

     jToolBar1 = new javax.swing.JToolBar(); 
     jButton1 = new javax.swing.JButton(); 
     jSplitPane1 = new javax.swing.JSplitPane(); 
     jScrollPane3 = new javax.swing.JScrollPane(); 
     jList2 = new javax.swing.JList<>(); 
     jPanel1 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     translationName = new javax.swing.JTextField(); 
     jLabel4 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel5 = new javax.swing.JLabel(); 
     jScrollPane2 = new javax.swing.JScrollPane(); 
     mappingTable = new javax.swing.JTable(); 
     destinationTemplateCombo = new javax.swing.JComboBox<>(); 
     sourceFileNameCombo = new javax.swing.JComboBox<>(); 
     submitButton = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jToolBar1.setRollover(true); 

     jButton1.setText("+"); 
     jButton1.setFocusable(false); 
     jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 
     jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 
     jToolBar1.add(jButton1); 

     jList2.setModel(new javax.swing.AbstractListModel<String>() { 
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; 
      public int getSize() { return strings.length; } 
      public String getElementAt(int i) { return strings[i]; } 
     }); 
     jList2.setMaximumSize(new java.awt.Dimension(100, 200)); 
     jList2.setMinimumSize(new java.awt.Dimension(100, 200)); 
     jScrollPane3.setViewportView(jList2); 

     jSplitPane1.setLeftComponent(jScrollPane3); 

     jLabel1.setText("Translation Name"); 

     translationName.setToolTipText(""); 

     jLabel4.setText("Source File"); 

     jLabel2.setText("Destination Template"); 

     jLabel5.setText("Mappings"); 

     mappingTable.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] { 
       {null, null, null} 
      }, 
      new String [] { 
       "Destination Field", "Destination Type", "Path/Constant/Script" 
      } 
     ) { 
      Class[] types = new Class [] { 
       java.lang.String.class, java.lang.Object.class, java.lang.String.class 
      }; 
      boolean[] canEdit = new boolean [] { 
       false, true, true 
      }; 

      public Class getColumnClass(int columnIndex) { 
       return types [columnIndex]; 
      } 

      public boolean isCellEditable(int rowIndex, int columnIndex) { 
       return canEdit [columnIndex]; 
      } 
     }); 
     mappingTable.setColumnSelectionAllowed(true); 
     jScrollPane2.setViewportView(mappingTable); 
     mappingTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION); 

     destinationTemplateCombo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "\"abc\"", "\"xxxc\"", "\"abx\"" })); 
     destinationTemplateCombo.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       destinationTemplateComboActionPerformed(evt); 
      } 
     }); 

     sourceFileNameCombo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); 

     submitButton.setText("Submit"); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addContainerGap() 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(sourceFileNameCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(translationName, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jLabel2) 
          .addComponent(jLabel5) 
          .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(destinationTemplateCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGap(245, 245, 245) 
         .addComponent(submitButton))) 
       .addContainerGap(21, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(translationName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jLabel4) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(sourceFileNameCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(8, 8, 8) 
       .addComponent(jLabel2) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(destinationTemplateCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(58, 58, 58) 
       .addComponent(jLabel5) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(34, 34, 34) 
       .addComponent(submitButton) 
       .addContainerGap(45, Short.MAX_VALUE)) 
     ); 

     jSplitPane1.setRightComponent(jPanel1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGap(532, 532, 532)) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jSplitPane1) 
       .addGap(6, 6, 6)) 
     ); 

     pack(); 
    }// </editor-fold>//GEN-END:initComponents 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed 
     // TODO add your handling code here: 
    }//GEN-LAST:event_jButton1ActionPerformed 

    private void destinationTemplateComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destinationTemplateComboActionPerformed 
     // TODO add your handling code here: 
    }//GEN-LAST:event_destinationTemplateComboActionPerformed 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Windows".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 
     //</editor-fold> 
       MainFrame mainFrame = new MainFrame(); 
     //  mainFrame.addSourceFileToCombobox(); 
       mainFrame.addTemplatesToComboBox(); 
     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 

       new MainFrame().setVisible(true); 


      } 
     }); 
    } 


    //add Template List in destinationTemplateCombo 
    public void addTemplatesToComboBox(){ 
     TranslationSpecificationProducer translationSpecificationProducer = new TranslationSpecificationProducer(); 
     List<String> templateLists = translationSpecificationProducer.getTemplates(); 
     // String[] items = (String[]) templateLists.toArray(); 
     DefaultComboBoxModel model = new DefaultComboBoxModel(); 
     for(String template: templateLists){ 
     model.addElement(template); 
     } 
     destinationTemplateCombo.setModel(model); 
     setVisible(true); 
     //System.out.println("Total Items"+destinationTemplateCombo.getItemCount()); 
     //add(destinationTemplateCombo); 
    } 

    // Variables declaration - do not modify//GEN-BEGIN:variables 
    private javax.swing.JComboBox<String> destinationTemplateCombo; 
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JLabel jLabel5; 
    private javax.swing.JList<String> jList2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JScrollPane jScrollPane3; 
    private javax.swing.JSplitPane jSplitPane1; 
    private javax.swing.JToolBar jToolBar1; 
    private javax.swing.JTable mappingTable; 
    private javax.swing.JComboBox<String> sourceFileNameCombo; 
    private javax.swing.JButton submitButton; 
    private javax.swing.JTextField translationName; 
    // End of variables declaration//GEN-END:variables 
} 

答えて

0

問題は移入されませんあなたの避難所あなたのcombobxある

に記載されている3つのアプローチがありますjava docs

最初のアプローチ - コンストラクタでinitComponents()を呼び出した後は、あなたのモデルを構築するためにあなたのコードを追加します。それを設定するにはdestinationTemplateComb.setModel(myModel)を呼び出します。だから、コンストラクタは次のようになります。

public SomeClass() { 
    initComponents(); 
    String [] myString = {"Item 1","Item 2","Item 3"}; 
    } 
    destinationTemplateCombo(new javax.swing.DefaultComboBoxModel(myString)); 
} 

第二のアプローチ:

destinationTemplateCombo.addItem(のmyString [0]);

第3のアプローチ:

destinationTemplateCombo =新しいjavax.swing.JComboBoxのメソッド(のmyString)。

最初のアプローチは非常に柔軟性があり、他の

0

私は方法から、文字列の配列リストの形でこれらの値を取得しています。

まあ、APIには、ArrayListからコンボボックスにデータを移入するメソッドはありません。

したがって、単純なループを使用して、ArrayListからコンボボックスにデータをコピーする必要があります。コンボボックスにアイテムを追加するには、JComboBoxのメソッドJComboBoxを使用します。

ArrayList<String> items = ... 

JComboBox<String> comboBox = new JComboBox<String>(); 

for (String item: items) 
    // add the item to the combo box 
関連する問題