public class RecuruitTest {
static class ButtonInJTable extends JFrame implements ActionListener
{
private JPanel topPanel, buttonPanel;
private JTable table;
private JScrollPane scrollPane;
private String[] columnNames= new String[3];
private String[][] dataValues ;
private EmployeeDAO eDAO = new EmployeeDAO();
private EmployeeVo emVo;
private ArrayList<String> title;
private ArrayList<EmployeeVo> list = new ArrayList<EmployeeVo>();
private JButton[] pageButton;
private PagingProcess pn;
TableModel model;
JButton button = new JButton();
public ButtonInJTable()
{
setTitle("Button in JTable Cell");
setSize(600,700);
topPanel= new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
button.addActionListener(this);
pageButton = new JButton[25];
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
pn = new PagingProcess();
eDAO = new EmployeeDAO();
pn.setPageNum(0);
int startPage = pn.getStartPage();
int endPage = pn.getEndPage();
list = eDAO.getEmployeeTotal(pn);
int rowCount=list.size();
columnNames=new String[] {"No","이름" , "직급" , "부서","이메일", "지원여부"};
dataValues = new String[25][columnNames.length];
for(int index = 0; index<rowCount; index++){
emVo=list.get(index);
dataValues[index][0]=emVo.getNo()+"";
dataValues[index][1]=emVo.getName();
dataValues[index][2]=emVo.getJobGrade();
dataValues[index][3]=emVo.getDepartment()+"";
dataValues[index][4]=emVo.getEmail();
dataValues[index][5]="지원하기";
}
for(int i = startPage; i<=endPage; i++){
pageButton[i] = new JButton((i)+"");
pageButton[i].addActionListener(this);
buttonPanel.add(pageButton[i]);
}
model=new DefaultTableModel(dataValues,columnNames);
table =new JTable( );
table.setModel(model);
table.getColumn("지원여부").setCellRenderer(new ButtonRenderer());
table.getColumn("지원여부").setCellEditor(new ButtonEditor(new JCheckBox()));
scrollPane=new JScrollPane(table);
topPanel.add(scrollPane,BorderLayout.CENTER);
topPanel.add(buttonPanel,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
ButtonInJTable mainFrame=new ButtonInJTable();
mainFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton source = (JButton)e.getSource();
if(e.getSource().equals(button)){
int row = table.getSelectedRow();
Object value = table.getValueAt(row, 0);
System.out.println("값 "+value);
}
if(e.getSource().equals(source) && e.getSource()!=button){
int pageNum=Integer.parseInt(source.getActionCommand());
pn.setPageNum(pageNum);
list = eDAO.getEmployeeTotal(pn);
int startPage = pn.getStartPage();
int endPage = pn.getEndPage();
int rowCount=list.size();
System.out.println("rowCount "+rowCount);
System.out.println("startPage "+startPage);
System.out.println("endPage "+endPage);
columnNames=new String[] {"No","이름" , "직급" , "부서","이메일", "지원여부"};
dataValues = new String[25][columnNames.length];
for(int index = 0; index<rowCount; index++){
emVo=list.get(index);
dataValues[index][0]=emVo.getNo()+"";
dataValues[index][1]=emVo.getName();
dataValues[index][2]=emVo.getJobGrade();
dataValues[index][3]=emVo.getDepartment()+"";
dataValues[index][4]=emVo.getEmail();
dataValues[index][5]="지원하기";
}
}
}
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ?"" : value.toString());
return this;
}
}
class ButtonEditor extends DefaultCellEditor {
private String label;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
label = (value == null) ? "" : value.toString();
button.setText(label);
return button;
}
public Object getCellEditorValue() {
return new String(label);
}
}
}
}
jframe을 상속 받고 jtable에 default Table Model을 집어 넣어서 테이블 셀 내에 버튼을 생성하는 형식입니다. 화면 아래에 버튼을 누르면 DAO 에서 List를 받아 와서 다시 뿌릴려고 하는데 리플레쉬가 제대로 안 먹히네요.
이거 해결 방법이 없을까요?