Overriding javax.swing.JButton and drawing the text

I am overriding the javax.swing.JButton paintComponent() I would like to know how to center the text on the button
here is part of the code, the text is printed near the end:
       ((java.awt.Graphics2D)grph2d).setRenderingHint(
                   java.awt.RenderingHints.KEY_ANTIALIASING,
                   java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
       grph2d.setColor(new java.awt.Color(0.0f,1.0f,0.0f,1f));
       grph2d.fillRect(0,0,getWidth(),getHeight());
       if(!this.model.isRollover()){
              grph2d.setColor(new java.awt.Color(0,125,0));
              grph2d.fillRect(0,0,getWidth(),getHeight());
       if(model.isPressed()){
            grph2d.setColor(new java.awt.Color(1f,0f,0f,1f));
            grph2d.fillOval(0,0,getWidth(),getHeight());
       if(model.isArmed()){
            grph2d.setColor(new java.awt.Color(0f,0f,1f,1f));
            grph2d.fillOval(0,0,getWidth(),getHeight());
       grph2d.setColor(new java.awt.Color(0.0f,0.0f,0.0f));
// text draws but isn't centered very well
       grph2d.drawString(this.getText(),
            this.getMargin().left + getX(),
            this.getMargin().top + getY());is there any way to auto center the text, or calculate the length of the text in pixels.

       grph2d.drawString(this.getText(),
            getWidth()/2 - (grph2d.getFontMetrics().stringWidth(getText())/2),
            grph2d.getFontMetrics().getHeight());thanks! works much better now.

Similar Messages

  • I have a PDF with a diagram, I need to translate the text so I open it in Illustrator.  The document opnes up vertically oriented  I try to put it horizontal to be able to read and change the text  However everything I have done is not working  The artboa

    I have a PDF with a diagram, I need to translate the text so I open it in Illustrator.  The document opens up vertically oriented  I try to put it horizontal to be able to read and change the text  However everything I have done is not working  The artboard does change, but the diagram stays vertical.....Please help  I need to finish that for today..Please help

    A PDF usually consists of one to an indefinite number of clipping masks inside each other.
    You need to be very carefull with selecting stuff.
    So you need to read the manual on how to select stuff and on the basics of Illustrator.
    On top of that: because of the complex hierarchy of PDF files, it is kind of difficult to help you without seeing anything.

  • Trouble with viewing font on spine of Photo book.  I can't find a way to view and edit the text which appears on the spine of the dustcover or the book itself when printed.

    I can't find a way to view and edit the text which appears on the spine of the dustcover or the photo book itself.  When I preview the book it appears on the dustcover and the book, but not when I'm back in the creation or editing windows.  I've printed 4 books in a series and two spines are one font size, and two others are a different font size.  One of the books has a shortened title on the book spine.

    The text on the spine will be the same text that is in the Title text box on the title page.  You can change the font and style of that text just like you change any text in Aperture. Aperture will size and center the spine text for you
    If you want the content or the font and style  of the text to be different from your Title box text then you have to trick the program a bit.  Put the text that you want in the spine into the Title text box and then move that box to a place on the page where it will not be visible when printed ( Y coordinate -1.0 inches for example).  Don't move it too far away or it will be hard to get back to in order to make an edit.    Then make a new text box on the title page and put the text you want on the cover in that.  (Or make a book without text on the cover , but with text on the spine). 
    You are 100% correct in that you can not preview this unless you preview the printed book

  • I get a dialogue box asking "What should Firefox do with this file" Options open with or save file. My wife selected save file and now it only opens note pad and displays the text of the file to be saved. OK in windows Internet Explorer.

    I get a dialogue box asking "What should Firefox do with this file" Options open with or save file. My wife selected save file and now it only opens note pad and displays the text of the file to be saved. OK in windows Internet Explorer.

    P.S. Site is http://www.coldwatercreek.com

  • Executing a native process and getting the text output

    Hi,
    I have the following problem.
    When I execute a native DOS process (lets say a batch file), and
    expect some text output from this process I use the following code:
    String command = "test.bat";
    Process process = runtime.exec(command);
    BufferedReader rd = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String s = new String();
    while((s = rd.readLine())!=null) {
         out.println(s + "<br/>" );
    The problem is that as long as rd.readLine() does not return null, the page does not display the result. When the process crash, sometimes nothing return.
    I would like to display the output as it comes, i.e. line by line.
    I have no clue on how to do that except writting the output in a text file, and reading the text file with a page that reload every x seconds.
    I do not beleive this is a good solution.
    Additionaly, I may have a huge output. It seems to make the process
    to hang. Is there a was to fix it.
    Could someone give me some hints.
    Regards,
    Arnaud.

    I am having the same problem...did you ever solve it?

  • Firefox Sync setup says "Incorrect words, try again" after receiving message "Your answer was correct. Please copy and paste the text in this text box into the box below."

    When setting up Firefox Sync, I get all the way to the "Please Confirm You're Not a Robot" screen in the wizard, and enter the captcha values. I then receive in a small scrollable text area the message "Your answer was correct. Please copy and paste the text in this text box into the box below."
    I copy and paste the code, in this case "03AHJ_Vuv1gStVScBWSYWw6M4_KKQ4wzhCDSzKqiCfv57XXGyvHtOk4ixehysQhOTIf-lsL41lc5n-piztjYJSKu6urCeRvETVfpzRDh7OmHCDZiIBZv9Qg4oyrmWDMu6JA7Eh-4C8aZn4" into the only other visible text box, and click the next button, only to receive the message "Incorrect words, try again."
    My only option is to cancel at that point. I have tried this three times, once even starting the entire process over from the start, and it ends up with the same outcome each time.

    When setting up Firefox Sync, I get all the way to the "Please Confirm You're Not a Robot" screen in the wizard, and enter the captcha values. I then receive in a small scrollable text area the message "Your answer was correct. Please copy and paste the text in this text box into the box below."
    I copy and paste the code, in this case "03AHJ_Vuv1gStVScBWSYWw6M4_KKQ4wzhCDSzKqiCfv57XXGyvHtOk4ixehysQhOTIf-lsL41lc5n-piztjYJSKu6urCeRvETVfpzRDh7OmHCDZiIBZv9Qg4oyrmWDMu6JA7Eh-4C8aZn4" into the only other visible text box, and click the next button, only to receive the message "Incorrect words, try again."
    My only option is to cancel at that point. I have tried this three times, once even starting the entire process over from the start, and it ends up with the same outcome each time.

  • Drag and drop the text and image

    Hi,
    I need help in drag and drop text and image.
    I am able to drag and drop the text and image in doc but  i want to drag the text in text frame and image in image frame only.
    For this how to get textframe id or is there any other way to do this. Pls help me if anyone know how to do this one.
    Thanks
    Arun

    This thread is pretty old, but this can help anyone in the future:
    To handle a drop in InDesign, you have to implement your own custom CDragDropTargetFlavorHelper. You can find a working example in SDK Sample. Take a look at BasicDragDrop.sdk project in the SDK.

  • Opening files and viewing the text

    I'm trying to write a method to open a .doc file and display the text to screen, the program runs but nothing appears on the screen. Is the code below the correct way or is it flawed.
    class Open implements ActionListener
              public void actionPerformed(ActionEvent event)
                   try {
                   JTextPane view = app.setScreen();
                 BufferedReader in = new BufferedReader(new FileReader("outfilename.doc"));
                 String str;
                 while ((str = in.readLine()) != null) {
                 view.setText(str);     
            in.close();
        } catch (IOException e) {
         }

    I'm trying to write a method to open a .doc file and
    display the text to screen, the program runs but
    nothing appears on the screen. Is the code below the
    correct way or is it flawed.
    class Open implements ActionListener
              public void actionPerformed(ActionEvent event)
                   try {
                   JTextPane view = app.setScreen();
    BufferedReader in = new BufferedReader(new
    w FileReader("outfilename.doc"));
         String str;
         while ((str = in.readLine()) != null) {
         view.setText(str);     
    in.close();
    atch (IOException e) {
    Hard to tell why you aren't getting any output without more of the program.
    One thing I see is that this classes replaces the text of JTextPane with the last string read from the file. If the last string were a blank line then you would have nothing to show.
    If you are assuming this is a reasonable size document then you might consider appending each line read into as StringBuffer then set the text of the JTextPane to the contents of the StringBuffer after the reaches then end.

  • How to copy and paste the text in Russian - 'command+c' erases it

    How to copy and paste the text in Russian - 'command+c' erases it

    I want to publish a book with Blurb. They don't have Russian fonts, so I have to type the text in Russian in Notes in my iPad, send it to my e-mail box, and then open the e-mail box in Mac, and copy and paste the text into the book template of Blurbs. I don't know how to make a Russian keyboard in Mac to be able to type quickly. I asked about it  in the Apple store, and they showed me how to find a Russian keyboard in the net, but it takes much time to type using a screen keyboard, and the texts also cannot be copied and pasted, when they are in Russian. They are erased when I press 'command+C", and all I have is a single letter 'c' instead. No problems with texts in English. Maybe Russian is not supported for copying and pasting?:(

  • I've created a Photoshop PDF and all the text is editbale after passing through indesign to add crop marks and bleeds - one line now isn't editable, what's causing this

    I've created a Photoshop PDF and all the text is editable in the resulting PDF.
    The PDF created by Photoshop is completely editable, all text! I put it through indesign so I could add crop marks and reduce the pdf file size and now all the text is editable except one line!
    I've not used any faux styles or effects...what could be causing this?
    Thanks!

    I've created a Photoshop PDF and all the text is editable in the resulting PDF.
    The PDF created by Photoshop is completely editable, all text! I put it through indesign so I could add crop marks and reduce the pdf file size and now all the text is editable except one line!
    I've not used any faux styles or effects...what could be causing this?
    Thanks!

  • I updated my 4s, and now the text messaging is so slow its almost unusable (type then wait 20  seconds to see characters)

    I updated my 4s, and now the text messaging is so slow its almost unusable (type then wait 20 seconds or more to see characters on the screen).  Help Please!

    It is possible that there is a problem with the files sessionstore.js and sessionstore.bak in the Firefox Profile Folder.
    Delete the sessionstore.js [2] file and possible sessionstore-##.js [3] files with a number and sessionstore.bak in the Firefox Profile Folder.
    * Help > Troubleshooting Information > Profile Directory: Open Containing Folder
    * http://kb.mozillazine.org/Profile_folder_-_Firefox
    Deleting sessionstore.js will cause App Tabs and Tab Groups and open and closed (undo) tabs to get lost, so you will have to create them again (make a note or bookmark them).
    See also:
    * [1] http://kb.mozillazine.org/Session_Restore
    * [2] http://kb.mozillazine.org/sessionstore.js
    * [3] http://kb.mozillazine.org/Multiple_profile_files_created

  • HT1414 I backed up my iphone last weekend.  Today (one week later), I was downloading music and noticed the text messages I send to myself (about 1 1/2 years worth) are gone.  Now, I can't find the backup from last week to try to get them back.  help???

    I backed up my iphone last weekend.  Today (one week later), I was downloading music and noticed the text messages I send to myself (about 1 1/2 years worth) are gone.  Now, I can't find the backup from last week to try to get them back.  help???

    You cannot update iPhone 4 to IOS 8.1.2, the highest you can update is IOS 7.1.2.
    Unless you've iPhone 4S (looks similar to iPhone 4)

  • Tool for the coding of pl/sql and drawing the flow chart

    dear sir,
    please kindly recommend one tool for the coding, debuging the
    pl/sql and drawing the flow chart.
    thanks

    Hello,
    You can use Tools Provided by KeepTool It includes,
    1. ER Diagrammer
    2. SQL Editior
    3. HORA,DBA Tool
    4. Pl/SQl Debugger
    www. keeptool.com
    www.keeptool.de
    Adi

  • HT3529 i have just updated my iphone to the ios 7.0.2 and now the text message bubble has changed from blue to green, is this as it should be?

    i have recently updated my iphone to the ios 7.0.2 version and now the text message bubble has changed from blue to green, is this as it should be?

    You will need to purchase the upgrade CD for 10.6 from the Apple Online store, OR you can likely get one at an Apple Retail Store.  It is the last version that was available on CD.  After 10.6, the upgrades are available in the Mac App Store. 
    HTH

  • The accursed little dark gray ball with white in the center and white arrows, either two or four, which seize control of the screen and move the text or pictures up, down, left, right and can be stopped only by finding the ball and clicking it. I want to

    The accursed little dark gray ball with white in the center and white arrows, either two or four, which seize control of the screen and move the text or pictures up, down, left, right and can be stopped only by finding the ball and clicking it. I want to stop the thing permanently. Firefox 3.6 and 3.64
    == This happened ==
    Every time Firefox opened
    == with 3.60

    The accursed little dark gray ball with white in the center and white arrows, either two or four, which seize control of the screen and move the text or pictures up, down, left, right and can be stopped only by finding the ball and clicking it. I want to stop the thing permanently. Firefox 3.6 and 3.64
    == This happened ==
    Every time Firefox opened
    == with 3.60

Maybe you are looking for

  • How do I set the delivery policy for a queue in iMQ 2.0?

    The list on page 67 of the 2.0 administration guide appears to be incomplete. Specifically, I'm interested in knowing how to set the default delivery policy for a Queue through the jmqobjmgr command. I need the valid attribute name to pass in to jmqo

  • Successful install of Seagate 1TB SSHD

    I finally have successfully installed a new Seagate 1TB SSHD drive, and thought I'd describe the process and some problems I had and how I got around them.  I can't claim this is the best or proper way to do the job, as I was floundering around a lot

  • How do I get my plugins to work?

    I have just upgraded (?)  to Elements 13 but I cannot get my plugins to work...... If I use the "Additional Plugins"  option you can only have 1 plugin at a time working.   I use the Nik collection and also DXO Viewpoint 2 if they are not going to wo

  • Spool to PDF - Problems with downloading PDF file when converting with job

    Hi all! I've got a problem. I've got a program that writes a smartform into the spool. After that, I am using the function modules RSTS_GET_ATTRIBUTES and CONVERT_OTFSPOOLJOB_2_PDF to convert it into PDF. I retrieve a internal table with the binary c

  • Cursor in swing

    I am very new to swing, and have gone thru the swing tutorial looking for how to make the cursor invisible, and I just cant seem to find it. Can someone help me out? I have a textarea and I want no blinking cursor in the area. THANKS John McDonald