Java jtable单元格内的滚动窗格不工作

Java jtable单元格内的滚动窗格不工作,java,swing,Java,Swing,单元格滚动窗格不工作: 你好, 我的问题是,我在单元格中使用了滚动窗格来显示单元格中的所有数据,但滚动窗格不工作意味着它不能滚动。请帮帮我。多谢各位 渲染器不是真正的组件,因此它不会对事件做出反应 因此,您需要自定义渲染器以显示可以接收事件的真实文本区域 以下是一个让您开始学习的基本示例: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import jav

单元格滚动窗格不工作:

你好,
我的问题是,我在单元格中使用了滚动窗格来显示单元格中的所有数据,但滚动窗格不工作意味着它不能滚动。请帮帮我。多谢各位

渲染器不是真正的组件,因此它不会对事件做出反应

因此,您需要自定义渲染器以显示可以接收事件的真实文本区域

以下是一个让您开始学习的基本示例:

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

public class TableTextArea extends JFrame
{
    public TableTextArea()
    {
        JTable table = new JTable(40, 5);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setRowHeight(40);
        table.setValueAt("one two three four five six seven eight nine ten", 0, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 1, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 0, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 2, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 3, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 4, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 5, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 6, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 7, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 8, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 9, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 10, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 11, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 12, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 13, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 14, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 15, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 16, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 17, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 18, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 19, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 20, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 21, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 22, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 23, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 24, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 25, 4);
        table.setValueAt("one two three four five six seven eight nine ten", 26, 2);
        table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 27, 2);
        table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 28, 4);
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );

        //  Override default renderer for a specific column

        TableCellRenderer renderer = new TextAreaRenderer();
        table.getColumnModel().getColumn(2).setCellRenderer( renderer );
        table.getColumnModel().getColumn(4).setCellRenderer( renderer );
        table.changeSelection(0, 0, false, false);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TableTextArea frame = new TableTextArea();
                frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
                frame.pack();
                frame.setLocationRelativeTo( null );
                frame.setVisible(true);
            }
        });
    }

    /*
    **
    */
    class TextAreaRenderer implements TableCellRenderer
    {
        private JTextArea renderTextArea;
        private JScrollPane renderScrollPane;
        private JTextArea focusTextArea;
        private JScrollPane focusScrollPane;
        private boolean firstTime = true;

        public TextAreaRenderer()
        {
            renderTextArea = new JTextArea();
            renderTextArea.setEditable( false );
            renderTextArea.setLineWrap( true );
            renderTextArea.setWrapStyleWord( true );
            renderScrollPane = new JScrollPane( renderTextArea );
            renderScrollPane.setBorder(null);
            renderScrollPane.revalidate();

            focusTextArea = new JTextArea();
            focusTextArea.setEditable( false );
            focusTextArea.setLineWrap( true );
            focusTextArea.setWrapStyleWord( true );
            focusScrollPane = new JScrollPane( focusTextArea );
            focusScrollPane.setBorder(null);
        }

        public Component getTableCellRendererComponent(
            final JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, final int column)
        {
            //  For some reason the scrollbars don't appear on the first cell renderered.
            //  Forcing a repaint of the cell seems to fix the problem.

            if (firstTime)
            {
                firstTime = false;
                Rectangle cellRectangle = table.getCellRect(row, column, false);
                table.repaint(cellRectangle);
            }

            table.remove(focusScrollPane);

            renderTextArea.setText( value != null ? value.toString() : "" );
            renderTextArea.setCaretPosition(0);

            if (hasFocus)
            {
                renderTextArea.setBackground( table.getSelectionBackground() );

                SwingUtilities.invokeLater( new Runnable()
                {
                    public void run()
                    {
                        addRealTextAreaToTable(table, row, column);
                    }
                });
            }
            else if (isSelected)
                renderTextArea.setBackground( table.getSelectionBackground() );
            else
                renderTextArea.setBackground( table.getBackground() );

            return renderScrollPane;
        }

        private void addRealTextAreaToTable(JTable table, int row, int column)
        {
            Object value = table.getValueAt(row, column);
            focusTextArea.setText( value != null ? value.toString() : "" );
            focusTextArea.setCaretPosition(0);
//          focusTextArea.setBackground( table.getBackground() );
            focusTextArea.setBackground( table.getSelectionBackground() );
            Rectangle cellRectangle = table.getCellRect(row, column, false);
            focusScrollPane.setBounds(cellRectangle);
            table.add(focusScrollPane);
            focusScrollPane.revalidate();
            focusTextArea.requestFocusInWindow();
        }
    }
}