List of usage examples for java.awt Component getFontMetrics
public FontMetrics getFontMetrics(Font font)
From source file:Main.java
/** * Calculates the average character width for the given {@link Component}. * This can be useful as a scaling factor when designing for font scaling. * * @param component the {@link Component} for which to calculate the * average character width./*from www.j a v a 2 s . c om*/ * @return The average width in pixels */ public static float getComponentAverageCharacterWidth(Component component) { FontMetrics metrics = component.getFontMetrics(component.getFont()); int i = 0; float avgWidth = 0; for (int width : metrics.getWidths()) { avgWidth += width; i++; } return avgWidth / i; }
From source file:Main.java
/** * Returns the height of the given component. * * @param c the component where the text is contained * @return the height of the text/*from ww w . jav a 2 s .c o m*/ */ public static int getStringHeight(Component c) { // get metrics from the graphics FontMetrics metrics = c.getFontMetrics(c.getFont()); // get the height of a line of text in this font and render context int hgt = metrics.getHeight(); // calculate the height of a box to hold the text with some padding. return hgt + 2; }
From source file:Main.java
/** * Returns the size of the given text computed towards to the given * component./*from w w w . j a v a 2 s . c o m*/ * * @param c the component where the text is contained * @param text the text to measure * @return the dimensions of the text */ public static Dimension getStringSize(Component c, String text) { // get metrics from the graphics FontMetrics metrics = c.getFontMetrics(c.getFont()); // get the height of a line of text in this font and render context int hgt = metrics.getHeight(); // get the advance of my text in this font and render context int adv = metrics.stringWidth(text); // calculate the size of a box to hold the text with some padding. return new Dimension(adv + 2, hgt + 2); }
From source file:Main.java
public static FontMetrics getFontMetrics(Component component) { Font font = component.getFont(); if (font == null) { font = Font.getFont(Font.DIALOG); }//w w w . j a va2s . c o m return component.getFontMetrics(font); }
From source file:edu.ku.brc.specify.tasks.subpane.wb.WorkbenchPaneSS.java
/** * Adjust all the column width for the data in the column, this may be handles with JDK 1.6 (6.) * @param tableArg the table that should have it's columns adjusted *//*from w ww . j a v a2 s . c o m*/ private void initColumnSizes(final JTable tableArg, final JButton theSaveBtn) throws Exception { TableModel tblModel = tableArg.getModel(); TableColumn column = null; Component comp = null; int headerWidth = 0; int cellWidth = 0; Element uploadDefs = null; if (WorkbenchTask.isCustomizedSchema()) { uploadDefs = XMLHelper.readFileToDOM4J( new File(UIRegistry.getAppDataDir() + File.separator + "specify_workbench_upload_def.xml")); } else { uploadDefs = XMLHelper.readDOMFromConfigDir("specify_workbench_upload_def.xml"); } //UIRegistry.getInstance().hookUpUndoableEditListener(cellEditor); Vector<WorkbenchTemplateMappingItem> wbtmis = new Vector<WorkbenchTemplateMappingItem>(); wbtmis.addAll(workbench.getWorkbenchTemplate().getWorkbenchTemplateMappingItems()); Collections.sort(wbtmis); DBTableIdMgr databaseSchema = WorkbenchTask.getDatabaseSchema(); columnMaxWidths = new Integer[tableArg.getColumnCount()]; for (int i = 0; i < wbtmis.size() /*tableArg.getColumnCount()*/; i++) { TableCellRenderer headerRenderer = tableArg.getColumnModel().getColumn(i).getHeaderRenderer(); WorkbenchTemplateMappingItem wbtmi = wbtmis.elementAt(i); // Now go retrieve the data length int fieldWidth = WorkbenchDataItem.getMaxWBCellLength(); DBTableInfo ti = databaseSchema.getInfoById(wbtmi.getSrcTableId()); if (ti != null) { DBFieldInfo fi = ti.getFieldByName(wbtmi.getFieldName()); if (fi != null) { wbtmi.setFieldInfo(fi); //System.out.println(fi.getName()+" "+fi.getLength()+" "+fi.getType()); if (RecordTypeCodeBuilder.getTypeCode(fi) == null && fi.getLength() > 0) { fieldWidth = Math.min(fi.getLength(), WorkbenchDataItem.getMaxWBCellLength()); } } else { log.error("Can't find field with name [" + wbtmi.getFieldName() + "]"); } } else { log.error("Can't find table [" + wbtmi.getSrcTableId() + "]"); } columnMaxWidths[i] = new Integer(fieldWidth); GridCellEditor cellEditor = getCellEditor(wbtmi, fieldWidth, theSaveBtn, uploadDefs); column = tableArg.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; comp = tableArg.getDefaultRenderer(tblModel.getColumnClass(i)).getTableCellRendererComponent(tableArg, tblModel.getValueAt(0, i), false, false, 0, i); cellWidth = comp.getPreferredSize().width; //comp.setBackground(Color.WHITE); int maxWidth = headerWidth + 10; TableModel m = tableArg.getModel(); FontMetrics fm = comp.getFontMetrics(comp.getFont()); for (int row = 0; row < tableArg.getModel().getRowCount(); row++) { String text = m.getValueAt(row, i).toString(); maxWidth = Math.max(maxWidth, fm.stringWidth(text) + 10); //log.debug(i+" "+maxWidth); } //XXX: Before Swing 1.1 Beta 2, use setMinWidth instead. //log.debug(Math.max(maxWidth, cellWidth)); //log.debug(Math.min(Math.max(maxWidth, cellWidth), 400)); column.setPreferredWidth(Math.min(Math.max(maxWidth, cellWidth), 400)); column.setCellEditor(cellEditor); } //tableArg.setCellEditor(cellEditor); }