栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JTable设计与后端数据结构同步

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JTable设计与后端数据结构同步

一旦用户确定编辑表,我就会重新创建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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470459.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号