We had some problems with the calculation of the correct sizes for JTables. I noticed that we allocated too much height (one pixel per row to be exact). I fixed the size methods in BasicTableUI for this.
2006-06-06 Roman Kennke <[EMAIL PROTECTED]>
* javax/swing/plaf/basic/BasicTableUI.java
(getMaximumSize): Don't return null. Fixed calculation of
table height.
(getMinimumSize): Don't return null. Fixed calculation of
table height.
(getPreferredSize): Fixed calculation of with and height. Added
API docs.
(getHeight): New helper method.
/Roman
--
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.52
diff -u -1 -0 -r1.52 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java 23 May 2006 18:10:32 -0000 1.52
+++ javax/swing/plaf/basic/BasicTableUI.java 6 Jun 2006 09:36:00 -0000
@@ -71,20 +71,21 @@
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.ActionMapUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.InputMapUIResource;
import javax.swing.plaf.TableUI;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
+import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class BasicTableUI extends TableUI
{
public static ComponentUI createUI(JComponent comp)
{
return new BasicTableUI();
}
@@ -378,51 +379,78 @@
* @param comp the component whose maximum size is being queried,
* this is ignored.
* @return a Dimension object representing the maximum size of the table,
* or null if the table has no elements.
*/
public Dimension getMaximumSize(JComponent comp)
{
int maxTotalColumnWidth = 0;
for (int i = 0; i < table.getColumnCount(); i++)
maxTotalColumnWidth += table.getColumnModel().getColumn(i).getMaxWidth();
- if (maxTotalColumnWidth == 0 || table.getRowCount() == 0)
- return null;
- return new Dimension(maxTotalColumnWidth, table.getRowCount()*
- (table.getRowHeight()+table.getRowMargin()));
+
+ return new Dimension(maxTotalColumnWidth, getHeight());
}
/**
* Return the minimum size of the table. The minimum height is the row
* height times the number of rows. The minimum width is the sum of
* the minimum widths of each column.
*
* @param comp the component whose minimum size is being queried,
* this is ignored.
* @return a Dimension object representing the minimum size of the table,
* or null if the table has no elements.
*/
public Dimension getMinimumSize(JComponent comp)
{
int minTotalColumnWidth = 0;
for (int i = 0; i < table.getColumnCount(); i++)
minTotalColumnWidth += table.getColumnModel().getColumn(i).getMinWidth();
- if (minTotalColumnWidth == 0 || table.getRowCount() == 0)
- return null;
- return new Dimension(minTotalColumnWidth, table.getRowCount()*table.getRowHeight());
+
+ return new Dimension(minTotalColumnWidth, getHeight());
}
+ /**
+ * Returns the preferred size for the table of that UI.
+ *
+ * @param comp ignored, the <code>table</code> field is used instead
+ *
+ * @return the preferred size for the table of that UI
+ */
public Dimension getPreferredSize(JComponent comp)
{
- int width = table.getColumnModel().getTotalColumnWidth();
- int height = table.getRowCount() * (table.getRowHeight()+table.getRowMargin());
- return new Dimension(width, height);
+ int prefTotalColumnWidth = 0;
+ for (int i = 0; i < table.getColumnCount(); i++)
+ {
+ TableColumn col = table.getColumnModel().getColumn(i);
+ prefTotalColumnWidth += col.getPreferredWidth();
+ }
+ return new Dimension(prefTotalColumnWidth, getHeight());
+ }
+
+ /**
+ * Returns the table height. This helper method is used by
+ * [EMAIL PROTECTED] #getMinimumSize(JComponent)}, [EMAIL PROTECTED] #getPreferredSize(JComponent)}
+ * and [EMAIL PROTECTED] #getMaximumSize(JComponent)} to determine the table height.
+ *
+ * @return the table height
+ */
+ private int getHeight()
+ {
+ int height = 0;
+ int rowCount = table.getRowCount();
+ if (rowCount > 0 && table.getColumnCount() > 0)
+ {
+ Rectangle r = table.getCellRect(rowCount - 1, 0, true);
+ height = r.y + r.height;
+ }
+ return height;
}
protected void installDefaults()
{
LookAndFeel.installColorsAndFont(table, "Table.background",
"Table.foreground", "Table.font");
table.setGridColor(UIManager.getColor("Table.gridColor"));
table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
table.setOpaque(true);
@@ -1307,17 +1335,17 @@
// paint horizontal grid lines
if (grid != null && table.getShowHorizontalLines())
{
Color save = gfx.getColor();
gfx.setColor(grid);
int y = top - rowMargin;
for (int r = r0; r <= rn; ++r)
{
// The horizontal grid is draw below the cells, so we
// add before drawing.
- y += table.getRowHeight(r);// + rowMargin;
+ y += table.getRowHeight(r);
gfx.drawLine(left, y, p2.x, y);
}
gfx.setColor(save);
}
}
}
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
