简体   繁体   中英

JList filled with a vector not showing anything

I am trying to fill a JList with some data that I get from a DB, for the purpose I first put the data into a vector and then I call the JList constructor with that vector as a parameter. When I display the list in the interface it appears empty, which it's senseless to me, I check that the vector has the correct data by printing it on the console, and so it does.

Here is the specific code snipet, and later the complete class.

Specific snipet:

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

And here is the entire class, the Jlist declaration is at the end:

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
}

Thanks everyone.

You need to set the viewportview AFTER creating the JList.

Like this:

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

jScrollPane1.setViewportView(jList1); //Set viewport

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