如何在Jtable单元格上添加Jbutton?

时间:2014-03-08 06:41:14

标签: java swing jtable jbutton

我有一个jtable,我可以放一些记录,但现在我想插入jbutton代替那些记录

我的代码在下面给出

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class ScrollableJTable {
    public static void main(String[] args) {
        new ScrollableJTable();
    }
    public ScrollableJTable() {
        JFrame frame = new JFrame("Creating a Scrollable JTable!");
        JPanel panel = new JPanel();
        String data[][] = {
            {
                "001", "vinod", "Bihar", "India", "Biology", "65", "First"
            },
        };
        panel.setLayout(new BorderLayout());
        String col[] = {
            "Roll", "Name", "State", "country", "Math", "Marks", "Grade"
        };
        JTable table = new JTable(data, col);
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.yellow);
        Toolkit tk = Toolkit.getDefaultToolkit();
        int xSize = ((int) tk.getScreenSize().getWidth());
        int ySize = ((int) tk.getScreenSize().getHeight());
        JScrollPane pane = new JScrollPane(table);
        panel.add(pane);
        frame.add(pane);
        frame.setSize(xSize, ySize);
        table.setSize(xSize, ySize);
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

如果要显示按钮是一列,则需要为该列创建自定义渲染器和编辑器。阅读Swing教程中的部分 - Concepts: Editors and Renderers以获取一些基本信息。

然后查看Table Button Column一种方法。