如何在JTable单元格中附加文本

use*_*102 3 java swing jtable treetablecelleditor

我这里有一个非常简单的演示程序,对于不那么短的代码版本感到抱歉,但它是我能做的最短的.反正,我这里有一个jtable与列2.编辑单元我想在右侧数字键盘来appendselected celljtable,但.setValueAt()没有做到这一点.有没有办法将右侧的小键盘附加到选定的小区?或者我应该

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.table.*;

public  class Jtabledemo {

    private static class JTabledemo {

        JTable table;
        JFrame Card = new JFrame();
        JTabbedPane card_tab = new JTabbedPane(); // FOR TAB
        JPanel buttonpanel = new JPanel(); // FOR BUTTON BELOW
        FlowLayout flow = new FlowLayout(FlowLayout.LEFT);

        public JTabledemo() {      
            Card.setVisible(true);
            Card.setSize(821,340);
            Card.setTitle("");
            Card.setResizable(false);

            final Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();      
            int x=(int)((dimension.getWidth() - Card.getWidth())/2);
            int y=(int)((dimension.getHeight() - Card.getHeight())/2);

            Card.setLocation(x, y);
            Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            JPanel bodypanel = new JPanel();
            bodypanel.setLayout(new BorderLayout());

            JPanel container = new JPanel();
            container.setLayout(new BorderLayout());

            JPanel jp1 = new JPanel();
            jp1.setBackground(Color.lightGray);
            jp1.setPreferredSize(new Dimension(510,260));
            jp1.setLayout(new BorderLayout());

            table = new JTable(new ExampleTableModel());
            JScrollPane scrollPane = new JScrollPane(table);
            table.setFillsViewportHeight(true);

            JTableHeader header = table.getTableHeader();
            table.setRowHeight(20);
            jp1.add(scrollPane);

            JPanel buttonpanel = new JPanel();
            buttonpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
            buttonpanel.setBorder(new EmptyBorder(0,0,0,0));

            JButton btnOk = new JButton("Ok");
            btnOk.setPreferredSize(new Dimension(90, 40));
            buttonpanel.add(btnOk);

            final JButton btnCancel = new JButton("Cancel");
            btnCancel.setPreferredSize(new Dimension(90, 40));
            buttonpanel.add(btnCancel);

            JPanel container2 = new JPanel();
            container2.setLayout(new BorderLayout());
            container2.setPreferredSize(new Dimension(344,0));

            JPanel numberpanel = new JPanel();
            numberpanel.setPreferredSize(new Dimension(235,0));
            numberpanel.setBorder(new EmptyBorder(25,0,0,0));
            numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
            numberpanel.setLayout(flow);

            JButton button_7 = new JButton("7");
            button_7.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    table.setValueAt("7", table.getSelectedRow(), table.getSelectedColumn());
                }
            });
            button_7.setPreferredSize(new Dimension(70, 70));
            numberpanel.add(button_7);

            JButton button_8 = new JButton("8");
            button_8.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    table.setValueAt("8", table.getSelectedRow(), table.getSelectedColumn());
                }   
            });
            button_8.setPreferredSize(new Dimension(70, 70));
            numberpanel.add(button_8);

            JButton button_9 = new JButton("9");
            button_9.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    table.setValueAt("9", table.getSelectedRow(), table.getSelectedColumn());
                }   
            });
            button_9.setPreferredSize(new Dimension(70, 70));
            numberpanel.add(button_9);

            Card.add(bodypanel);
            bodypanel.add(container, BorderLayout.WEST);
            container.add(jp1,BorderLayout.NORTH);
            container.add(buttonpanel,BorderLayout.SOUTH);
            bodypanel.add(container2, BorderLayout.EAST);
            container2.add(numberpanel, BorderLayout.EAST);
        }

        public static class ExampleTableModel extends DefaultTableModel {
            public ExampleTableModel() {
                super(new Object[]{"Tank", "Current", "Dip"}, 0);
                for (int index = 0; index < 4; index++) {
                    addRow(new Object[]{index, "Text for " + index, "Na na", index});
                }
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return columnIndex == 2;
            }
        }
    }

    public static void main(String[] args){
    //Use the event dispatch thread for Swing components
        EventQueue.invokeLater(new Runnable() {
            @Override
            @SuppressWarnings("ResultOfObjectAllocationIgnored")
            public void run() {
                new JTabledemo();         
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

当我单击小键盘时,它应该附加到选定的单元格,就像您单击小键盘时只插入一个数字.我希望这些数字附加在选定的单元格中.

cam*_*ckr 6

我希望这些数字附加在选定的单元格中.

然后将数字附加到现有值.

例如,"7"按钮的代码可能是这样的:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
String value = table.getValueAt(row, column) + "7";
table.setValueAt(value, row, column);
Run Code Online (Sandbox Code Playgroud)

当然,更好的解决方案是使用通用的ActionListener,然后您可以使代码更通用:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
String value = table.getValueAt(row, column) + e.getActionCommand();
table.setValueAt(value, row, column);
Run Code Online (Sandbox Code Playgroud)

action命令只是默认为按钮的字符串.