JLabel - calculating the number of lines in HTML wrapped text

Hi folks,
I've run into a problem. I have a JTable with row and column headers embedded within a JScrollPane. I have this component sized just how I like it. What I want to do is add a label above it serving as a title to the chart. I want this title to respect the width of the table - in other words, if the text within it is too long, I want it to wrap rather than to enlarge the size of the Table to fit it.
I found that wrapping the title in "<html><center></center></html>" tags takes care of the wrapping. And furthermore I found code to handle the "dimension" of said label.
     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4348815
    class WrappableJLabel extends JLabel {
           private final int preferredWidth;
           public WrappableJLabel(final int preferredWidth)
              this.preferredWidth=preferredWidth;
           @Override
           public Dimension getPreferredSize()
              Dimension superPreferred=super.getPreferredSize();
              return new Dimension
                  (int) Math.min(preferredWidth,superPreferred.getWidth()),
                  (int) superPreferred.getHeight()
    }The problem is that the height does not get calculated correctly for the label. If I knew how many lines the JLabel's text took up, then I could multiply the height returned by the getPreferredSize() method by that number and get the correct size. I need to do all of this at compile time (so calling getHeight() on label doesn't help) so that I can size the JPanel holding both my table and my label to be of the correct size.
Does anyone have any clues on how to do this?
Thank you

Maybe you need to calculate it yourself. The Java Developers Almanac 1.4 contains an general example:
[http://www.exampledepot.com/egs/java.awt/TextDim.html|http://www.exampledepot.com/egs/java.awt/TextDim.html]
You can obtain the font with the getFont() method on any component.

Similar Messages

  • Set the number of lines allowed in multi-line text field

    Hi,
    I am designing a form where I have some multi-line text fields where I only want the user to be able to enter a maximum of say 5 lines. I know I could do this by enabling the "Limit Length to Visible Area" option, however, this option is ignored by Reader 7 and the field scrolls, hiding any text that spills beyond the visible area from printouts. It works great in 8 though. To avoid this bug/feature in Reader 7, is there some script that I can use to limit the number of lines allowed in a text field?
    Thanks

    I ran into similar issues switching between Acrobat/Reader. Here is the prominent code that works for me when I use this in "Enter" event of the textField.
    var aSOM=this.somExpression.substr(15,this.somExpression.length - 1);
    var acroField = event.target.getField(aSOM);
    acroField.doNotScroll = true;
    I chose Allow Multiple Lines alone and noting else.
    Good Luck,
    SekharN

  • Java Comiler - How to find out the Number of lines of Executable code

    Hello,
    Is there any option we have or could have to get the "Number of Lines of Executable Code" while we run the java compiler?
    Technically Java is an interpreter. So in case the term is incorrect, please excuse me.
    Since the compiler anyway traverses through the Java files, it is easy and correct to get the number from the compiler rather than having any external tool calculating the same.
    It should remove the comments, blank lines and take care of java single sentence written in multiple lines.
    Any suggestions or help is highly appreciated.
    regards,
    Sabyasachi

    How many lines of code are there in this program?
    public class sabre20110211 {public static void main(String[] args) {for (int i = 0; i < 10; i++)System.out.println(i);}}And how many in this program?
    public
            class
            sabre20110211
        public
                static
                void
                main(
                String[]
                args
            for
                    (int i = 0;
            i < 10;
            i++
                System.out.println(i);
    }

  • How can I find out the number of lines in a text file?

    How can I find out the number of lines in a text file?

    java.io.BufferedReader in = new java.io.BufferedReader( new java.io.FileReader( "YourFile.txt" ) );
    int lineCount = 0;
    while( in.readLine() != null )
    lineCount ++;
    System.out.println( "Line Count = " + lineCount );

  • How do you find the number of lines in a file?

    I need to count how many lines there are in a file, I am using a BufferedReader to read in the data, but how can I find the number of lines?
    Thanks

    That depends. How do you define a line? Is it a specific number of
    characters, a String that's terminated by a newline character or some
    combination thereof?

  • How do I increase the number of lines presented in a drop down list?

    When I am entering a single character/number for each of a random selection of three letters in a password verification, I get a drop down list from A to S only, the first 20. Accessing letter T to number 9 means scrolling down. How can I increase the number of lines in a drop down box to 36?

    Hi canddski,
    If you are taking about an interface on a website, this is up to the ui designer. But there is a reflow that depends on screensize for the Firefox UI.
    You can use asp to control the list:
    [http://forums.asp.net/t/1970301.aspx?How+can+i+display+selected+no+of+records+from+datatable+using+dropdown+list+without+database+]
    but also try asking on stackoverflow.com

  • How to get the number of lines of a FileReader

    I need to calculate the number of lines of a FileReader object. How can I do that ?

    I wrote the following some while ago. It assumes that a line is terminated by '\n' but it should be easy to adapt.
    import java.io.*;
    public class LineCounter
        public static int countLines(File file, String encoding) throws IOException
            int lineCount = 0;
            Reader reader = new InputStreamReader(new FileInputStream(file), encoding);
            char[] buffer = new char[4096];
            for (int charsRead = 0; (charsRead = reader.read(buffer)) >= 0;)
                for (int charIndex = 0; charIndex < charsRead ; charIndex++)
                    if (buffer[charIndex] == '\n')
                        lineCount++;
            reader.close();
            return lineCount;
        public static void main(String[] args)
            try
                File file = new File("/home/sabre/work/dev/maps/EUROPE.RIV");
                long startTime = System.currentTimeMillis();
                int lineCount = countLines(file, "UTF-8");
                double time =  (System.currentTimeMillis() - startTime) / 1000.0;
                System.out.println("File size = " + file.length() + " contains " + lineCount + " lines taking " + time);
            catch (Exception e)
                e.printStackTrace();
    }

  • How to get the number of lines of a file?

    Folks:
    Is there a way to get the number of lines of a text file? I don't know if there is an existing method to do that.
    Thanks a lot.

    HI
    I found some solution
    other than increment loop and all
    here is the code it might helpful to u
    //returns the number of lines in a file
    //author : Ravindra S
    //Symphony software Hyderabad
    try
    RandomAccessFile randFile = new RandomAccessFile(csvFile,"r");
    long lastRec=randFile.length();
    randFile.close();
    FileReader fileRead = new FileReader(csvFile);
    LineNumberReader lineRead = new LineNumberReader(fileRead);
    lineRead.skip(lastRec);
    countRec=lineRead.getLineNumber()-1;
    fileRead.close();
    lineRead.close();
    catch(IOException e)

  • Increasing the number of line items in PO

    hello fellow abapers,
    i have a query. i dont know if its the headache of the function ppl or the technicial person - which happens to be me - but i have to get this solved at the earliest.
    now ive made a PO for materials from the scratch and everythin is workin fine. except that out of the blue comes a PO with over 10000 line items. yes, thats right - 10,000 line items.
    so obviously an error was thrown by SAP. unfortunately the end-user did not think it necessary to take a printout or record the error message.
    anyways the error said something to the effect that it did not allow so many line items or somthing like that.
    now can anybody pls help me out with this. firstly is dhere any restrictions on the number of line items, and secondly, if thats the case how do i find a work-around.
    thanx a bunch
    pk

    Hi,
    The basis sets the maximum number of pages that can be printed by the user in authorization object S_SPO_PAGE.
    Please discuss with your basis guy.
    Tyken

  • Determine the number of lines of textfield or floating field

    Hi,
    Is there a way to determine the number of lines of text in a textfield or in a floatingfield.
    My problem is that I'm creating a form with three tables(made of subforms) underneed each other that needs to be set to hidden when the total of rows is greater than 25.
    Now it would be easy to just count the rows but these rows can exist of multiple lines of text.
    The fields of my row are floating fields so when the text is bigger than the displayable width the text continous on a new line in the same row. So I need to know when this happens because now I can show only 24 rows.
    I tried counting the characters but this doesn't work, when there are a lot of 'i,j,l' or other small characters I can place 40 characters in my textfield, but when there are a lot of 'm,w' characters I can place only 22 characters in my field.
    I also tried to use xfa.layout.h(TextField1,"cm") to determine the height of the field, but it always retuns the value -1. I get the same -1 with the y or x - position.
    Does anybody have an idea how to solve this problem?
    Thanks in advance,
    RonnyR

    oops ... i got it thanks.
    code below -----
    for(var i=0; i <= s.length-1; i++)
    sCurrentLetter = s.substring(i,i+1);
    if (s.charCodeAt(i) == 13)
    // this is a line break

  • How can i read the number of lines written - in spreadshee​t array?

    hello NI,
                           I am writing file as spreadsheet format continuously, contains date, time, chanel1, channel 2, channel 3..... etc.
    i just want to know how many lines has been written after file write....
    Regards,
    Balaji DP

    You count the number of line termination char. Se example 
    Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
    (Sorry no Labview "brag list" so far)
    Attachments:
    sample.PNG ‏25 KB

  • How can i know the number of lines in field-symbol internal table

    how can i know the number of lines in field-symbol internal table

    Hi,
    If your field symbol has been defined as an internal table :
    Use std describe as
    Data: l type i.
    describe <fs> lines l.
    'l' will contain the number of lines as needed.
    FYI
    The size of this storage area in a field symbols depends on the number of table lines which is not fixed, but determined dynamically at runtime.
    Regards,
    Amit

  • How to count the number of lines dynamically,

    In the below code am trying to read the lines which are selected using a check box, also am categorizing the contents depending on the follow up material.
    after displaying one category contents am displaying a line
    which also counts to a line in the internal table.
    My question is how to count the number of lines(ULINE) displayed dynamically.
    FORM GET_LINES .
      DATA: LV_LINES TYPE I,
            LV_TIMES TYPE I,
            LV_TABIX TYPE SY-TABIX.
      DESCRIBE TABLE IT_REC LINES LV_LINES.
      DO LV_LINES TIMES.
        LV_TIMES = SY-INDEX .
        READ LINE LV_TIMES FIELD VALUE IT_REC-CHECK INTO GV_CHECK.
        IF SY-SUBRC EQ 0 AND GV_CHECK IS NOT INITIAL.
          LV_TABIX =  LV_TIMES.
          READ TABLE IT_REC INDEX LV_TABIX INTO GWA_UPDATE.
          IF SY-SUBRC EQ 0.
            APPEND GWA_UPDATE TO GT_UPDATE.
          ENDIF.
        ENDIF.
      ENDDO.
    ENDFORM.                    " GET_LINES

    In the below code am trying to read the lines which are selected using a check box, also am categorizing the contents depending on the follow up material.
    after displaying one category contents am displaying a line
    which also counts to a line in the internal table.
    My question is how to count the number of lines(ULINE) displayed dynamically.
    FORM GET_LINES .
      DATA: LV_LINES TYPE I,
            LV_TIMES TYPE I,
            LV_TABIX TYPE SY-TABIX.
      DESCRIBE TABLE IT_REC LINES LV_LINES.
      DO LV_LINES TIMES.
        LV_TIMES = SY-INDEX .
        READ LINE LV_TIMES FIELD VALUE IT_REC-CHECK INTO GV_CHECK.
        IF SY-SUBRC EQ 0 AND GV_CHECK IS NOT INITIAL.
          LV_TABIX =  LV_TIMES.
          READ TABLE IT_REC INDEX LV_TABIX INTO GWA_UPDATE.
          IF SY-SUBRC EQ 0.
            APPEND GWA_UPDATE TO GT_UPDATE.
          ENDIF.
        ENDIF.
      ENDDO.
    ENDFORM.                    " GET_LINES
    The display function is:
    FORM DISPLAY_DATA .
      ULINE.
      WRITE :  /1 SY-VLINE, 'check',
                10 SY-VLINE, 'Plant',
               20 SY-VLINE, 'Material number',
               50 SY-VLINE, 'Follow up material',
               70 SY-VLINE, 'Safety stock',
              100 SY-VLINE, 'Partc'.
      ULINE.
      LOOP AT IT_MARC.
        MOVE: IT_MARC-WERKS TO IT_REC-WERKS,
              IT_MARC-MATNR TO IT_REC-MATNR,
              IT_MARC-NFMAT TO IT_REC-NFMAT,
              IT_MARC-EISBE TO IT_REC-EISBE,
              IT_MARC-PARTC TO IT_REC-PARTC .
        APPEND IT_REC.
        CLEAR IT_MARC.
      ENDLOOP.
      DATA: GV_TABIX TYPE SY-TABIX.
      LOOP AT IT_REC.
        GV_TABIX = SY-TABIX.
        READ TABLE GT_TOTAL WITH KEY WERKS = IT_REC-WERKS
                                     NFMAT = IT_REC-NFMAT.
        IF SY-SUBRC EQ 0.
          IT_REC-PARTC = GT_TOTAL-PARTC.
          MODIFY IT_REC INDEX GV_TABIX TRANSPORTING PARTC.
        ENDIF.
      ENDLOOP.
      LOOP AT IT_REC.
        WRITE : /1 SY-VLINE, IT_REC-CHECK AS CHECKBOX,
                10 SY-VLINE, IT_REC-WERKS,
                20 SY-VLINE, IT_REC-MATNR,
                50 SY-VLINE, IT_REC-NFMAT,
                70 SY-VLINE, IT_REC-EISBE,
               100 SY-VLINE, IT_REC-PARTC.
        AT END OF NFMAT.
          ULINE.
        ENDAT.
      ENDLOOP.
      ULINE.
    ENDFORM.                    " DISPLAY_DATA
    Solved

  • Count the number of lines in a txt file

    I need to count the number of lines in a txt file, but I can't do it using readLine(). This is because the txt file is double spaced. readLine() returns null even if it is not the end of the file. thanks for the help

    I need to count the number of lines in a txt file,
    but I can't do it using readLine(). Then just compare each single byte or char to the newline (code 10).
    This is because the txt file is double spaced. readLine() returns
    null even if it is not the end of the file.Errm what? What do you mean by "double spaced"? Method readLine() should only return null if there's nothing more to read.

  • Count the number of lines in  each file in a directory

    Hello,
    I would like to count the number of lines for each file in my directory and subdirectories.
    I wonder how can I count the lines of the specified files?
    Here is the program which is listed the files in the specified directory:
                        String path = "C:/User/Studies/Pracices/src/Pract1";
           String files;
           File folder = new File(path);
           File[] listOfFiles = folder.listFiles();
           for (int i = 0; i < listOfFiles.length; i++)
            if (listOfFiles.isFile())
         files = listOfFiles[i].getName();
         System.out.println(files);

    Ok so one more question.
    Actually my path is pointing to one Directory and like here I can just read from one file that the name of the file should be specified in the path is it possible to help me in that part?
    File f = new File("C:/User/Studies/Practices/src/Pract1/");
    FileReader fr = new FileReader(f);
          BufferedReader br = new BufferedReader(fr);
          String str = br.readLine();
              LineNumberReader ln = new LineNumberReader(br);
              int count = 0;
              while (ln.readLine()!=null)
                  count++;
              System.out.println("no. of lines in the file = " + count);

Maybe you are looking for

  • Can't see image in File Open box for CR2 files.

    I just upgraded to PSE 13. When I open a folder with my RAW files, CR2 format, I only see icons, not the actual image. I didn't have this issue with PSE 11. Do I need to download a codec to allow me to view the files? I am running Windows7 Profession

  • Canon MP970:  Degrading Performance, Through PC. HELP

    This one has me pulling my hair out. My wife & I have a set up of an XP PC & iMAC dual core in room. We have the MP970 network printer, set up on our wireless network in room. Canon is very specific on how you have to do this to work with 2 CPUs, & w

  • The option " open in low resolution " in not there

    When i right click on any app ,, and go to " Get info "  the option  " open in low resolution " in not there , my OS is up to date as well the app i clicked on and its happen on all apps ,,, why ? macbook pro reitna 2.7 16gb

  • How can provider-hosted app accessible to anonymous users online

    Hi, I am new SharePoint and still learning it. I have created a Provider Hosted App. built in asp.net Web application. This apps retrieve all file to shared documents of sharepoint online using REST API. it's access only register user. The problem is

  • Saprouter config with RFC on tcp port 33xx

    I have a customer who has configured saprouter to allow remote (from the Internet) connections via the SAP GUI.  These connections work great.  However, when they try to add entries to the route tables for TCP port 3300, 3301, and 3303 the external a