2016-03-21 46 views
1

我正在製作一個用於添加學生和教師數據的程序。我想,以示對學生或教師上表中的數據通過選擇單選按鈕如何使單選按鈕更改表格數據

public void paneling(){ 
    panell = new JPanel(new BorderLayout()); 
    DefaultTableModel model = new DefaultTableModel(); 
     JTable table = new JTable(); //make the table 
     table.setModel(model); 
     table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
     table.setFillsViewportHeight(true); 
     JScrollPane scroll = new JScrollPane(table); 
     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn = DriverManager.getConnection(url, username, password); 

     }catch(Exception e){ 
      JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
     } 
    panell.add(scroll,BorderLayout.CENTER); 
    panel4 = new JPanel(); 
    panel4.setLayout(new FlowLayout()); 
    rb_siswa = new JRadioButton("Siswa"); 
    rb_dosen = new JRadioButton("Dosen"); 
    ButtonGroup bg = new ButtonGroup(); 
    bg.add(rb_siswa); 
    bg.add(rb_dosen); 
    rb_siswa.setSelected(true); //i set RadioButton siswa default 
    panel4.add(rb_siswa); 
    panel4.add(rb_dosen); 
    panell.add(panel4,BorderLayout.NORTH); 
    if(rb_siswa.isSelected()){ //the first if 
     String[] columnNames= {"NIM", "ID_Jurusan", "ID_Kelas", "Name", "Tanggal_Lahir", "Gender", "Semester", "Alamat", "email", "nohp"}; 
     model.setColumnIdentifiers(columnNames); 
     try{ 
     sql = "select * from siswa"; 
      PreparedStatement ps = conn.prepareStatement(sql); 
      ResultSet rs = ps.executeQuery(); 
      while(rs.next()){ 
       NIM = rs.getString("NIM"); 
       IDJurusan = rs.getString("id_jurusan"); 
       IDKelas = rs.getString("id_kelas"); 
       Name = rs.getString("Nama"); 
       TL = rs.getString("TanggalLahir"); 
       gender = rs.getString("JenisKelamin"); 
       Semester = rs.getString("Semester"); 
       alamat = rs.getString("Alamat"); 
       email = rs.getString("e-mail"); 
       nohp = rs.getString("nohp"); 
       model.addRow(new Object[]{NIM, IDJurusan, IDKelas, Name, TL, gender, Semester, alamat, email, nohp}); 
      } 
     }catch(Exception aae){ 
      JOptionPane.showMessageDialog(null, aae.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
     } 
    } 
    else if(rb_dosen.isSelected()){ // the second if 
     String[] columnNames= {"ID_Dosen", "Name", "Gender", "Alamat", "email", "nohp"}; 
     model.setColumnIdentifiers(columnNames); 
     try{ 
      sql = "select * from siswa"; 
      PreparedStatement ps = conn.prepareStatement(sql); 
      ResultSet rs = ps.executeQuery(); 
      while(rs.next()){ 
       IDDosen = rs.getString("IDDosen"); 
       Name = rs.getString("Nama"); 
       gender = rs.getString("JenisKelamin"); 
       alamat = rs.getString("Alamat"); 
       email = rs.getString("email"); 
       nohp = rs.getString("nohp"); 
       model.addRow(new Object[]{IDDosen, Name, gender, alamat, email, nohp}); 
      } 
     }catch(Exception aae){ 
    } 
    } 

我想打,如果我選擇單選按鈕表變化。我需要做一個按鈕?

this is how the table looks like, hope can help you imagine

+0

你有沒有試過'table.revalidate()'或'table.repaint()'進行更改的'模型後JTable'。請記住,更改相同的模型對象並不保證視圖值的更改。相反,在使用它之前創建一個新的'DefaultTableModel'對象或將其設爲空。此鏈接可能對您有所幫助。 http://stackoverflow.com/a/30117380/1540330 –

+0

@VighaneshGursale已經和它不工作 –

回答

0

我不知道一噸左右搖擺(上次使用它時,我在學校),但似乎你需要一個聽衆添加到您的單選按鈕。

這一概念在這裏解釋:http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningtoJRadioButtonEventswithaChangeListener.htm

基本上你想創建一個變化監聽像這樣:

ChangeListener changeListener = new ChangeListener() { 
    public void stateChanged(ChangeEvent changEvent) { 
    AbstractButton aButton = (AbstractButton)changEvent.getSource(); 
    ButtonModel aModel = aButton.getModel(); 
    // Code to change table here 
    } 
}; 

你會然後把你的改變監聽器,並把它添加到您的按鈕,像這樣的:

rb_siswa.addChangeListener(changeListener); 
rb_dosen.addChangeListener(changeListener); 

這應該可能工作。它看起來像不能直接添加一個監聽器到ButtonGroup而不是按鈕。那是我的第一個想法。你可以返回選擇的ButtonModel(如果我正在閱讀的話),但ButtonGroup沒有它自己的偵聽器概念。

在這裏看到:https://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonGroup.html

我希望這有助於!

請讓我知道,如果我有什麼不對,因爲我已經觸摸任何鞦韆相關已經多年了!

+0

所以我需要把我的if代碼執行操作?並讓我的模型方法公開? –

0

您可以將ItemListener添加到單選按鈕。如果你只有兩個單選按鈕,然後避免任何Enumerator只需直接使用這樣的:

JRadioButton radio1 = new JRadioButton("Hello"); 
JRadioButton radio2 = new JRadioButton("World"); 

ButtonGroup btnGroup = new ButtonGroup(); 

// add listeners to button 
radio1.addActionListener(this); 
radio2.addActionListener(this); 

// add buttons to button group 
btnGroup.add(radio1); 
btnGroup.add(radio2); 

. 
. 
. 
// and implement the itemStateChanged() method as follows: 
public void itemStateChanged(ItemEvent e){ 

     if(radio1.isSelected()) 
     { 
      // create the default table model and then populate it to jtable and put the jtable into scroll bar (for student) 
     } 
     else { 
      // same as above (for teacher). 
     } 

} 
+0

相同,不工作,我嘗試添加System.out.print他們兩個,以確保單選按鈕是工作,但它不 –

+0

您可以請發佈更新後的代碼您所做的更改,以便我們可以瞭解您的問題面對。還要檢查在按鈕組中添加組件之前是否添加了偵聽器。 –

相關問題