一旦用户确定编辑表,我就会重新创建DS。
您始终可以创建一个自定义编辑器来显示一个弹出对话框,其中每个范围值都有两个单独的文本字段。然后,您可以将每个字段编辑为指定范围内的双精度值,并在将其保存到模型之前重新创建格式化的字符串。这是我为了让您起步而使用的一个旧示例:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;//public class TablePopupEditor extends AbstractCellEditorpublic class TablePopupEditor extends DefaultCellEditor implements TableCellEditor{ private PopupDialog popup; private String currentText = ""; private JButton editorComponent; public TablePopupEditor() { super(new JTextField()); setClickCountToStart(2); // Use a JButton as the editor component editorComponent = new JButton(); editorComponent.setBackground(Color.white); editorComponent.setBorderPainted(false); editorComponent.setContentAreaFilled( false ); // Set up the dialog where we do the actual editing popup = new PopupDialog(); } public Object getCellEditorValue() { return currentText; } public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { SwingUtilities.invokeLater(new Runnable() { public void run() { System.out.println("run"); popup.setText( currentText );// popup.setLocationRelativeTo( editorComponent ); Point p = editorComponent.getLocationOnScreen(); popup.setLocation(p.x, p.y + editorComponent.getSize().height); popup.show(); fireEditingStopped(); } }); currentText = value.toString(); editorComponent.setText( currentText ); return editorComponent; } class PopupDialog extends JDialog implements ActionListener { private Jtextarea textarea; public PopupDialog() { super((frame)null, "Change Description", true); textarea = new Jtextarea(5, 20); textarea.setLineWrap( true ); textarea.setWrapStyleWord( true ); KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER"); textarea.getInputMap().put(keyStroke, "none"); JScrollPane scrollPane = new JScrollPane( textarea ); getContentPane().add( scrollPane ); JButton cancel = new JButton("Cancel"); cancel.addActionListener( this ); JButton ok = new JButton("Ok"); ok.setPreferredSize( cancel.getPreferredSize() ); ok.addActionListener( this ); JPanel buttons = new JPanel(); buttons.add( ok ); buttons.add( cancel ); getContentPane().add(buttons, BorderLayout.SOUTH); pack(); getRootPane().setDefaultButton( ok ); } public void setText(String text) { textarea.setText( text ); } public void actionPerformed(ActionEvent e) { if ("Ok".equals( e.getActionCommand() ) ) { currentText = textarea.getText(); } textarea.requestFocusInWindow(); setVisible( false ); } } public static void main(String[] args) { String[] columnNames = {"Item", "Description"}; Object[][] data = { {"Item 1", "Description of Item 1"}, {"Item 2", "Description of Item 2"}, {"Item 3", "Description of Item 3"} }; JTable table = new JTable(data, columnNames); table.getColumnModel().getColumn(1).setPreferredWidth(300); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane(table); // Use the popup editor on the second column TablePopupEditor popupEditor = new TablePopupEditor(); table.getColumnModel().getColumn(1).setCellEditor( popupEditor ); Jframe frame = new Jframe("Popup Editor Test"); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.getContentPane().add( scrollPane ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); }}


