下面列出了javax.swing.JTable#setSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Forces the table to resize given column.
*/
private void resize(int newWidth, JTable table) {
int oldWidth = getWidth();
JTableHeader header = table.getTableHeader();
if (header == null) {
return;
}
header.setResizingColumn(this);
final int oldMin = getMinWidth();
final int oldMax = getMaxWidth();
setMinWidth(newWidth);
setMaxWidth(newWidth);
setWidth(newWidth);
// The trick is to restore the original values
// after the table has be layouted. During layout this column
// has fixed width (by setting min==max==preffered)
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setMinWidth(oldMin);
setMaxWidth(oldMax);
}
});
Container container;
if ((header.getParent() == null) ||
((container = header.getParent().getParent()) == null) ||
!(container instanceof JScrollPane)) {
header.setResizingColumn(null);
return;
}
if (!container.getComponentOrientation().isLeftToRight() &&
! header.getComponentOrientation().isLeftToRight()) {
if (table != null) {
JViewport viewport = ((JScrollPane)container).getViewport();
int viewportWidth = viewport.getWidth();
int diff = newWidth - oldWidth;
int newHeaderWidth = table.getWidth() + diff;
/* Resize a table */
Dimension tableSize = table.getSize();
tableSize.width += diff;
table.setSize(tableSize);
/* If this table is in AUTO_RESIZE_OFF mode and
* has a horizontal scrollbar, we need to update
* a view's position.
*/
if ((newHeaderWidth >= viewportWidth) &&
(table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
Point p = viewport.getViewPosition();
p.x = Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
viewport.setViewPosition(p);
}
}
}
header.setResizingColumn(null);
}
public TaskList(TaskHandler handler) {
this();
this.handler = handler;
model = new SingleHandlerTaskModel(handler);
table = new JTable(model);
table.setAutoCreateColumnsFromModel(true);
table.setDefaultRenderer(String.class, new DefaultTableCellRenderer());
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
update(0,0);
table.setSize(640,200);
table.setPreferredSize(new Dimension(640, 200));
pack();
}
private static void checkTableGridLines() {
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 10;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int row, int col) {
return " ";
}
};
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setOpaque(true);
r.setBackground(CELL_RENDERER_BACKGROUND_COLOR);
JTable table = new JTable(dataModel);
table.setSize(WIDTH, HEIGHT);
table.setDefaultRenderer(Object.class, r);
table.setGridColor(GRID_COLOR);
table.setShowGrid(true);
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
table.setBackground(TABLE_BACKGROUND_COLOR);
checkTableGridLines(table);
}
private static void checkTableGridLines() {
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 10;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int row, int col) {
return " ";
}
};
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setOpaque(true);
r.setBackground(CELL_RENDERER_BACKGROUND_COLOR);
JTable table = new JTable(dataModel);
table.setSize(WIDTH, HEIGHT);
table.setDefaultRenderer(Object.class, r);
table.setGridColor(GRID_COLOR);
table.setShowGrid(true);
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
table.setBackground(TABLE_BACKGROUND_COLOR);
checkTableGridLines(table);
}
public TaskList(List handlers) {
this();
this.handlers = handlers;
model = new MultipleHandlerTaskModel(handlers);
table = new JTable(model);
table.setAutoCreateColumnsFromModel(true);
table.setDefaultRenderer(String.class, new DefaultTableCellRenderer());
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
update(0,0);
table.setSize(640,200);
table.setPreferredSize(new Dimension(640, 200));
pack();
}
private JComponent getMappingsPanel() {
// Mappings panel
final ActionMap actionMap = this.getContext().getActionMap(this.getClass(), this);
tableModel = new PortMappingsTableModel(app);
mappingsTable = new JTable(tableModel);
mappingsTable.setAutoCreateRowSorter(true);
mappingsTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
mappingsTable.setSize(new Dimension(400, 100));
mappingsTable.getSelectionModel().addListSelectionListener(
e -> firePropertyChange(PROPERTY_MAPPING_SELECTED, false, isMappingSelected()));
final JScrollPane mappingsTabelPane = new JScrollPane();
mappingsTabelPane.setViewportView(mappingsTable);
final JPanel mappingsPanel = new JPanel(new MigLayout("", "[fill,grow]", "[grow,fill][]"));
mappingsPanel.setName("port_mappings");
final Border panelBorder = BorderFactory
.createTitledBorder(app.getResourceMap().getString("mainFrame.port_mappings.title"));
mappingsPanel.setBorder(panelBorder);
mappingsPanel.add(mappingsTabelPane, "height 100::, span 2, wrap");
mappingsPanel.add(new JButton(actionMap.get(ACTION_REMOVE_MAPPINGS)), "");
mappingsPanel.add(new JButton(actionMap.get(ACTION_UPDATE_PORT_MAPPINGS)), "wrap");
return mappingsPanel;
}
private void setSizeAndDimensions(@NotNull JTable table, @NotNull JBPopup popup,
@NotNull RelativePoint popupPosition, @NotNull List<UsageNode> data) {
JComponent content = popup.getContent();
Window window = SwingUtilities.windowForComponent(content);
Dimension d = window.getSize();
int width = calcMaxWidth(table);
width = (int) Math.max(d.getWidth(), width);
Dimension headerSize = ((AbstractPopup) popup).getHeaderPreferredSize();
width = Math.max((int) headerSize.getWidth(), width);
width = Math.max(myWidth, width);
if (myWidth == -1) myWidth = width;
int newWidth = Math.max(width, d.width + width - myWidth);
myWidth = newWidth;
int rowsToShow = Math.min(30, data.size());
Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
dimension = rectangle.getSize();
Point location = window.getLocation();
if (!location.equals(rectangle.getLocation())) {
window.setLocation(rectangle.getLocation());
}
if (!data.isEmpty()) {
TableScrollingUtil.ensureSelectionExists(table);
}
table.setSize(dimension);
//table.setPreferredSize(dimension);
//table.setMaximumSize(dimension);
//table.setPreferredScrollableViewportSize(dimension);
Dimension footerSize = ((AbstractPopup) popup).getFooterPreferredSize();
int newHeight = (int) (dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
Dimension newDim = new Dimension(dimension.width, newHeight);
window.setSize(newDim);
window.setMinimumSize(newDim);
window.setMaximumSize(newDim);
window.validate();
window.repaint();
table.revalidate();
table.repaint();
}
private void setSizeAndDimensions(@NotNull JTable table,
@NotNull JBPopup popup,
@NotNull RelativePoint popupPosition,
@NotNull List<UsageNode> data) {
JComponent content = popup.getContent();
Window window = SwingUtilities.windowForComponent(content);
Dimension d = window.getSize();
int width = calcMaxWidth(table);
width = (int)Math.max(d.getWidth(), width);
Dimension headerSize = ((AbstractPopup)popup).getHeaderPreferredSize();
width = Math.max((int)headerSize.getWidth(), width);
width = Math.max(myWidth, width);
if (myWidth == -1) myWidth = width;
int newWidth = Math.max(width, d.width + width - myWidth);
myWidth = newWidth;
int rowsToShow = Math.min(30, data.size());
Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
dimension = rectangle.getSize();
Point location = window.getLocation();
if (!location.equals(rectangle.getLocation())) {
window.setLocation(rectangle.getLocation());
}
if (!data.isEmpty()) {
TableScrollingUtil.ensureSelectionExists(table);
}
table.setSize(dimension);
//table.setPreferredSize(dimension);
//table.setMaximumSize(dimension);
//table.setPreferredScrollableViewportSize(dimension);
Dimension footerSize = ((AbstractPopup)popup).getFooterPreferredSize();
int newHeight = (int)(dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
Dimension newDim = new Dimension(dimension.width, newHeight);
window.setSize(newDim);
window.setMinimumSize(newDim);
window.setMaximumSize(newDim);
window.validate();
window.repaint();
table.revalidate();
table.repaint();
}