分类:
2008-09-09 12:42:23
今天需要做一个界面,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;
}
[1]