当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

在JTable中放置JTextarea

    今天需要做一个界面,JTable的其中最后一列的显示的文字比较多,而且需要分行显示。JTable默认所存放的CellRenderer是JTextField,无法满足要求,经过google在网上查找相关资料,大多数实现并没有实现根据textarear里面文字信息的多少决定textarea的高度。写了一个实现,请大家参考一下。


package com.ideal.ieai.adaptors.taskadaptor.usertask.view.timeout;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class TimeoutPanel extends JPanel
{

    private JScrollPane       _scrlRules     = null;
    private JTable            _tblRules      = null;
    private JPanel            _panelCtrl     = null;
    private JButton           _btnAdd        = null;
    private JButton           _btnEdit       = null;
    private JButton           _btnDel        = null;
    private DefaultTableModel _tblModelRules = null;
    private JTextPane         _sumaryTxtPane = null;

    /**
     * This is the default constructor
     */
    public TimeoutPanel()
    {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize ()
    {
        setLayout(new BorderLayout());
        add(getScrlRules(), BorderLayout.CENTER);
        add(getPanelCtrl(), BorderLayout.SOUTH);
        this.setSize(500, 400);
    }

    private Component getPanelCtrl ()
    {
        if (null == _panelCtrl)
        {
            _panelCtrl = new JPanel();
            _panelCtrl.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4));
            _panelCtrl.add(getBtnAdd());
            _panelCtrl.add(getBtnEdit());
            _panelCtrl.add(getBtnDel());
        }
        return _panelCtrl;
    }

    private Component getBtnDel ()
    {
        if (null == _btnDel)
        {
            _btnDel = new JButton("delete");
        }
        return _btnDel;
    }

    private Component getBtnEdit ()
    {
        if (null == _btnEdit)
        {
            _btnEdit = new JButton("edit");
        }
        return _btnEdit;
    }

    private Component getBtnAdd ()
    {
        if (null == _btnAdd)
        {
            _btnAdd = new JButton("add");
        }
        return _btnAdd;
    }

    private Component getScrlRules ()
    {
        if (null == _scrlRules)
        {
            _scrlRules = new JScrollPane();
            _scrlRules.getViewport().add(getTblRules());
        }
        return _scrlRules;
    }

    private Component getTblRules ()
    {
        if (null == _tblRules)
        {
            _tblRules = new JTable();
            _tblRules.setModel(getTblModelRules());

            _tblRules.getColumnModel().getColumn(5).setCellRenderer(new DefaultTableCellRenderer()
            {
                public Component getTableCellRendererComponent ( JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column )
                {
                    getSumaryTxtPane().setText((String) value);
                    TableColumnModel columnModel = table.getColumnModel();
                    getSumaryTxtPane().setSize(columnModel.getColumn(column).getWidth(), 100000);
                    int heightWanted = (int) getSumaryTxtPane().getPreferredSize().getHeight();
                    if (heightWanted != table.getRowHeight(row))
                    {
                        table.setRowHeight(row, heightWanted);
                    }
                    return getSumaryTxtPane();
                }
            });
        }
        return _tblRules;
    }

    private JTextPane getSumaryTxtPane ()
    {
        if (null == _sumaryTxtPane)
        {
            _sumaryTxtPane = new JTextPane();
        }
        return _sumaryTxtPane;
    }

    private TableModel getTblModelRules ()
    {
        if (null == _tblModelRules)
        {
            _tblModelRules = new DefaultTableModel()
            {
                public boolean isCellEditable ( int row, int colum )
                {
                    return false;
                }
            };
            _tblModelRules.addColumn("Rule Name");
            _tblModelRules.addColumn("Type");
            _tblModelRules.addColumn("Timeout");
            _tblModelRules.addColumn("Task Status");
            _tblModelRules.addColumn("Alert Type");
            _tblModelRules.addColumn("Sumary");
            _tblModelRules
                    .addRow(new Object[] { "rule1", "type1", "timeout1", "task status",
                            "alert type1",
                            "a\na\na\na" });
            _tblModelRules
                    .addRow(new Object[] { "rule1", "type1", "timeout1", "task status",
                            "alert type1",
                            "a\na\na\na" });
            _tblModelRules
            .addRow(new Object[] { "rule1", "type1", "timeout1", "task status",
                    "alert type1",
                    "a\na\na\na" });
            _tblModelRules
            .addRow(new Object[] { "rule1", "type1", "timeout1", "task status",
                    "alert type1",
                    "a\na\na\na" });
        }
        return _tblModelRules;
    }

}


控制的关键在这一段代码:
getSumaryTxtPane().setSize(columnModel.getColumn(column).getWidth(), 100000);
其中100000可以变成>=7的数字都是可以的

相关内容
赞助商链接