3

我这里有一个非常简单的演示程序,很抱歉代码的版本不是那么短,但它是我能做的最短的。无论如何,我在这里有一个jtable用于第 2 列的可编辑单元格。我希望右侧的小键盘append到 的selected 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();         
            }
        });
    }
}

编辑

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

4

2 回答 2

6

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

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

例如,“7”按钮的代码可能类似于:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
String value = table.getValueAt(row, column) + "7";
table.setValueAt(value, row, column);

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

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
String value = table.getValueAt(row, column) + e.getActionCommand();
table.setValueAt(value, row, column);

动作命令只是默认为按钮的字符串。

于 2014-07-23T16:39:56.433 回答
3

不要忘记检查是否选择了列和行(选择的行和列索引不是-1):

public void actionPerformed(ActionEvent e) {
   int selectedRow = table.getSelectedRow();
   int selectedColumn = table.getSelectedColumn();

   // **** better first check that user has selected something
   if (selectedRow < 0 || selectedColumn < 0) {
      return;
   }

   // **** next make sure cell is editable
   if (table.isCellEditable(selectedRow, selectedColumn)) {

      // get current value
      String value = table.getValueAt(selectedRow, selectedColumn).toString();

      // append new value
      value += "7";  // or actionCommand as per camickr -- 1+ to his answer.
      table.setValueAt(value, selectedRow, selectedColumn);
   }
}
于 2014-07-23T16:43:13.923 回答