Java 在 JTable 的单元格内添加按钮和数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2069641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:14:01  来源:igfitidea点击:

Adding Buttons inside cell of JTable along with data?

javaswingjtablejbutton

提问by Todd

Is it possible to add buttons inside the JTable cell along with data? What I am trying to do is to create a table with columns which display data(number) from the database, and two buttons to increase/decrease the number inside the same cell.

是否可以在 JTable 单元格内添加按钮和数据?我想要做的是创建一个表格,其中包含显示数据库中数据(数字)的列,以及两个按钮来增加/减少同一单元格内的数字。

|ID | Quantity|
|06| 2 [+][-] |

|身 | 数量|
|06| 2 [+][-] |

it would be something like above with [+][-] being buttons. So when I press [+], the number will change to 3 and 1 if pressing [-].

就像上面的 [+][-] 是按钮一样。因此,当我按 [+] 时,如果按 [-],数字将变为 3 和 1。

采纳答案by OscarRyz

Yes, it is possible, although It won't be easy.

是的,这是可能的,虽然这并不容易。

You have to write your own custom cell rendererand your own cell editor.

您必须编写自己的自定义单元格渲染器和您自己的单元格编辑器

This is a sample I made in 5 minutes:

这是我在 5 分钟内制作的示例:

sample

样本

It is far from perfect, but shows the concept.

它远非完美,但显示了概念。

Here's the source code:

这是源代码:

import java.awt.Component;
import java.awt.Font;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.Dimension;

public class CustomCell {
    public static void main( String [] args ) { 
        Object [] columnNames = new Object[]{ "Id", "Quantity" };
        Object [][] data        = new Object[][]{ {"06", 1}, {"08", 2} };

        JTable table = new JTable( data, columnNames ) { 
            public TableCellRenderer getCellRenderer( int row, int column ) {
                return new PlusMinusCellRenderer();
            }
         };

        table.setRowHeight( 32 );
        showFrame( table );
    }

    private static void showFrame( JTable table ) {
        JFrame f = new JFrame("Custom Cell Renderer sample" );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.add( new JScrollPane( table ) );
        f.pack();
        f.setVisible( true );
    }
}

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
        public Component getTableCellRendererComponent(
                            final JTable table, Object value,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
                this.add( new JTextField( value.toString()  ) );
                this.add( new JButton("+"));
                this.add( new JButton("-"));
                return this;
        }
}

Here's a thread that may be interestingand here.

这里有可能是一个线程有趣这里

回答by Joonas Pulakka

I think you need to create a custom cell renderer if you want to show anything else than text (or numbers) in the cell. The cell renderer's job is to paint whatever you need to show in the cell.

如果您想在单元格中显示除文本(或数字)以外的任何内容,我认为您需要创建一个自定义单元格渲染器。单元格渲染器的工作是绘制您需要在单元格中显示的任何内容。

See Table Renderer documentation.

请参阅表渲染器文档

So in this case you could create a small JPane which contains the text field and the tiny + and - buttons - or a just a JSpinner component, if does what you need. A bit tricky, for sure, but should be possible.

因此,在这种情况下,您可以创建一个包含文本字段和微小的 + 和 - 按钮的小型 JPane - 或者只是一个 JSpinner 组件,如果您需要的话。当然有点棘手,但应该是可能的。

回答by trashgod

As discussed in the tutorialyou'll need both a renderer to display your value and an editor to detect events from the cell being edited. In this example, the Componentis a JCheckBox. Note that this requires a custom DataModelthat extends AbstractTableModelto supply the correct Classfor a given column. Joonas' suggestion to use JSpinneris a good one that works well.

正如教程中所讨论的,您需要一个渲染器来显示您的值,还需要一个编辑器来检测正在编辑的单元格中的事件。在这个例子中Component是一个JCheckBox. 请注意,这需要一个自定义DataModel扩展AbstractTableModel以提供Class给定列的正确值。Joonas 的使用建议JSpinner很好,效果很好。