下面列出了javax.swing.table.TableColumn#setMaxWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private JScrollPane tableScrollPane(){
table_model = new CharSetsTableModel(getTableDataWithAvailableCharsets(), columns);
JTable table = new JTable(table_model);
TableColumn col = table.getColumnModel().getColumn(0);
col.setMinWidth(50);
col.setMaxWidth(50);
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if(0==table.getSelectedColumn()){
return;
}
table.setValueAt(!(Boolean)table.getValueAt(table.getSelectedRow(), 0), table.getSelectedRow(),0);
}
});
sorter = new TableRowSorter<CharSetsTableModel>(table_model);
table.setRowSorter(sorter);
JScrollPane jscrollPane = new JScrollPane(table);
return jscrollPane;
}
public void createAttributesTable(DataObjectInfo doi) {
table = new GenericTable(new AttributeTableModel(doi));
table.setColumnSelectorAvailable(false);
table.getSelectionModel().addListSelectionListener(this);
//To stop cell editing when user switches to another component without using tab/enter keys
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
// TableCellRenderer checkBoxRenderer = new TableCellCheckBoxRenderer();
// TableCellCheckBoxEditor checkBoxEditor = new TableCellCheckBoxEditor();
TableColumn tc0 = table.getColumnModel().getColumn(0);
tc0.setMinWidth(55);
tc0.setMaxWidth(55);
TableColumn tc1 = table.getColumnModel().getColumn(1);
tc1.setMinWidth(40);
tc1.setMaxWidth(40);
TableColumn tc2 = table.getColumnModel().getColumn(2);
tc2.setMinWidth(70);
tc2.setMaxWidth(70);
scrollPane.getViewport().setView(table);
TableColumn tc5 = table.getColumnModel().getColumn(5);
ClassPickerTextButtonCellEditor editor = new ClassPickerTextButtonCellEditor(new Context(null, ProjectUtils.getViewControllerProject()),this );
tc5.setCellEditor(editor);
}
void hideColumn(TableColumn column, ProfilerTable table) {
hiddenColumnWidths.put(column.getModelIndex(), column.getWidth());
column.setMinWidth(0);
column.setMaxWidth(0);
int selected = table.getSelectedColumn();
if (selected != -1 && getColumn(selected).equals(column)) {
int newSelected = getPreviousVisibleColumn(selected);
getSelectionModel().setSelectionInterval(newSelected, newSelected);
}
if (table.isSortable()) {
ProfilerRowSorter sorter = table._getRowSorter();
int sortColumn = sorter.getSortColumn();
if (sortColumn == column.getModelIndex()) {
int newSortColumn = table.convertColumnIndexToView(sortColumn);
newSortColumn = getPreviousVisibleColumn(newSortColumn);
sorter.setSortColumn(getColumn(newSortColumn).getModelIndex());
}
}
}
public static TableColumn createTableColumn(int index, String headerValue, TableCellRenderer headerRenderer,
boolean resizable)
{
TableColumn column = new TableColumn(index);
if (headerValue.startsWith("in_"))
{
column.setHeaderValue(LanguageBundle.getString(headerValue));
}
else
{
column.setHeaderValue(headerValue);
}
column.setHeaderRenderer(headerRenderer);
if (!resizable)
{
column.sizeWidthToFit();
column.setMaxWidth(column.getMaxWidth() + 10);
column.setPreferredWidth(column.getPreferredWidth() + 10);
}
column.setResizable(resizable);
return column;
}
private void refreshUI () {
lToImport.setEnabled (toImport.size () > 0);
tToImport.setEnabled (toImport.size () > 0);
lToInstall.setEnabled (toInstall.size () > 0);
tToInstall.setEnabled (toInstall.size () > 0);
TableColumn activeColumn = tToImport.getColumnModel ().getColumn (0);
activeColumn.setMaxWidth (tToImport.getTableHeader ().getHeaderRect (0).width);
activeColumn = tToInstall.getColumnModel ().getColumn (0);
activeColumn.setMaxWidth (tToInstall.getTableHeader ().getHeaderRect (0).width);
if (bImport != null) {
bImport.setEnabled (checkedToInstall.indexOf (Boolean.TRUE) != -1 || checkedToImport.indexOf (Boolean.TRUE) != -1);
}
}
private void tableFixedColumnWidth(JTable table, int[] menu_width, boolean[] fixed_map) {
for (int index = 0; index < fixed_map.length; index++) {
if (fixed_map[index]) {
TableColumn col = table.getColumnModel().getColumn(index);
col.setMinWidth(menu_width[index]);
col.setMaxWidth(menu_width[index]);
}
}
}
/**
* Packs some columns of a table.<br>
* Resizes the specified columns to exactly the maximum width of the values in each column.
* @param table table to be packed
* @param columns columns to be resized
*/
public static void packTable( final JTable table, final int[] columns ) {
for ( final int column : columns ) {
int maxWidth = 10; // A minimum width for the columns
for ( int row = table.getRowCount() - 1; row >= 0 ; row-- )
maxWidth = Math.max( maxWidth, table.getRowMargin() + table.getCellRenderer( row, column ).getTableCellRendererComponent( table, table.getValueAt( row, column ), false, false, row, column ).getPreferredSize().width );
final TableColumn tableColumn = table.getColumnModel().getColumn( column );
tableColumn.setMaxWidth( maxWidth );
tableColumn.setMinWidth( maxWidth );
}
}
private void initTable() {
TableColumn tagColumn = columnModel.getColumn(LogTableModel.COL_TAG);
TableColumn pidColumn = columnModel.getColumn(LogTableModel.COL_PID);
TableColumn pNameColumn = columnModel.getColumn(LogTableModel.COL_PROCESS);
TableColumn timeColumn = columnModel.getColumn(LogTableModel.COL_TIME);
TableColumn levelColumn = columnModel.getColumn(LogTableModel.COL_LEVEL);
tagColumn.setMinWidth(50);
tagColumn.setPreferredWidth(150);
tagColumn.setMaxWidth(300);
pidColumn.setMinWidth(35);
pidColumn.setPreferredWidth(50);
pidColumn.setMaxWidth(70);
pNameColumn.setMinWidth(60);
pNameColumn.setPreferredWidth(80);
pNameColumn.setMaxWidth(300);
timeColumn.setMinWidth(60);
timeColumn.setPreferredWidth(80);
timeColumn.setMaxWidth(120);
levelColumn.setMinWidth(20);
levelColumn.setPreferredWidth(80);
levelColumn.setMaxWidth(80);
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
ElistTablePanel tmp = left;
left = top;
top = tmp;
model.close();
model = createModel();
if ((Boolean) getValue(SELECTED_KEY)) {
model.setRevert(true);
} else {
model.setRevert(false);
}
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableHeader = new TopTablePanel(top, table);
table.setTableHeader(tableHeader);
table.setRowHeight(CELL_BORDER);
tableHeader.revalidate();
tableHeader.repaint();
TableColumnModel columnModel = table.getColumnModel();
for (int i = 0; i < columnModel.getColumnCount(); i++) {
TableColumn column = columnModel.getColumn(i);
column.setMaxWidth(CELL_BORDER);
column.setMinWidth(CELL_BORDER);
}
pane.setRowHeaderView(left);
updateHeadders();
pane.revalidate();
pane.repaint();
pane.setViewportView(table);
}
/**
* Set up the table so it looks well arranged.
*/
private void arrangeTable() {
// memTable.setRowHeight(20);
TableColumn column;
column = memTable.getColumn(MemoryMapModel.READ_COL);
column.setMaxWidth(25);
column.setMinWidth(25);
column.setResizable(false);
column = memTable.getColumn(MemoryMapModel.WRITE_COL);
column.setMaxWidth(25);
column.setMinWidth(25);
column.setResizable(false);
column = memTable.getColumn(MemoryMapModel.EXECUTE_COL);
column.setMaxWidth(25);
column.setMinWidth(25);
column.setResizable(false);
column = memTable.getColumn(MemoryMapModel.VOLATILE_COL);
column.setMaxWidth(57);
column.setMinWidth(57);
column.setResizable(false);
column = memTable.getColumn(MemoryMapModel.OVERLAY_COL);
column.setMaxWidth(55);
column.setMinWidth(55);
column.setResizable(false);
column = memTable.getColumn(MemoryMapModel.BLOCK_TYPE_COL);
column.setMinWidth(60);
// column.setResizable(true);
column = memTable.getColumn(MemoryMapModel.INIT_COL);
column.setMaxWidth(68);
column.setMinWidth(68);
column.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
if (myColumns.isEmpty()) {
stop();
if (myDone != null) {
SwingUtilities.invokeLater(myDone);
}
return;
}
final TableColumn c = myColumns.get(0).first;
if (!added) {
myTable.addColumn(c);
c.setMaxWidth(0);
c.setPreferredWidth(0);
c.setWidth(0);
added = true;
}
final int prefWidth = myColumns.get(0).second.intValue();
int width = c.getWidth();
width = Math.min(width + myStep, prefWidth);
c.setMaxWidth(width);
c.setPreferredWidth(width);
c.setWidth(width);
if (width == prefWidth) {
added = false;
myColumns.remove(0);
//c.setMaxWidth(oldMaxWidth);
}
}
/**
*
*/
private void setColumnWidth(int column, int minWidth, int maxWidth) {
TableColumn tableColumn = jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(column));
if (minWidth >= 0 && maxWidth >= 0) {
tableColumn.setPreferredWidth((minWidth + maxWidth) / 2);
}
if (minWidth >= 0) {
tableColumn.setMinWidth(minWidth);
}
if (maxWidth >= 0) {
tableColumn.setMaxWidth(maxWidth);
}
}
/**
*
*/
private void setColumnWidth(int column, int minWidth, int maxWidth) {
TableColumn tableColumn = jTable_Tasks.getColumnModel().getColumn(column);
if (minWidth >= 0 && maxWidth >= 0) {
tableColumn.setPreferredWidth((minWidth + maxWidth) / 2);
}
if (minWidth >= 0) {
tableColumn.setMinWidth(minWidth);
}
if (maxWidth >= 0) {
tableColumn.setMaxWidth(maxWidth);
}
}
private void setPrefferedColumnsSize() {
TableColumn col = table.getColumnModel().getColumn(0);
int width = 20;
col.setMinWidth(width);
col.setMaxWidth(width);
col.setPreferredWidth(width);
col = table.getColumnModel().getColumn(1);
width = 160;
col.setPreferredWidth(width);
col = table.getColumnModel().getColumn(2);
width = 120;
col.setPreferredWidth(width);
col = table.getColumnModel().getColumn(3);
width = 80;
col.setPreferredWidth(width);
col = table.getColumnModel().getColumn(4);
width = 80;
col.setPreferredWidth(width);
col = table.getColumnModel().getColumn(5);
width = 80;
col.setPreferredWidth(width);
}
private void adjustColumnWidths( int topColumn ) {
TableColumnModel colModel = getColumnModel();
int colWidth = 0;
int subColWidth = -1;
for( int row=0; row<getRowCount(); row++ ) {
Item item = ( Item ) getValueAt( row, topColumn );
Component ren = prepareRenderer( this.getCellRenderer( row, topColumn ), row, topColumn, item, true );
int prefWidth = ren.getPreferredSize().width;
colWidth = Math.max( colWidth, prefWidth );
if( null != item && item.hasSubItems() && topColumn+1 < getColumnCount()
&& !getSwitcherModel().isTopItemColumn( topColumn+1 ) ) {
Item[] subItems = item.getActivatableSubItems();
for( int i=0; i<subItems.length; i++ ) {
ren = prepareRenderer( this.getCellRenderer( 0, topColumn+1 ), 0, topColumn+1, subItems[i], true );
prefWidth = ren.getPreferredSize().width;
subColWidth = Math.max( subColWidth, prefWidth );
}
}
}
colWidth = Math.min( colWidth, MAX_TOP_COLUMN_WIDTH );
TableColumn tc = colModel.getColumn( topColumn );
tc.setPreferredWidth( colWidth );
tc.setWidth( colWidth );
tc.setMaxWidth( colWidth );
if( subColWidth > 0 ) {
subColWidth = Math.min( subColWidth, MAX_SUB_COLUMN_WIDTH );
tc = colModel.getColumn( topColumn+1 );
tc.setPreferredWidth( subColWidth );
tc.setWidth( subColWidth );
tc.setMaxWidth( subColWidth );
}
}
/**
* Configure a column model.
*
* @param table Table for which the column model should be configured.
*/
public void configureColumnModel(JTable table) {
if (table == null) {
return;
}
TableColumnModel model = table.getColumnModel();
TableColumn column = null;
column = model.getColumn(COLUMN_BOT);
column.setMinWidth(20);
column.setPreferredWidth(20);
column.setMaxWidth(20);
column.setCellRenderer(
new BooleanIconCellRenderer("commons-approve-icon.png", null));
column.setHeaderRenderer(
new IconCellRenderer("commons-nuvola-apps-kcmsystem.png"));
column = model.getColumn(COLUMN_DESCRIPTION);
column.setMinWidth(100);
column = model.getColumn(COLUMN_FIX);
column.setMinWidth(20);
column.setPreferredWidth(20);
column.setMaxWidth(20);
column.setHeaderRenderer(
new IconCellRenderer("commons-nuvola-apps-kcmsystem.png"));
column = model.getColumn(COLUMN_LIST);
column.setMinWidth(20);
column.setPreferredWidth(20);
column.setMaxWidth(20);
column.setHeaderRenderer(
new IconCellRenderer("gnome-logviewer.png"));
column = model.getColumn(COLUMN_NUMBER);
column.setMinWidth(40);
column.setPreferredWidth(40);
column.setMaxWidth(40);
table.addMouseListener(EventHandler.create(
MouseListener.class, this, "mouseClicked", "", "mouseClicked"));
}
/**
* Initializes the table.
*/
protected void init() {
refreshTimer.setRepeats(false);
refreshTimer.setCoalesce(true);
setModel(rowModel);
setColumnSelectionAllowed(true);
setGridColor(Color.blue);
setSelectionBackground(LIGHT_BLUE);
setSelectionForeground(Color.red); // foreground color for selected cells
setColumnModel(new DataTableColumnModel());
setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
setColumnSelectionAllowed(true);
rowModel.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
// forward the table model event to property change listeners
DataRowTable.this.firePropertyChange("cell", null, e); //$NON-NLS-1$
}
});
setDefaultRenderer(Object.class, cellRenderer);
getTableHeader().setForeground(Color.blue); // set text color
getTableHeader().setReorderingAllowed(true);
getTableHeader().setDefaultRenderer(new HeaderRenderer());
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
int width = 24;
String name;
TableColumn column;
if(getColumnCount()>0) {
// set width of column 0 (row index)
name = getColumnName(0);
column = getColumn(name);
column.setMinWidth(width);
column.setMaxWidth(2*width);
column.setWidth(width);
}
// set width of other columns
width = 60;
for(int i = 1, n = getColumnCount(); i<n; i++) {
name = getColumnName(i);
column = getColumn(name);
column.setMinWidth(width);
column.setMaxWidth(3*width);
column.setWidth(width);
}
}
private MainPanel() {
super(new BorderLayout());
RowDataModel model = new RowDataModel();
model.addRowData(new RowData("Name 1", "comment..."));
model.addRowData(new RowData("Name 2", "Test"));
model.addRowData(new RowData("Name d", "ee"));
model.addRowData(new RowData("Name c", "Test cc"));
model.addRowData(new RowData("Name b", "Test bb"));
model.addRowData(new RowData("Name a", "ff"));
model.addRowData(new RowData("Name 0", "Test aa"));
JTable table = new JTable(model);
// // TEST:
// JTable table = new JTable(model) {
// protected final Color evenColor = new Color(0xF0_F0_FF);
// @Override public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
// Component c = super.prepareRenderer(tcr, row, column);
// if (isRowSelected(row)) {
// c.setForeground(getSelectionForeground());
// c.setBackground(getSelectionBackground());
// } else {
// c.setForeground(getForeground());
// c.setBackground(row % 2 == 0 ? evenColor : getBackground());
// }
// return c;
// }
// };
StripeTableRenderer renderer = new StripeTableRenderer();
table.setDefaultRenderer(Object.class, renderer);
table.setDefaultRenderer(Integer.class, renderer);
table.setShowGrid(false);
// table.setShowHorizontalLines(false);
// table.setShowVerticalLines(false);
TableColumn col = table.getColumnModel().getColumn(0);
col.setMinWidth(60);
col.setMaxWidth(60);
col.setResizable(false);
table.setAutoCreateRowSorter(true);
table.setFillsViewportHeight(true);
table.setComponentPopupMenu(new TablePopupMenu());
add(new JScrollPane(table));
setPreferredSize(new Dimension(320, 240));
}
private void init() {
setOpaque( false );
getSelectionModel().clearSelection();
getSelectionModel().setAnchorSelectionIndex(-1);
getSelectionModel().setLeadSelectionIndex(-1);
setAutoscrolls( false );
setShowHorizontalLines(false);
setShowVerticalLines( false);
setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
setTableHeader( null );
// Calc row height here so that TableModel can adjust number of columns.
calcRowHeight(getOffscreenGraphics());
//mouse click into the table performs the switching
addMouseListener( new MouseAdapter() {
@Override
public void mousePressed( MouseEvent e ) {
int row = rowAtPoint( e.getPoint() );
int col = columnAtPoint( e.getPoint() );
if( row >= 0 && col >= 0 ) {
if( select( row, col ) ) {
performSwitching();
}
}
}
});
//icon for top-level items with sub-items
rightArrowLabel.setIcon( new ArrowIcon() );
rightArrowLabel.setIconTextGap( 2 );
rightArrowLabel.setHorizontalTextPosition( JLabel.LEFT );
topItemPanel.setLayout( new BorderLayout(5, 0) );
topItemPanel.add( rightArrowLabel, BorderLayout.EAST );
topItemPanel.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(Table.class, "ACD_OTHER_EDITORS") );
topItemPanel.setBorder( BorderFactory.createEmptyBorder( 0, 0, 0, 2) );
//adjust column widths to accomodate the widest item in each column
for( int col=0; col<getColumnCount(); col++ ) {
if( getSwitcherModel().isTopItemColumn( col ) )
adjustColumnWidths( col );
}
//include the width of vertical scrollbar if there are too many rows
int maxRowCount = getSwitcherModel().getMaxRowCount();
if( maxRowCount > MAX_VISIBLE_ROWS && getRowCount() <= MAX_VISIBLE_ROWS ) {
JScrollPane scroll = new JScrollPane();
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
int scrollWidth = scroll.getVerticalScrollBar().getPreferredSize().width;
TableColumn tc = getColumnModel().getColumn( getColumnCount()-1 );
tc.setMaxWidth( tc.getMaxWidth() + scrollWidth );
tc.setPreferredWidth( tc.getPreferredWidth() + scrollWidth );
tc.setWidth( tc.getWidth() + scrollWidth );
}
}
/**
* Configure a column model.
*
* @param model Column model.
*/
public void configureColumnModel(TableColumnModel model) {
TableColumn column;
column = model.getColumn(COLUMN_BACKLINKS_MAIN);
column.setMinWidth(50);
column.setPreferredWidth(50);
column.setMaxWidth(100);
column = model.getColumn(COLUMN_BACKLINKS_OTHER);
column.setMinWidth(50);
column.setPreferredWidth(50);
column.setMaxWidth(100);
column = model.getColumn(COLUMN_BACKLINKS_TEMPLATE);
column.setMinWidth(40);
column.setPreferredWidth(40);
column.setMaxWidth(100);
column = model.getColumn(COLUMN_COMMENTS_TEXT);
column.setMinWidth(60);
column = model.getColumn(COLUMN_DISAMBIGUATION);
column.setMinWidth(20);
column.setPreferredWidth(20);
column.setMaxWidth(20);
column.setCellRenderer(
new BooleanIconCellRenderer("commons-disambig-colour.png", null));
column.setHeaderRenderer(
new IconCellRenderer("commons-disambig-colour.png"));
column = model.getColumn(COLUMN_REDIRECT);
column.setMinWidth(20);
column.setPreferredWidth(20);
column.setMaxWidth(20);
column.setCellRenderer(
new BooleanIconCellRenderer("commons-redirect-arrow-without-text.png", null));
column.setHeaderRenderer(
new IconCellRenderer("commons-redirect-arrow-without-text.png"));
column = model.getColumn(COLUMN_PAGE);
column.setMinWidth(100);
column.setPreferredWidth(200);
column = model.getColumn(COLUMN_WATCHED);
column.setMinWidth(20);
column.setPreferredWidth(20);
column.setMaxWidth(20);
column.setCellRenderer(
new BooleanIconCellRenderer("gnome-logviewer.png", null));
column.setHeaderRenderer(
new IconCellRenderer("gnome-logviewer.png"));
}