1

DBから取得したデータをJListに入力しようとしています。目的のために、最初にデータをベクターに入れ、次にそのベクターをパラメーターとしてJListコンストラクターを呼び出します。インターフェイスにリストを表示すると、空のように見えますが、これは私には無意味です。コンソールに出力して、ベクトルに正しいデータがあることを確認します。

これが特定のコードスニペットであり、後で完全なクラスです。

特定のスニペット:

query = "SELECT * FROM SENSORES WHERE IDDRONE = " + idDrone;
    rs = sentencia.executeQuery(query);
    String temp;
    Vector<String> vector = new Vector<String>();
    while(rs.next())
    {
        temp = rs.getString("ID") + " " + rs.getString("DESCRIPCION");
        vector.add(temp);
    }

    jList1 = new JList<String>(vector);
    System.out.println(vector);

そして、これがクラス全体です。Jlist宣言は最後にあります。

package UI;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JList;

import sqlConnection.DatabaseConnection;


public class DeustoDronesJFrame extends javax.swing.JFrame {

/**
 * Creates new form DeustoDronesJFrame
 * @throws SQLException 
 */
public DeustoDronesJFrame() throws SQLException {
    initComponents();
}

/**
 * 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.
 * @throws SQLException 
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() throws SQLException {

    jLabel2 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();

    jLabel1 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel2.setFont(new java.awt.Font("Times New Roman", 2, 18)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(0, 0, 153));



    jScrollPane1.setViewportView(jList1);
    Statement sentencia = DatabaseConnection.getInstance().createStatement();
    String query = "SELECT * FROM DRONES";
    ResultSet rs = sentencia.executeQuery(query);
    String idDrone = rs.getString("IDDRONE");
    String estadoGPS = rs.getString("ESTADOGPS");
    System.out.println(estadoGPS);
    if (estadoGPS.equals(estadoGPS))
        estadoGPS = "OFF";
    else 
        estadoGPS = "ON";


    query = "SELECT * FROM SENSORES WHERE IDDRONE = " + idDrone;
    rs = sentencia.executeQuery(query);
    String temp;
    Vector<String> vector = new Vector<String>();
    while(rs.next())
    {
        temp = rs.getString("ID") + " " + rs.getString("DESCRIPCION");
        vector.add(temp);
    }

    jList1 = new JList<String>(vector);
    System.out.println(vector);

    jLabel1.setText("jLabel1");
    jLabel2.setText("Drone" + idDrone);
    jLabel3.setText("GPS " + estadoGPS);

    jButton1.setText("CAMBIAR ESTADO GPS");

    jButton2.setText("CAMBIAR ESTADO SENSOR");

    jButton3.setText("HISTORICO SENSOR");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(24, 24, 24)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jLabel2)
                .add(layout.createSequentialGroup()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 328, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(42, 42, 42)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .add(layout.createSequentialGroup()
                    .add(12, 12, 12)
                    .add(jLabel1)
                    .add(84, 84, 84)
                    .add(jLabel3)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(jButton1)))
            .addContainerGap(88, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(26, 26, 26)
                    .add(jLabel2)
                    .add(24, 24, 24)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel1)
                        .add(jLabel3)))
                .add(layout.createSequentialGroup()
                    .add(63, 63, 63)
                    .add(jButton1)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 62, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(69, 69, 69))
                .add(layout.createSequentialGroup()
                    .add(59, 59, 59)
                    .add(jButton2)
                    .add(52, 52, 52)
                    .add(jButton3)
                    .addContainerGap())))
    );

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

/**
 * @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 ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(DeustoDronesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(DeustoDronesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(DeustoDronesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(DeustoDronesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {
                new DeustoDronesJFrame().setVisible(true);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}

みんな、ありがとう。

4

1 に答える 1

1

JListを作成した後、viewportviewを設定する必要があります。

このような:

jList1 = new JList<String>(vector);   //First create list

jScrollPane1.setViewportView(jList1); //Set viewport
于 2012-05-27T21:27:55.350 に答える