1

i am making a program for add student and teacher data. i want to show the data on the table for student or teacher by select the radio button

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){
    }
    }

i want to make if i select the radio button the table change. am i need to make a button ?

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

Kira Katou
  • 158
  • 2
  • 2
  • 16
  • Have you tried to `table.revalidate()` or `table.repaint()` after making changes to model of `JTable`. Also keep in mind changing the same model object does not guarantee change in values of view. Instead create a new `DefaultTableModel` object or make it empty before using it. This link may help you. http://stackoverflow.com/a/30117380/1540330 – Vighanesh Gursale Mar 21 '16 at 18:59
  • @VighaneshGursale already, and its doesnt work – Kira Katou Mar 22 '16 at 04:53

2 Answers2

0

I don't know a ton about Swing (last used it when I was in school) but it seems that you need to add a listener to your radio buttons.

The concept is explained here: http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningtoJRadioButtonEventswithaChangeListener.htm

Basically you would want to create a change listener like such:

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

You would then take your change listener and add it to your buttons like such:

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

That should probably work. It looks like can't add a listener directly to the ButtonGroup and not the buttons. That was my first thought. You can return the ButtonModel of the selection (if I am reading that right) but the ButtonGroup does not have it's own listener concept.

See here: https://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonGroup.html

I hope this helps!

Please let me know if I got anything wrong since it has been many years since I have touched anything swing related!

njfife
  • 3,555
  • 1
  • 20
  • 31
0

You can add ItemListener to radio buttons. If you have only two radio buttons then avoid any Enumerator just directly use something like this:

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).
       }

}
Vighanesh Gursale
  • 921
  • 5
  • 15
  • 31
  • same, doesnt work and i try to add System.out.print on both of them to ensure that the radiobutton is work but it doesnt – Kira Katou Mar 23 '16 at 13:49
  • Could you please post the updated code what changes you made so that we can understand the problem you are facing. Also check if you added listener before adding the component in button group. – Vighanesh Gursale Mar 26 '16 at 19:47