Location of Text in JLabel

Hi, I want to retrieve the location co-ordinates from a JLabel.
I know that JLabel.getPreferredSize() gets the size of the actual text but I also need to know where in the JLabel the text is located.
This is because I want to create an image out of it and need specified co-ordinates (x,y,width,height).
//Considerate

The preferred size of the label is equal to the size of the text plus the insets of the label. Use the getInsets() method.

Similar Messages

  • How to center the text in JLabel without image?

    I need to center the text inside JLabel, how do i do it?
    Also I need some advise, what is the best way to mark a clicked card (I'm making a card game)?

    I found this method that controls that text alignment:you can try few more:
    label.setVerticalAlignment(JLabel.CENTER);    //these two would  center the label contents
    label.setHorizontalAlignment(JLabel.CENTER);
    label.setHorizontalTextPosition(JLabel.CENTER);Thanks!

  • How to highlight text in JLabel????

    I m writing code for find functionality(similar to ctrl+f )..
    I have a JTable with some of its cell contains JLabel...
    I want to highlight some part of text from JLabel using using tableCellRenderer
    (eg. text on JLabel is "India" & my search keyword is "In", then only first two letters of JLabel should get highlighted)..
    It works, if Jtable contains string but not for any JComponent(like JLabel, JPanel, JCheckBox etc..) ..
    Please help me out for this....
    NOte: ---
    I m posting some part of code that i have used to highlight string in a JTable cell..
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                         boolean isSelected, boolean hasFocus, int row, int column) {
                 // method to highlight WORD depending upon condition..
                           try {
                              high.addHighlight( i, j, highlight_painter );          // method to highlight word
                         catch (Exception e) {
                              e.printStackTrace();
                return this;
            }thanks in advance
    suyog

    I am posting my code that i am using for highlighting text in JTable...
    But this code is not working, if cell contains JLabel...
    Can you please suggest me any solution to work this code for JLabel with plain text content & with hyperlink (formatted using HTML tags), with out affecting to its original functionality
    (I am not getting what cramick want to suggest... )
    public class TableCellRendererBug{
         // code to highlight the specific WORD in JTable
         Vector vrecord;          // vector used in condition to highlight
         JTable tab;
         public  DefaultHighlighter high ;
             public void highlightWord(JTable table,Vector record){
                  tab=table;
                  vrecord =new Vector(1);
                  vrecord=record;
                  tab.setDefaultRenderer(Object.class, new CellHighlightRenderer());
        class CellHighlightRenderer extends JTextField implements TableCellRenderer {
              DefaultHighlighter.DefaultHighlightPainter highlight_painter =
                        new DefaultHighlighter.DefaultHighlightPainter(new Color(198,198,250));
             public  DefaultHighlighter high ;
            public CellHighlightRenderer() {
                 high = new DefaultHighlighter();       
                 setBorder( BorderFactory.createEmptyBorder() );
                setHighlighter(high);
                  tab.updateUI();
            public Component getTableCellRendererComponent(JTable table, Object value,
                                                         boolean isSelected, boolean hasFocus, int row, int column) {
                 // method to highlight WORD depending upon condition..
                    setFont(table.getFont());
                setValue(value);                    // intialize object
               *    condition to execute addHighlight..
               *    Here i am using  above mentioned Vector & all the things
               if(condition){
                          try {
                              high.addHighlight( start, end, highlight_painter );          // method to highlight word
                         catch (Exception e) {
                              e.printStackTrace();
                     }//if
                return this;
            protected void setValue(Object value) {
                 // method to initialize value of object
                 setText((value == null) ? "" : value.toString());
    }Thanks In advance..
    Suyog

  • I can't seem to locate the text and buttons on a flash template I bought

    Hi Everyone,
    I have been working with this flash template I purchased online and can't seem to be able to change the text that is displayed on it's buttons except for the first one. There are four buttons to the side but I can only change the first button's contents as I see them in the "Library". I am new to Flash and learning. Please guide me as to where I can find the other components.
    I can only see and change "WorldWide" and "Call Anytime Anywehre" button.
    Flash source file posted here:
    https://docs.google.com/leaf?id=0B8tFparkP0rKMzliNTcxZDktYmQ3My00ZjJiLWFhMDQtNTEzNWExMWZlN Dgx&hl=en&authkey=CKrQwcwF
    I would also like to add a link to each button and I can't seem to locate the URL Link button. I am using Adobe Flash CS5.
    Much appreciated.    

    What I described is what your file involves.  In your library there is a "text" folder .  In that folder are movieclips named txt_1, and txt_2 which hold the text used in your buttons.  If you open those movieclips for editing you will see each has four frames with different text in each frame.  That is where you need to change your text for the buttons.
    If you look at the actionscript in the top layer of those movieclips, you will see how it uses a value assigned to the button/mc to determine which frame it displays....
    gotoAndStop(_parent._parent.num);
    And if you look at the actionscript applied to the button on the main timeline (select the button with the Actions window open), you will see how that num value gets assigned to each instance...
    onClipEvent (load) {
    num = 1;
    this.hitArea = this.area;

  • JPanel doesn't show new text after JLabel edit

    Hello,
    I am trying to create a custom progress bar-esk window to show users during lengthy methods.
    I have a new Thread which creates and displays a new ProgressWindow object (a JFrame with a JPanel and JLabel attatched). At second intervals the text of the JLabel attatched to this frame will change. however the text seen in the window is not updated.
    System.out's show the value of the JLable to be correctly changing.
    Here is the code for creating the Thread and window:
         Runnable r = new Runnable() {
                        public void run() {
                            try {
                                prog = new ProgressWindow();//creates the frame
                                prog.addStuff();//adds the components
                                prog.addListeners(inter);
                               while(true){
                                   prog.makeAnimation();
                            } catch (InterruptedException ex) {
                                ex.printStackTrace();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                   new Thread(r).start();and the code within the ProgressWindow class which updates the label
    public void makeAnimation() throws InterruptedException, MalformedURLException {
            Thread.sleep(1000);
            if (dots == 0) {
                System.out.println("dots 0");
                labelPanel.remove(dotty);
                dotty = new JLabel("Analysing input  ");
                labelPanel.add(dotty);
                labelPanel.repaint();
                dots++;
            } else if (dots == 1) {
                System.out.println("dots 1");
                labelPanel.remove(dotty);
                dotty = new JLabel("Analysing input .");
                labelPanel.add(dotty);
                labelPanel.repaint();
                dots++;
            } else if (dots == 2) {
                System.out.println("dots 2");
                labelPanel.remove(dotty);
                dotty = new JLabel("Analysing input ..");
                labelPanel.add(dotty);
                labelPanel.repaint();
                dots++;    
            } else if (dots == 3) {
                System.out.println("dots 3");
                labelPanel.remove(dotty);
                dotty = new JLabel("Analysing input ...");
                labelPanel.add(dotty);
                labelPanel.repaint();
                dots = 0; 
        }I assume this is a threading issue, however I don't understand why the value is correctly changing, but not being repainted. I'd appriciate any insight :)
    thanks :)

    Fillis52 wrote:
    ..As for the SSCCE, I have looked at the linked site and honestly thought I had met the requirements. The code was formatted, and only the methods which presented the problem were given. Should I have included the code to create the window also?The first 'S' seems covered, but it is not SC, C or an E. Which part of the words associated with those letters are you having trouble understanding?
    If you had copy/pasted your code into the STBC (as recommended in the SSCCE document), it would have been obvious that it was not an SSCCE.
    Instead of being rebuked, I should be told what is missing from my post in order to create a SSCCE as i obviously dont know how to do it properly.You expect us to have a crystal ball? Instead of complaining, it would be more productive to be specific about what part of S-SC-C-E document you do not understand. And this time, read it carefully, don't skim it.

  • Location of Icon in JLabel

    I am writing an application that responds to a mouse click in a JLabel. This Icon has to move as the window resizes (as default in GridLayout). Despite this I need to be able to translate clicking on the top corner of the ImageIcon to clicking on (0,0).
    I know that this top corner will be red, so I could search the JLabel for the first pixel of that color. I'm not sure how to do that though.
    If there is an easier way to figure out where the top corner of a particular icon in a label is, though, it would be greatly appreciated.
    Thanks in advance,
    Sarah

    This is the function from BasicLabelUI that is used to position the icon.
    iconR is the icon Rectangle.
        protected String layoutCL(
            JLabel label,                 
            FontMetrics fontMetrics,
            String text,
            Icon icon,
            Rectangle viewR,
            Rectangle iconR,
            Rectangle textR)
            return SwingUtilities.layoutCompoundLabel(
                (JComponent) label,
                fontMetrics,
                text,
                icon,
                label.getVerticalAlignment(),
                label.getHorizontalAlignment(),
                label.getVerticalTextPosition(),
                label.getHorizontalTextPosition(),
                viewR,
                iconR,
                textR,
                label.getIconTextGap());
        }This is the call to this method:
            JLabel label = (JLabel)c;
            String text = label.getText();
            Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
            if ((icon == null) && (text == null)) {
                return;
            FontMetrics fm = SwingUtilities2.getFontMetrics(label, g);
            Insets insets = c.getInsets(paintViewInsets);
            paintViewR.x = insets.left;
            paintViewR.y = insets.top;
            paintViewR.width = c.getWidth() - (insets.left + insets.right);
            paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
            paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
            paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
            String clippedText =
                layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

  • Centering Text within JLabel

    I am trying to figure out a way to center the text within a JLabel. I trying to set a specific size of the label, and then center the text in the very middle (horizontally) of that label. I have tried many different methods from this forum and other places on Google, but none of them have had any effect. For example:
    label = new JLabel ("<html>blah, blah, blah, blahblah, blah...</html>"); 
                                                         // HTML tags make the lines wrap.
              label.setHorizontalAlignment(JLabel.CENTER);
              label.setPreferredSize (new Dimension (84, 48));The text remains aligned left within the label. Is there a way to get it to center within the label?

    what version are you using?
    your posted code works OK in 1.5.0_05
    import java.awt.*;
    import javax.swing.*;
    class Testing extends JFrame
      public Testing()
        setSize(300,100);
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        JLabel label = new JLabel("<html>line 1<br>line 2</html>");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setPreferredSize (new Dimension (84, 48));
        label.setBorder(BorderFactory.createLineBorder(Color.black));
        p.add(label);
        getContentPane().add(p);
      public static void main(String[] args){new Testing().setVisible(true);}
    }

  • Can i show an HTML text on JLabel

    Can any body help me about showing HTML text on a JLabel component!!
    Thanks.
    Neeraj

    http://forums.java.sun.com/thread.jsp?forum=57&thread=151899

  • How to locate italic text

    I need to find all the locations where italic text is in the pdf document. It seems to me that there is not a way to do this with Acrobat X. Would someone please suggest a method to do this.

    It's not possible with a script, either. Maybe with a plugin. Try the Acrobat SDK forum.

  • Disable marking the text in JLabel

    Hello,
    I use the setText(String) method to set the text in the JLabel,
    But now i need to disable the marking of that text, while pressing the mouse and moving on the text.
    Please let me know how to do that.
    Regards
    javaxjini

    hi,
    try this:     JTextField fd = null;
         fd.addMouseMotionListener
              new java.awt.event.MouseMotionAdapter ()
                   public void mouseDragged (java.awt.event.MouseEvent e)
                        fd.select(0,0);
         );greetings,
    Stijn

  • Aligning text in JLabel

    Hi all,
    I'm simply trying to align text in a JLabel.
    I've been trying to use HTML formatting inside the JLabel, e.g:
    label = new JLabel (" <HTML><CENTER><P>Line one</P> <P>Line two</P></CENTER> </HTML>" );but when I try to use the <CENTER> tag it seems to be ignored?
    Why?
    How do I get around this?
    Many thanks.

    You can use the following code segment:
    JLabel ab = new JLabel("GG" ,  SwingConstants.CENTER);Hope it helps.
    --DM                                                                                                                                                                                                                                                                       

  • Rendering location and text

    Hi
    I am rendering village locations using marker style and text style. I am using mapviewer11ea.Following is the xml format of this.
    <?xml version="1.0" standalone="yes"?>
    <styling_rules>
    <rule column="MSLINK">
    <features style="M.MAJOR_VILLAGES"> </features>
    <label column="NAME" style="T.MAJOR_VILLAGES"> 1 </label>
    </rule>
    </styling_rules>
    Now it renders village location and village name without any problem. For identifying feature [getting informtion of selected village] I need to hilite this location and place marker where the mouse has clicked. For placing marker i am using mapviewer.addPointFeature(...) and for hiliting the selected village I am using mapViewer.addJDBCTheme(...) methods. But in the both the cases the text[village name] will go off [ text will disappear].
    Please give me suggession to solve this
    Thnaks,
    Sujnan

    Hi,
    Thanks for replay.
    Here is my addPointFeature and addJDBCTheme methods
    Point2D pt = mapViewer.getUserPoint(intScreenX, intScreenY);
    mapViewer.addPointFeature(pt.getX(), pt.getY(), 81993, M.CYAN PIN, null, null, null);
    where intScreenX, intScreenY are screen coordinates.
    and addJDBCTheme method is
    mapViewer.addJDBCTheme("MapDataSource", "Hilite", strQuery, "geom", "81993", "M.HILITE_POINT" , null, null, false);
    Xml style definition for M.HILITE_POINT is
    <?xml version="1.0" standalone="yes"?>
    <svg height="1in" width="1in">
    <desc/>
    <g class="marker" style="stroke:#FFCC00;fill:#FFFF33;width:8;height:8;font-family:Dialog;font-size:12;font-fill:#FF0000;text-offset-x:10;text-offset-y:10">
    <circle cx="0" cy="0" r="8.0"/>
    </g>
    </svg>
    Thanks,
    Sujnan

  • Determining location of text within containing textframe

    After using findChangeTextOptions to locate a certain phrase, I receive a collection of Word objects. I would like to determine where in the containing text frame the Word object begins, so that I can move it down to the start of the next linked textFrame.
    The parent is a story, the next parent the document (myWord.parent.parent). How do I identify the containing textFrame? Then, how do I determine where (which character) in that textFrame the found Word begins?
    Thanks

    Solved:
    I now see "parentTextFrames" as a Word property in the documentation, but that is not needed.
    I set the Word .startParagraph property to StartParagraph.NEXT_PAGE - this effectively emulates a page break.

  • Can I set the default save location in text edit to dropbox?

    I'm happy with dropbox for now and want to set the default save folder to a dropbox folder but I cannot see how to do this.
    Can it be done? Or do I need a 3 party text editor?
    Thanks
    Tom

    You cannot. Because this option both takes much longer and reduces the quality of files, it has to be chosen specifically.
    In any case, when you save a PDF file from Word. this process does not involve Acrobat or Adobe software in any way.

  • Use of threads for refreshing text in JLabel

    Hi everyone! I've got a question about subj. Little intro: I have a form at which various swing components are placed (JButton, JLabel, JFrame itself, JComboBox etc..). I'm trying to make the following: after I push a button, the counter starts ticking and after a period of time (by default I set delay to 10 seconds) it stops and shows the result. But in fact the whole frame freezes and becomes editable after the specified period of time. As I understand it's because my application is the only one thread which is running and that's why currient thread freezes. The class at which I use these components does not extend or implement anything. Any suggestions how to correctly use Thread in my case? Thanks in advance!

    Better still for Swing see javax.swing.Timer.

Maybe you are looking for

  • Install CS6 failed for Ai, Ps, Fw, and Id. How can I get these installed?

    Purchased product through TechSoup for nonprofits. Installing on Dell Vostro, Core2Duo, 2GB RAM, 32-bit Windows 7 Professional Error messages that followed: Exit Code: 6 Please see specific errors and warnings below for troubleshooting. For example, 

  • Script Just doesn't want to run correctly.

    trying to run this script It applies registry values based on ($apps) Error = Missing Expression after unary operator '_'  On several lines Please assist $app = get-childitem c:\dome\$computername $computername = (get-wmiobject -class win32_compute

  • Removing Duplicated PLd

    Hi All, How can I remove duplicated PLD's? I tried to duplicate a PLD from Invoice to purchase order but now i want to remove that duplicated PLD. How can that be done? Thanks, Joseph

  • Photoshop Elements Organizer Catalogues

    I have several different catalogues within the Elements Organizer 9. I recently restored one catalog from a DVD backup. Now, none of my catalogs are availabe for viewing. There doesn't seem to be an option anywhere within the catalog menu for locatin

  • Cannot access media device through USB.

    Hello I have recently upgraded to windows 7 and have had to set up Windows XP mode to work with some of our older software. Everything has gone great on the set up and the program works fine however I have a device which I use to record geographical