JList边框错误

时间:2015-05-14 18:34:38

标签: java jframe jlist

JList listaMovimentacao = new JList();
listaMovimentacao.setListData(dados.toArray());
System.out.println(listaMovimentacao);
listaMovimentacao.setVisible(true);

我在初始化我的屏幕的函数中运行此代码,变量dados是一个arrayList字符串,这是来自SQL数据库中一个表的列的数据,当我运行我的代码时,我的JList不显示项目它位于String arrayList中,低于出现的错误:

    javax.swing.JList[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0]

整个代码下方:

package controle_financeiro;

import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListModel;


public class TelaCadastroMovimentação extends javax.swing.JFrame {

    public TelaCadastroMovimentação() {
        initComponents();
        ArrayList<String> dados = new ArrayList<String>();  
            String databaseURL = "jdbc:postgresql://localhost/db_controle_financeiro?user=us_iago&password=123";
            String driverName = "org.postgresql.Driver";
            Connection con;   
        try {
            Class.forName(driverName).newInstance();
            con = DriverManager.getConnection(databaseURL); 
            System.out.println("Conexão obtida com sucesso.");
            PreparedStatement input = null;  
            Statement st = con.createStatement();
            String sql = "Select nome from categoria"; 
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){  
                dados.add(rs.getString("nome"));
            }

            JList listaMovimentacao = new JList();
            listaMovimentacao.setListData(dados.toArray());
            System.out.println(listaMovimentacao);
            listaMovimentacao.setVisible(true);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        } catch (Exception e) {
            System.out.println("Problemas ao tentar conectar com o banco de dados: " + e);
        }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        TextFieldDescricao = new javax.swing.JTextField();
        dataMovimentacao = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaMovimentacao = new javax.swing.JList();
        botao_voltar = new javax.swing.JButton();
        botao_cadastrar = new javax.swing.JButton();
        valorMovimentacao = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel1.setText("Descrição");

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel2.setText("Data");

        jLabel3.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel3.setText("Valor (R$)");

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel4.setText("Categoria");

        TextFieldDescricao.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        TextFieldDescricao.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        TextFieldDescricao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextFieldDescricaoActionPerformed(evt);
            }
        });

        try {
            dataMovimentacao.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        dataMovimentacao.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        dataMovimentacao.setText("dd/mm/aaaa    ");
        dataMovimentacao.setToolTipText("dd/mm/aaaa");
        dataMovimentacao.setFocusTraversalPolicyProvider(true);
        dataMovimentacao.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N

        listaMovimentacao.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listaMovimentacao.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaMovimentacaoValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listaMovimentacao);

        botao_voltar.setText("Voltar");
        botao_voltar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botao_voltarMouseClicked(evt);
            }
        });

        botao_cadastrar.setText("Cadastrar");
        botao_cadastrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botao_cadastrarMouseClicked(evt);
            }
        });

        valorMovimentacao.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        valorMovimentacao.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        valorMovimentacao.setToolTipText("");
        valorMovimentacao.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(valorMovimentacao, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botao_voltar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(botao_cadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1)
                    .addComponent(dataMovimentacao, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TextFieldDescricao)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TextFieldDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dataMovimentacao, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(valorMovimentacao, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botao_voltar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botao_cadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22))
        );

        dataMovimentacao.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>                        

    private void botao_cadastrarMouseClicked(java.awt.event.MouseEvent evt) {                                             

        String data_movimentacao = (dataMovimentacao.getText()); 
        Date data = null;  
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
        try {  
               format.setLenient(false);  
               data = format.parse(data_movimentacao);  
        } catch (ParseException e) {  
              JOptionPane.showMessageDialog(null,"Data inválida!","AVISO",JOptionPane.WARNING_MESSAGE);  
        }  

        String svalor_movimentacao = (valorMovimentacao.getText());
        float valor_movimentacao = Float.parseFloat(svalor_movimentacao);

        String descricao_movimentacao = (TextFieldDescricao.getText());



        TelaInicial tela_inicial_ccategoria = new TelaInicial();
        tela_inicial_ccategoria.setVisible(true);  
        dispose();

    }                                            

    private void botao_voltarMouseClicked(java.awt.event.MouseEvent evt) {                                          
      TelaInicial tela_inicial_ccategoria = new TelaInicial();
        tela_inicial_ccategoria.setVisible(true);  
        dispose();
    }                                         

    private void TextFieldDescricaoActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        String descricao_movimentação = (TextFieldDescricao.getText()); 

    }                                                  

    private void listaMovimentacaoValueChanged(javax.swing.event.ListSelectionEvent evt) {                                               
        String selected = listaMovimentacao.getSelectedValue().toString();
    }                                              



    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCadastroMovimentação().setVisible(true);
            }
        });

    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField TextFieldDescricao;
    private javax.swing.JButton botao_cadastrar;
    private javax.swing.JButton botao_voltar;
    private javax.swing.JFormattedTextField dataMovimentacao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaMovimentacao;
    private javax.swing.JFormattedTextField valorMovimentacao;
    // End of variables declaration                   
}

0 个答案:

没有答案