String width

Hi
Is there anyway to determine width and height(in pixels or whatever its possible) a String is? I dont want the number of characters in it, I want the "width" and the "height" of some given String with a particular font name,style and size???
i tried using getStringWidth() in FontMetrics. its giving the width but its not considering the string value. i wanted to get the width for string width also considering the value of string.
can i use getStringBounds(String str, Graphics context)???
but what is this Graphics context??
i wanted to use it in a core java pgm.not using any awt or swing etc???
Does anyone know how to do this?
help me out plz??

can i use getStringBounds(String str, Graphics context)???
but what is this Graphics context??The context is the graphics object to which you wish to draw this string. As strings sizes are done in a resolution independent format, the size in pixels changes depending on the resolution of the device. You would want a 12pt String to be about the same size if displayed either on 72dpi monitor, or a 300dpi printout.

Similar Messages

  • How do I get a strings width w/o getFontMetrics()?

    Now that getFontMetrics() is deprecated - how am I supposed to get the width of a string? The FontMetrics class had a stringWidth() method.

    I dont have the API docs here so I cant give u the exact method names and classes but heres generally how you can do it: You have the Graphics object for the screen/whatever that you want to calculate the bounds for so do this
    Graphics2D g = ...
    FontRenderContext frc = g.getFontRenderContext();
    Font font = g.getFont();
    Rectangle2D bounds = font.getStringBounds("foo", frc);
    or something like that, and if you dont have the Graphics object you can always pass that part by calling the FontRenderContext constructor and passing some null values to it and so creating your own context that does some basic calculations correctly anyways.. Enuff for any font bounds anyway.

  • String width in pixels

    Hi All,
    I'm making a small bar chart program that needs to print the bar values at the very end of the bar.
    Is there a way to determine the width of a string in pixels given it's font and length in characters?
    Basically, I'm trying to get the very last character of the value string to be right aligned with the bar itself without having it run off the edge or be too far towards the center of the bar.
    Also, I don't suppose there is a way to print strings leftward instead of rightward?? No no, of course not, I'm just being silly ;) right?
    Thanks for the help
    Marcos

    Anybody ever hear of StringBuffer???
    Fair enough and yes, yes I have thank you -though I feel it is often better for the newbie to see the process manually and if they study this kinfd of thing then they may learn to work stuff out for themselves when they need to implement their own algorithm.
    This - Snr whoIsFred - is what makes a programmer,
    - knowing how to manipulate your data types to make things work
    - it is not about knowing the API's off by heart (though that can help)
    - knowing how to read and utilise the java API docs is the real key
    I think what was posted therefore is moare beneficial than StringBuffer(aString).reverse()

  • String widths

    Hello all,
    Is it possible to get the width of a String? By this I mean that actual space that the String takes up not .length().
    The reason I need this is because I have a JPS that populates an HTML menu with 2 pieces of text on each row. To align the second piece of text properly I need to find out the width of the first piece and apply the neccessary spacing.
    Any ideas?

    Well, you can try and work with Font and FontRenderContext ...
    But you should consider the fact, that you can't really control which font a user specifies in its browser preferences. Therefore it is usually preferable to use HTML elements to do the spacing for you (e.g. with tables).

  • Calculate String Width

    Is it possible to calculate the width of a String in a certain font without needing a Graphics object? I need to calculate this before a component is displayed in order to adjust the sizes of the components. getGraphics() is returning null at this point.

    Didn't my original post answer that question?
    My control is being wrapped in a JScrollPane. I need to set the preferred size of the scroll pane based on the width of a String. If I try to do that during initialization before it is displayed, getGraphics() returns null so I get a NullPointerException.

  • Find the width of a text string inside a text box?

    Hi,
    I have a text box with a long text string in it. I would like to know what the width of the text string is so that I can scroll it inside the textbox using the text box scrollviewer.
    var sv = this.foodieItemText.GetFirstDescendantOfType<ScrollViewer>();
    sv.ScrollToHorizontalOffsetWithAnimation(this.foodieItemText.Width, 20);
    The scrolling works fine but I don't know what value to use for the width offset in the first argument to ScrollToHorizontalOffsetWithAnimation. For my purposes I want it to scroll the full length of the text string (which is longer than the actual with of
    the text box). Any ideas?

    Hi duffybr,
    Yes, there is a ScrollViewer inside TextBox template and we could scroll to the specific position as we need, however there is no direct way to get the string width in TextBox, however we could get the width in TextBlock, by measure method.
    See this discussion for more information:
    http://stackoverflow.com/questions/9126076/how-can-i-calculate-the-width-of-a-string-in-metro-without-displaying-it
    Besides, there are some third party libraries help us get the String width.
    --James
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Measuring string bounds in headless mode on a server?

    I have a problem with calculating screen string width for the browser client.
    I'm outputting a stream of content type application/vnd.ms-excel and sending back an XML spreadsheet. (No complaints about using Mc$oft products, it's what's needed!)
    Excel does not autosize columns which contain string data, so the text columns are the standard Excel width, and the text is wrapped onto about 7 lines. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexcl2k2/html/odc_xmlss.asp for details of the XML format.
    Excel does allow you to specify the with of a column in points, so I was using the awt Font classes to calculate the string bounds in pixels. I explicitly set the resolution to be used to 72dpi by creating a FontRenderContext with an AffineTransform with a scaling factor of (1.0, 1.0), so that asking the pixel width, gets the point width.
    This worked fine testing it on my windoze machine, but on the Unix server, it's obviously running in headless mode, and anyway, it doesn't have the Arial font which I'm specifying in my spreadsheet as the font to use.
    Is there any way of calculating the width of a string in points knowing its font and the point size of the font? You shouldn't need a graphcs environment, just the font information. I have all the .TTF files, I could copy them to the Unix machine if Java could use them and do the calculation.
    I hate leaving the user interface looking so shoddy with wrong sized columns!
    It SHOULD have worked in a headless environment according to http://java.sun.com/j2se/1.4.2/docs/guide/awt/AWTChanges.html#headless and we are on version 1.4.2_01. It actually blew up in awt code:
    java.lang.NoClassDefFoundError
         at java.lang.Class.forName0(Native Method)
         at java.lang.Class.forName(Class.java:141)
         at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
         at java.awt.Font.initializeFont(Font.java:308)
         at java.awt.Font.<init>(Font.java:344)That's when I'm trying to instantiate a new Font("Arial", Font.PLAIN, 10);I've looked at the FOP project wondering whether that has a solution but it's a huge, confusing thing. There must be a way. Can anyone help?

    When you say "unix doesn't have Arial" you obviously aren't using Solaris where Arial most certainly
    ships and is in fact the main font used to support dialog & sanserif.
    There are numerous APIs to calculate the width of a string in pixels, depending on whether
    you want the integer metrics, fp metrics , logical bounds, pixel bounds (which guarantees to enclose
    every pixel if you correctly specify the graphics/FRC)
    Every single one of these works in headless mode.
    Even the most simple-minded FontMetrics.stringWidth(String) call should be good enough
    for your requirements.
    The error you show looks like you have a misconfigured environment.. It can't find
    the correct implementation class of GraphicsEnvironment which is nothing to do with
    fonts. Moreover this DOES work for me :
    import java.awt.*;
    public class Arial {
    public static void main(String args[]) {
    Font arial = new Font("Arial", Font.PLAIN, 10);
    System.out.println(arial);
    % java -version
    java version "1.4.2_01"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06)
    Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)
    % java -Djava.awt.headless=true Arial
    java.awt.Font[family=Arial,name=Arial,style=plain,size=10]
    QED.
    -Phil.

  • Strange Behaviour setting column width & height programmatically

    I'm trying to set the height and width of an image by storing the image, height and width in the database at the row level.
    OATableBean table = (OATableBean)webBean.findIndexedChildRecursive("QuoteLinesRN");
    OAImageBean lineImageBean = (OAImageBean)table.findIndexedChildRecursive("ImageItem");
    //Next 4 lines set the image file name from the data in Segment1 - they work as expected
    FixedBoundValue imageDirectory = new FixedBoundValue(APPS_MEDIA_DIRECTORY);
    OADataBoundValueViewObject statusBinding = new OADataBoundValueViewObject(lineImageBean, "Segment1");
    ConcatBoundValue statusCBV = new ConcatBoundValue(new BoundValue[] {imageDirectory, statusBinding});
    lineImageBean.setAttributeValue(SOURCE_ATTR, statusCBV);
    //Now here is the funny part -
    OAMessageStyledTextBean heightBean = (OAMessageStyledTextBean)webBean.findIndexedChildRecursive("Segment2");
    OAMessageStyledTextBean widthBean = (OAMessageStyledTextBean)webBean.findIndexedChildRecursive("Segment3");
    String height = (String)heightBean.getValue(pageContext);
    String width = (String)widthBean.getValue(pageContext);
    lineImageBean.setHeight(height);
    //Setting the width doesn't do anything if I set the height. And the image displays miraculously at the dimensions set in the actual physical file itself, for both height and width.
    //The values I have in the table to get the height and width are completely irrelevant.
    //Except I need to make a string out of the OAMessageStyleTextBean or the value for height and width will be as I specify ie. if I instead use: String height = "300" the height will be 300.
    //But why is the height and width not using the values I specify in the table as Segment2 and Segment3?
    //Is it because I'm not explicity setting the same index when creating the OAMessageStyledTextBeans as I used for my OAImageBean?

    I found some issues.
    http://javafx-jira.kenai.com/browse/RT-24636
    http://javafx-jira.kenai.com/browse/RT-22728
    http://javafx-jira.kenai.com/browse/RT-23517
    On my mind, these are the same.

  • "getBounds" of a String

    i am writing a program that can generate report.
    but i have problem on right-alignmnet Chinese when printing..
    the getBounds() function not return correct string width.
    how can i solve the problem??

    Check out the link shown below:
    http://forum.java.sun.com/thread.jsp?forum=20&thread=319214
    I find that the getStringBounds method returns the most accurate info compared to other method such as getBounds or getStringWidth.
    ;o)
    V.V.

  • TableView auto resize columns based on header text width or cell content

    Is there a method on the tableview or tablecolumn to automatically resize based on the content of the hearder or data?
    If not what approach can used used with JavaFX to calculate the string width based on the current font?

    The table column size is inconsistent, I set the columns in the table view and the items in the initialize method. I execute a service and task from a button action that loads data into the list, if I don't reset the columns in the table view in the succeed method then each column is the same width regardless of the content in the cells. If I do reset the table columns in the succeed method then the columns are resized based on content.
    I don't think it should be a requirement to reset the columns just to get the column widths to set based on the content in the cells. Is there a method on the table column or view that will resize the cells based on the cell content? I would prefer the largest value; column header content or the longest cell content for a column.

  • IText trouble with automatically adjustment of table width

    With iText I an producing a PDF file with some elements. One of these is a table which is supposed to be centered.
    In this table 2x2 with input form a Servlet. This input is a string of variable length, in other words I do not know the length of it.
    I want this table to automatically adjust the cells width according to the length of the String. As I understand iText does not posess this functionality.
    This is how I have created the table: The first column has a width of 0.3 because the values in those I allready know. The default value is centered and 80% span, and because of this the table is not centered according to the text since the content contains empty space, but the 80% span table is centered.
    PdfPTable table = new PdfPTable(2);
    float[] widths = { 0.3f, 0.8f };
    table.setWidths(widths);How could I calculate this? The string has a font size of 22pt. Could I check for the number of chars in the String and adjust the width accordingly? It seems like I need to set the table.setWidthPercentage(50) in addition to the width size. But if I dont know the relationship between the width and percentages.
    If I calculate the width to be 0.6f with font size 22 and length x, how much percentages should I use?
    Message was edited by:
    DJ_Viking
    Message was edited by:
    DJ_Viking

    You need to use FontMetrics. In particular, use the FontMetrics of the table's renderer component so the string width will be calculated correctly even if the cell's value is bold. Something like this:
    Component comp = table.getDefaultRenderer(Object.class).getTableCellRendererComponent(table, cellValue, false, false, row, column);
    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    int cellWidth = fm.stringWidth(cellValue);
    table.getColumnModel().getColumn(column).setWidth(cellWidth);

  • Fix the Column Width in WAD 7.0

    Hi Guys,
                 I have two Queries in a Web Template and when I execute the Web Template in Web, both the Query results are are populated one below the other. The column width for both the Query output from a Web Template when executed in Web needs to be the same.
    Any ideas and suggestions will be really appreciated. Thanks again Guys.
    Thanks,
    Harsha.

    The width attribute of af:column element specifies column width.
    The setWidth(java.lang.String width) method of oracle.adf.view.faces.component.core.data.CoreColumn
    also specifies column width.

  • Can fix the column width in af:table

    I am using af:table, i need to fix the static column width, it can be able to do it?
    plz help,.....

    The width attribute of af:column element specifies column width.
    The setWidth(java.lang.String width) method of oracle.adf.view.faces.component.core.data.CoreColumn
    also specifies column width.

  • ComboBox drop down width

    No matter what width I set for a combobox, the FIRST time I click to show the drop down list, the width of the dropdown is about 100px (irrespective of the width of the combobox itself). If you click off the drop down and re-open, it is sized correctly to the with of the widest string.
    I've tried setting all manner of options in SceneBuilder, and also tried setting the prefWidth in a CellFactory, but makes no difference. I get the same behaviour on Windows or Linux.
    I'm using this version of Java:
    java version "1.7.0_21"
    Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

    Java8 renders ALL of the UI really poorly, which is another issue entirely.
    I've located the cause of my problem. When you create a ComboBox with SceneBuilder, the FXML creates this by default:
    <items>
    <FXCollections fx:factory="observableArrayList">
    <String fx:value="Item 1" />
    <String fx:value="Item 2" />
    <String fx:value="Item 3" />
    </FXCollections>
    </items>
    I'm actually loading the list via a database lookup, something like:
    myCombo.setItems(myList);
    The problem is that when the drop down is first rendered, it's using the string widths from the initial list of strings to calculate the drop down dimensions.
    I have "solved" the problem in my app by creating the combobox in code and adding it to the scene, in which case everything works correctly. It would be nice if there was a way to tell the combobox to recalculate it's preferred sizes after a list was added/updated, but I'm unable to find a method that might do that (tried Node.autosize() without success).

  • String height

    hi
    I dont know whether this is the correct forum to ask my doubt.
    i calculated the string width of a string with a font style,font size,name etc.
    eg:
    Font font = new Font(fontName, style, size);
    JButton jButton = new JButton();
    FontMetrics fontMetrics = jButton.getFontMetrics(font);
    width = fontMetrics.stringWidth(inputString);
    i also want to get the height of the string also.how can i get it.i want the height of the string only based on font name, style, size.
    is there any method to find it.
    plz help me.
    rgds

    FontMetrics.getHeight
    FontMetrics.getMaxHeight

Maybe you are looking for

  • ITunes 10.7 Won't open

    Just updated iTunes and now it won't open. Anyone else had the same problem? I've reinstalled, run Disk Utility and all the other normal fixes.

  • ITunes 12.0.1 windows file movement

    Hi I have always been a Windows user, and can find my way around the file system without having to think about it. I use iTunes for an extensive music/movie collection and have sorted 99.9% of the files metadata so that itunes recognises it correctly

  • Creating 3D text in Motion 4

    Hi, I am new to Motion. I have Motion 4 as part of the Final Cut Studio package. Hopefully, this is not too much of a newbie question: I would like to create a 3D phone number that slides and rotates up in a commercial. I combed the web for tutorials

  • First row checked by default

    Hi, I am using ADF and JSF . For my table , there is a radio button in front of each row. Originally, the first row of the table is checked by default. But after clicking the next button, no row is selected. How to make the first row always is select

  • Will ios 7 still be supported by apple with the release of ios 8?

    Will ios 7 still be supported by apple with the release of ios 8?