繁体   English   中英

Java Eclipse Jtable单元格/列

[英]Java Eclipse Jtable Cell / Column

对于Java Kepler Eclipse和Jtable,我试图做到这一点,以便在选择特定表单元格时,该单元格可以用作editorPane; 或让整个专栏都充当editorPane。 当我单击COMMENTS列上的单元格时,它会放大行,但无法使其用作editorPane。 我的项目实际上是非常不同的,但是我用表格编写了这个迷你项目,因此您可以复制,粘贴和运行它,以查看单击COMMENTS单元格时出了什么问题。

我试图使该列成为editorPane的开始,就像我使用checkBox使该列完成了一样,但它不起作用或我做错了。 我也尝试了cellRenderer,但是我也无法做到这一点。

整个列是用作editorPane还是仅用作选定单元格都无关紧要,只要它起作用就更容易

import javax.swing.*;
import javax.swing.table.*;    
import java.awt.*;
public class JavaTestOne {
    JFrame frmApp;
    private JTable table;
    private JCheckBox checkbox;
    DefaultTableModel model;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaTestOne window = new JavaTestOne();
                    window.frmApp.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });  
    }
    public JavaTestOne() {  
        initialize();
    }
    private void initialize() {
        frmApp = new JFrame();
        frmApp.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
        frmApp.setBounds(50, 10, 1050, 650);
        frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmApp.getContentPane().setLayout(new CardLayout(0, 0));
        frmApp.setTitle("App");
        {
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(0, 42, 984, 484);
            frmApp.add(scrollPane);
            {
                table = new JTable();
                table.setFillsViewportHeight(true);
                Object[][] data = {
                        {"I01", "Tom",new Boolean(false), ""},
                        {"I02", "Jerry",new Boolean(false), ""},
                        {"I03", "Ann",new Boolean(false), ""}};
                String[] cols = {"ID","NAME","DONE","COMMENTS"};
                model = new DefaultTableModel(data, cols) {
                    private static final long serialVersionUID = -7158928637468625935L;

                    public Class getColumnClass(int column) {
                        return getValueAt(0, column).getClass();
                    }
                };
                table.setModel(model);
                table.setRowHeight(20);

                table.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent evt) {
                        int row = table.rowAtPoint(evt.getPoint());
                        int col = table.columnAtPoint(evt.getPoint());
                        table.setRowHeight(20);
                        if(col==3){
                            table.setRowHeight(row, 100);
                            //this is where I need it to work as an editorPane if it is only for the selected cell
                        }
                    }
                });
                table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
                scrollPane.setViewportView(table);
                checkbox = new JCheckBox("OK");
                checkbox.setHorizontalAlignment(SwingConstants.CENTER);
                checkbox.setBounds(360, 63, 97, 23);
            }
        }
    }
}

似乎您需要实现自己的TableCellEditor ,请在教程中阅读更多内容。

例如这样的:

 private class CustomEditor extends AbstractCellEditor implements TableCellEditor{

    private JTextPane pane = new JTextPane();
    private JScrollPane scroll = new JScrollPane(pane);
    private int row = -1;

    @Override
    public Object getCellEditorValue() {
        return pane.getText();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
        if(this.row != -1)
            table.setRowHeight(this.row, 20);
        this.row = row;
        table.setRowHeight(row, 100);
        pane.setText(value == null ? "" : value.toString());
        return scroll;
    }

}

然后将其设置为列编辑器: table.getColumn("COMMENTS").setCellEditor(new CustomEditor());

在此处输入图片说明

另一种选择是显示一个弹出窗口来编辑单元格:

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();
            }
        });
    }

}

使用这种方法,您不会持续操纵行的大小。 您甚至可以自定义代码,以使对话框适合单元格的宽度并显示在单元格下方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM