2016-05-12 78 views
0

在我的程序中,我有一個ComboBox,用戶可以在其中選擇他想要編輯的用戶,目前我用來填充ComboBox的類沒有這樣做。我在網站上看到了一些類似的問題,但沒有看到我做錯了什麼。如果有人可以提供解決這個問題,將不勝感激。我對這個大多數人還是比較陌生的。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

你的問題是,你正在創建的userPicker JComboBox,並將它與方法級別範圍賦值給一個變量,而不是使用具有一流水平範圍變量。您添加到內容窗格的JComboBox與嘗試添加項目的項目不同(在調用getUserPicker()時偶然爲空)。

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

應該

userPicker = new JComboBox<String>(); 

您可能需要閱讀http://www.java-made-easy.com/variable-scope.html,以獲得更好的理解到底是怎麼回事的。

+0

感謝您的幫助設法得到這個排序! –

0

更改在初始化函數中完成。這些值未設置,因爲您正在創建一個新對象userPicker。下面的代碼工作

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 
    } 

} 
}