hi, I am having a problem with adding button on jTable for each data I get from the database.
here is what I get from database in SQLite

 try{ 
     String sql = "select ClassName as 'Class Name', NumberOfStudents as 'Number of Students' from ClassRooms";
     pst=conn.prepareStatement(sql);      
     rs=pst.executeQuery();
     Data_Table1.setModel(DbUtils.resultSetToTableModel(rs));
     if(rs.next){
         //code to add the button here.
     }
    }

    catch(Exception e){
    JOptionPane.showMessageDialog(null, e);

    } finally{
    try{
       rs.close();
       pst.close();                
    }
        catch(Exception e){        
        }                
    }

I am not understanding how to make the button code?

new JButton("added button");

creates a Button. can you be more specific about what you try to do, and how you've tried it so far?

lets say in a Table called Students in SQLite Database, are a bunch of students.
about 30 students, they all are showin in a jTable, when teacher click a row, it will show a button in the last row, similar with the pic listed bellow 1.PNG

but not exactly like that. only 1 button to be created when click 1 row. when click other, it has to be hiden, or deleted, and create the new for the new row

Also, give it a function. when teacher click that button, it show a JOptionPane with the name of the student

Have a look at Oracle's detailed tutorial on JTables
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
in the section on Custom Renderers you will learn how to replace cells in the table with the controls of your choice - in this case you need to check if the cell is in a currently selected row then if so use a JButton as the renderer, otherwize leave the cell empty or visible(false)

You can add an action listener to the JButton you use as the custom renderer, so responding to it to open an OptionPane or whetever is just the same as for any other button.

  1. about last column ( "Accept/Reject" )

    • how can I reject ??? with simple JButton
    • JOptionPane should be always wrapped into invokeLater - moved to the end of EDTs queue
  2. when teacher click a row

    • override isSelected in your Xxx(Table)CellRenderer / prepareRenderer, there is place to create painting for JButton
    • JButton is just illusion created, painted by XxxRenderer
    • JTable has Renderer (painting illusions) and Editor (real, semi, temporarry JComponents)
  3. everything depend of your code, otherwise search for Java + Swing + JTable + JButton + Editor, good JButton as TableCellEditor has Swing Action as accelator,

  4. don't put JButton to the XxxTableModel, there is stored value for editor / renderer, just string "Accept" in your case,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.