我正在制作一个添加学生和教师数据的程序。我想通过选择单选按钮
在学生或教师的桌子上显示数据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){
}
}
如果我选择表格更改的单选按钮,我想做。我需要按一下按钮吗?
答案 0 :(得分:0)
我不太了解Swing(我上学时最后一次使用它),但似乎你需要在你的单选按钮上添加一个监听器。
这里解释了这个概念: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
我希望这有帮助!
如果我有任何错误,请告诉我,因为我已经触及任何与摇摆相关的事情多年了!
答案 1 :(得分: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).
}
}