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