java – 在JTable单元格下显示弹出JFrame?

在过去的几个月里,我已经获得了很多JTables的经验,而且我已经掌握了ActionListeners和创意实现.但是,有一点我无法弄清楚.

当单击一个单元格在某个列上进行编辑时,我想在其下面弹出一个操作单元格值的小JFrame,就像JComboBox一样.我把一切都搞定了,但是,我无法将表格完全定位在JTable单元格下方(技术上,我希望单元格的左下角是弹出窗口的左上角.我尝试使用“getCellRect(.. .)“但它并没有让我得到结果或角落的正确坐标.

所以现在我正在使用MousePointer.getMousePointerInfo()来显示在该单元格中单击鼠标的弹出窗口.但它并不理想,因为我希望它固定在单元格下而不是跟随光标.

有关如何做到这一点的任何建议?

解决方法

I want to popup a tiny JFrame

不要使用JFrame.子窗口应该是jdialog.

创建自定义编辑器.例如:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

/*
 * The editor button that brings up the dialog.
 */
//public class TablePopupEditor extends AbstractCellEditor
public class TablePopupEditor extends DefaultCellEditor
    implements TableCellEditor
{
    private PopupDialog popup;
    private String currentText = "";
    private JButton editorComponent;

    public TablePopupEditor()
    {
        super(new JTextField());

        setClickCountToStart(1);

        //  Use a JButton as the editor component

        editorComponent = new JButton();
        editorComponent.setBackground(Color.white);
        editorComponent.setBorderPainted(false);
        editorComponent.setContentAreaFilled( false );

        // Make sure focus goes back to the table when the dialog is closed
        editorComponent.setFocusable( 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()
            {
                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;
    }

    /*
    *   Simple dialog containing the actual editing component
    */
    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 );
        }

        /*
        *   Save the changed text before hiding the popup
        */
        public void actionPerformed(ActionEvent e)
        {
            if ("Ok".equals( e.getActionCommand() ) )
            {
                currentText = textArea.getText();
            }

            textArea.requestFocusInWindow();
            setVisible( false );
        }
    }

    private static void createAndShowUI()
    {
        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.add(new JTextField(),BorderLayout.norTH);
        frame.add( scrollPane );
        frame.pack();
        frame.setLocationRelativeto( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokelater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }

}

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...