私は生徒と教師のデータを追加するプログラムを作っています。ラジオボタンを選択して生徒や教師用のテーブルにデータを表示したいラジオボタンの作り方テーブルのデータを変更する
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
をあなたが(table.revalidate 'しようとしています)や' 'table.repaint()' 'のモデルに変更を加えた後JTable'。また、同じモデルオブジェクトを変更しても、ビューの値の変更は保証されません。代わりに、新しい 'DefaultTableModel'オブジェクトを作成するか、それを空にしてから使用してください。このリンクはあなたを助けるかもしれません。 http://stackoverflow.com/a/30117380/1540330 –
@VighaneshGursale既に、そのdoesntの仕事 –