简体   繁体   English

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

[英]How can i add Jbutton on Jtable cell?

i have a jtable where i can put some records but now i want to insert jbutton in place of that records 我有一个jtable,我可以在其中放置一些记录,但是现在我想在该记录的位置插入jbutton

my code is given below 我的代码如下

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);
    }
}

If you want to display a button is a column then you need to create a custom renderer and editor for the column. 如果要显示按钮是一列,则需要为该列创建自定义渲染器和编辑器。 Read the section from the Swing tutorial on - Concepts: Editors and Renderers for some basic information. 阅读Swing教程- 概念:编辑器和渲染器中的部分以获取一些基本信息。

Then check out Table Button Column for one approach. 然后查看“ 表按钮列”中的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM