[英]How to add checkbox if i fetch the record from Database
Class Name:-AddAccountDao.Java 类名称:-AddAccountDao.Java
public class AddAccountDao { public static List<AddAccount> view(){ List <AddAccount> list=new ArrayList<>(); try{ Connection con=getCon(); PreparedStatement pst=con.prepareStatement("SELECT * FROM adminaccount"); ResultSet rst=pst.executeQuery(); while(rst.next()){ AddAccount ad=new AddAccount(); ad.setId(rst.getInt(1)); ad.setName(rst.getString(2)); ad.setPassword(rst.getString(3)); ad.setEmail(rst.getString(4)); ad.setContact(rst.getString(5)); list.add(ad); } con.close(); } catch(Exception e){ System.out.println(e); } return list; } }
Class Name:-AdminViewAccount 类名称:-AdminViewAccount
public class AdminViewAccount extends JFrame{ static AdminViewAccount frame; public AdminViewAccount(){ List<AddAccount> list=AddAccountDao.view(); int size=list.size(); String data[][]=new String[size][6]; int row=0; for(AddAccount ad:list){ data[row][1]=String.valueOf(ad.getId()); data[row][2]=ad.getName(); data[row][3]=ad.getPassword(); data[row][4]=ad.getEmail(); data[row][5]=ad.getContact(); row++; } String columnName[]= {"Select","ID","Name","Password","Email","Contact No."}; JTable t=new JTable(data,columnName); JScrollPane sp=new JScrollPane(t); add(sp); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100,100,800,400); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ @Override public void run() { try{ frame=new AdminViewAccount(); frame.setVisible(true); } catch(Exception e){ e.printStackTrace(); } } }); } }
Class Name:-AddAccount 类名称:-AddAccount
public class AddAccount { private int id; private String name,password,email,contact; public AddAccount(){} public AddAccount(String name,String password,String email,String contact){ super(); this.name=name; this.password=password; this.email=email; this.contact=contact; } public void setId(int id){ this.id=id; } public int getId(){ return id; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setPassword(String password){ this.password=password; } public String getPassword(){ return password; } public void setEmail(String email){ this.email=email; } public String getEmail(){ return email; } public void setContact(String contact){ this.contact=contact; } public String getContact(){ return contact; } }
One way is to create a custom TableModel that uses your TableModel and adds a check box column at the start of the table: 一种方法是创建一个使用TableModel的自定义TableModel,并在表的开头添加一个复选框列:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class CheckBoxWrapperTableModel extends AbstractTableModel
{
private ArrayList<Boolean> checkBoxes = new ArrayList<>();
private DefaultTableModel model;
private String columnName;
public CheckBoxWrapperTableModel(DefaultTableModel model, String columnName)
{
this.model = model;
this.columnName = columnName;
for (int i = 0; i < model.getRowCount(); i++)
checkBoxes.add( Boolean.FALSE );
}
@Override
public String getColumnName(int column)
{
return (column > 0) ? model.getColumnName(column - 1) : columnName;
}
@Override
public int getRowCount()
{
return model.getRowCount();
}
@Override
public int getColumnCount()
{
return model.getColumnCount() + 1;
}
@Override
public Object getValueAt(int row, int column)
{
if (column > 0)
return model.getValueAt(row, column - 1);
else
{
Object value = checkBoxes.get(row);
return (value == null) ? Boolean.FALSE : value;
}
}
@Override
public boolean isCellEditable(int row, int column)
{
if (column > 0)
return model.isCellEditable(row, column - 1);
else
return true;
}
@Override
public void setValueAt(Object value, int row, int column)
{
if (column > 0)
model.setValueAt(value, row, column - 1);
else
{
checkBoxes.set(row, (Boolean)value);
}
fireTableCellUpdated(row, column);
}
@Override
public Class getColumnClass(int column)
{
return (column > 0) ? model.getColumnClass(column - 1) : Boolean.class;
}
public void removeRow(int row)
{
checkBoxes.remove(row);
fireTableRowsDeleted(row, row);
model.removeRow(row);
}
private static void createAndShowGUI()
{
// Create the table with check marks in the first column
DefaultTableModel model = new DefaultTableModel(5, 1);
for (int i = 0; i < model.getRowCount(); i++)
{
model.setValueAt("" + i, i, 0);
}
CheckBoxWrapperTableModel wrapperModel = new CheckBoxWrapperTableModel(model, "Select");
JTable table = new JTable(wrapperModel);
// Add button to delete selected rows
JButton button = new JButton( "Delete Selected Rows" );
button.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
for (int i = table.getRowCount() - 1; i >= 0; i--)
{
Boolean selected = (Boolean)table.getValueAt(i, 0);
System.out.println(selected + " : " + i);
if (selected)
{
wrapperModel.removeRow(i);
}
}
}
});
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane( table ) );
frame.add( button, BorderLayout.PAGE_END );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
Or the other approach, since you are just copying the data to the TableModel is to just add an extra column when you copy the data: 或另一种方法,因为您只是将数据复制到TableModel中,所以只需在复制数据时添加一个额外的列即可:
data[row][0] = Boolean.FALSE; // added
data[row][1]=String.valueOf(ad.getId());
You would then also need to override the getColumnClass(...)
method of your TableModel to return Boolean.class
for the first column as demonstrated in the TableModel above. 然后,您还需要重写TableModel的
getColumnClass(...)
方法以为第一列返回Boolean.class
,如上面的TableModel中所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.