我有一个7列的JTable。我想在七一列添加带有Icon的JButton。
所以我有这个:
mappa= modelManager.getContoBancarioManager().getContiBancari(null,WFConst.CONTO_BANCARIO_PUBBLICO);
//fontTable = new Font("Century Gothic", Font.PLAIN, 15);
tableModelContiBancari = new MyTableModelContiBancari();
tableContiBancari= new JTable(tableModelContiBancari);
tableModelContiBancari.stampaTabella(mappa);
tableContiBancari.addMouseListener(new MyMouseAdapterTableConti());
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(tableContiBancari);
jScrollPane.setPreferredSize(dTabella);
Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
Double larghezza =screenSize.getWidth()*0.95;
// System.out.println(larghezza);
int lar = (int) (larghezza /90);
int lar2 = (int)(larghezza /5);
tableContiBancari.getColumnModel().getColumn(0).setPreferredWidth(10);
tableContiBancari.getColumnModel().getColumn(1).setPreferredWidth(lar2);
tableContiBancari.getColumnModel().getColumn(2).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(3).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(4).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(5).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(6).setPreferredWidth(lar);
Action delete = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
//to do
System.out.println("pp");
}
};
ButtonColumn buttonColumn = new ButtonColumn(tableContiBancari, delete, 7);
buttonColumn.setMnemonic(KeyEvent.VK_D);
这是ButtonColumn类:
public void daiProprietaJTableContiBancari(){
mappa= modelManager.getContoBancarioManager().getContiBancari(null,WFConst.CONTO_BANCARIO_PUBBLICO);
//fontTable = new Font("Century Gothic", Font.PLAIN, 15);
tableModelContiBancari = new MyTableModelContiBancari();
tableContiBancari= new JTable(tableModelContiBancari);
tableContiBancari.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F5)
cambiaTipologiaConti();
}
});
tableModelContiBancari.stampaTabella(mappa);
//tableContiBancari.setFont(fontTable);
tableContiBancari.addMouseListener(new MyMouseAdapterTableConti());
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(tableContiBancari);
jScrollPane.setPreferredSize(dTabella);
tableContiBancari.setRowHeight(25);
Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
Double larghezza =screenSize.getWidth()*0.95;
// System.out.println(larghezza);
int lar = (int) (larghezza /90);
int lar2 = (int)(larghezza /5);
tableContiBancari.getColumnModel().getColumn(0).setPreferredWidth(10);
tableContiBancari.getColumnModel().getColumn(1).setPreferredWidth(lar2);
tableContiBancari.getColumnModel().getColumn(2).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(3).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(4).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(5).setPreferredWidth(lar);
tableContiBancari.getColumnModel().getColumn(6).setPreferredWidth(lar);
DefaultTableCellRenderer renderer_archivi = new DefaultTableCellRenderer();
renderer_archivi.setHorizontalAlignment(SwingConstants.RIGHT);
tableContiBancari.getColumnModel().getColumn(4).setCellRenderer(renderer_archivi);
tableContiBancari.getColumnModel().getColumn(5).setCellRenderer(renderer_archivi);
tableContiBancari.getColumnModel().getColumn(6).setCellRenderer(renderer_archivi);
Action delete = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
//to do
System.out.println("pp");
}
};
ButtonColumn buttonColumn = new ButtonColumn(tableContiBancari, delete, 7);
buttonColumn.setMnemonic(KeyEvent.VK_D);
//tableContiBancari.getColumnModel().getColumn(7).setCellRenderer(new ButtonRenderer());
//tableContiBancari.getColumnModel().getColumn(7).setCellEditor(new ButtonEditor(new JCheckBox()));
//setUpColumnButton(tableContiBancari, tableContiBancari.getColumnModel().getColumn(7));
}
如果我尝试运行代码,我在最后一列有一个带JButton的JTable,但是如果我尝试点击一个JButton,那么该动作就不会执行。
答案 0 :(得分:0)
您需要实现自己的TableCellRenderer
并使其方法getTableCellRendererComponent
为您的列返回JButton
。请参阅Tutorial。
答案 1 :(得分:0)
查看http://jsfiddle.net/zs860fLx/2/一种方法。
您可以像对任何其他列一样向列中添加文本,然后将ButtonColumn
类用作:
renderer
- 以便文本显示在按钮上editor
- 所以你可以点击按钮来调用一个动作。您还必须向Action
课程提供ButtonColumn
。 Action
可以访问所点击按钮的行。例如,您可以轻松使用行号删除行,或使用该行从表中获取数据并执行其他处理。