2016-05-12 7 views
0

私のプログラムでは、ユーザーが編集したいユーザーを選択できるコンボボックスがあります。現在、コンボボックスを埋めるために使用するクラスは実行していません。私はこのウェブサイト上のいくつかの同様の問題を見てきましたが、私が間違っていることを見逃しました。誰かが問題を解決することができれば、それは高く評価されます。私はまだこれのほとんどに全く新しいです。JavaコンボボックスがSQLから塗りつぶされていない

import java.awt.EventQueue; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

import javax.swing.JComboBox; 
import javax.swing.JFrame; 

public class AdminPanelDelUser { 


private String Host = "Hidden"; 
private String Name = "Hidden"; 
private String Pass = "Hidden"; 

private JComboBox<String> userPicker; 
private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       AdminPanelDelUser window = new AdminPanelDelUser(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public AdminPanelDelUser() { 
    initialize(); 
    getUserPicker(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 300, 352); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JComboBox<String> userPicker = new JComboBox<String>(); 
    userPicker.setBounds(59, 6, 235, 27); 
    frame.getContentPane().add(userPicker); 
} 

public JComboBox<String> getUserPicker() { 
    try { 
     Connection conn = DriverManager.getConnection(Host, Name, Pass); 
     PreparedStatement pst = conn.prepareStatement("SELECT * From `table_1`"); 
     ResultSet rs = pst.executeQuery(); 
     while(rs.next()) { 
      String name =rs.getString("user_name"); 
      userPicker.addItem(name); 
     } 
    } 
    catch (Exception e) { 
     //Place Pop Warning Later 
    } 
    return userPicker; 
} 
} 

また、私がコードをさらに改善する方法についての提案は、喜んで行われます。この問題を解決するのに役立つ人に感謝します。

答えて

0

あなたの問題がある働いています。内容ペインに追加したJComboBoxは、アイテムを追加しようとしたものとは異なります(getUserPicker()が呼び出された場合はnullです)。

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

は、あなたは何が起こっているかのより良い理解を得るためにhttp://www.java-made-easy.com/variable-scope.htmlを読みたいかもしれません

userPicker = new JComboBox<String>(); 

でなければなりません。

+0

このおかげで助けてくれてありがとう! –

0

変更が初期化機能で行われました。新しいオブジェクトuserPickerを作成しているため、値は設定されていません。次のコードは、あなたがuserPicker JComboBoxを作成し、メソッドレベルのスコープで変数に代入の代わりに、クラスレベルのスコープを持つ変数を使用していることを

package com.mylender.test; 
import java.awt.EventQueue; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 

public class AdminPanelDelUser { 



private JComboBox<String> userPicker; 
private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       AdminPanelDelUser window = new AdminPanelDelUser(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public AdminPanelDelUser() { 
    initialize(); 
    getUserPicker(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 300, 352); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    //JComboBox<String> userPicker = new JComboBox<String>(); 
    userPicker = new JComboBox<String>(); 
    userPicker.setBounds(59, 6, 235, 27); 
    frame.getContentPane().add(userPicker); 
} 

public void getUserPicker() { 
    try { 

     String a[]= {"HELLO","WORLD"}; 
     for(String s:a) 
      userPicker.addItem(s); 

    } 
    catch (Exception e) { 
     //Place Pop Warning Later 
    } 

} 
} 
関連する問題