Putting a label in a specific place

I have a small ImageIcon that I'd like to display in a specific location in a JPanel.
I discovered you can't paint an ImageIcon directly onto a JPanel (or can you???). So I added the ImageIcon to a JLabel, then added that to my JPanel.
OK, that worked. Trouble is, I want to be able to move that label around on the panel -- to be able to set and reset its location based on user input. But I can't find a way to do that. If I try to at least set the location of the JLabel in the upper left-hand corner of the JPanel, I can't even do that. I tried setLocation(), but it doesn't seem to work. I'm guessing that when I call setLocation(), the method is only affecting the placement of the graphic in the JLabel itself, but not the placement of the JLabel in the JPanel.
So do any of you out there have suggestions for how I could do this? I'm wondering if I shouldn't just create a large JLabel that covers the whole panel, and then use setLocation() to move the ImageIcon around on the label, rather than try to move the label around on the panel. Does that sound like a reasonable solution?
The original suggestion was to custom make a paint() method to use on a Canvas, but I really prefer using the Swing JComponents if I can.
Thanks for any help!

Here is an example of code that moves a JLabel within a JFrame.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class LabelMover extends JPanel {
    JLabel myLabel;
    /** Creates a new instance of LabelMover */
    public LabelMover() {
     addMouseMotionListener(new MyMouseMotionAdapter2());
     myLabel = new JLabel("this is an example");
     myLabel.setOpaque(true);
     myLabel.setBackground(Color.YELLOW);
     myLabel.setBorder(BorderFactory.createLineBorder(Color.black));
     add(myLabel);
    public class MyMouseMotionAdapter2 extends MouseMotionAdapter {
     public void mouseMoved(MouseEvent e) {
         myLabel.setLocation(e.getX(), e.getY());
    public static void makeGUI() {
     JFrame myFrame = new JFrame();
     myFrame.setBounds(0, 0, 300, 300);
     myFrame.getContentPane().setLayout(null);
     LabelMover myContent = new LabelMover();
     myContent.setOpaque(true);
     myContent.setBackground(Color.WHITE);
     myFrame.setContentPane(myContent);
     myFrame.setVisible(true);
     * @param args the command line arguments
    public static void main(String[] args) {
     javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
          makeGUI();
}

Similar Messages

  • Putting a Jlabel in a specific place in JPanel

    Hi,
    I have created a JApplet where I fill it up with some JPanels. Inside a JPanel i want to put some Jlabels on a specific place. However, it doesn't work.
    Whenever i try to put a setLayout(null), the application doesn't show my modifications. If don't use this command, the modification are loaded.
    I don't know what happes with this layout. Is it a problem of layout or a problem of JPanel references?
              canvas = new AVLTreeAnimation();
              //canvas.setLayout(null);
              JLabel label = new JLabel("trestadf");
              label.setLocation(10, 10);          
              canvas.add(label);
              canvas.setBorder(new TitledBorder(null,"AVL-Tree Animation",
                        TitledBorder.LEFT, TitledBorder.TOP));
              canvas.repaint();          
              return canvas;thanks for your help

    man, this same problem's come up a lot this week.
    If you set the layout to null, you have to set the size and the location, not just the location. All components have a size of 0 x 0 initially.

  • Is there a way to put the cursor at a specific location in a RichTextBox?

    Is there a way to put the cursor at a specific location in a RichTextBox?
    The following code places the cursor at the start of a RichTextBox:
        RichTextBox RTbx
        RTbx.Focus();
        TextPointer tp = RTbx.CaretPosition;
        tp = tp.DocumentStart;
        RTbx.CaretPosition = tp;
    Not sure how to place it at a specific location other than start and end.
    The RichTextBox has contents.  At this point in the code, I do know the specific "word" in the contents, as well as the position (number of chars) where that word is located.  I just need to programmatically place the cursor at that location.
    bhs67

    Oh sorry, I see I missed something out my original reply.
    I have markup:
    <Grid>
    <RichTextBox Name="rtb">
    <FlowDocument>
    <Paragraph >This is the first paragraph</Paragraph>
    <Paragraph >TSecond paragraph is here</Paragraph>
    </FlowDocument>
    </RichTextBox>
    </Grid>
    Code - this is the window contentrendered event
    private void Window_ContentRendered(object sender, EventArgs e)
    rtb.Focus();
    // TextPointer moveTo = rtb.CaretPosition.GetNextInsertionPosition(LogicalDirection.Forward);
    TextPointer moveTo = rtb.CaretPosition.GetPositionAtOffset(9, LogicalDirection.Forward);
    if (moveTo != null)
    rtb.CaretPosition = moveTo;
    I see the cursor between the t and he of the second "the".
    Note that there are numerous other options such as the commented out line.
    Hope that helps.
    Recent Technet articles: Property List Editing;
    Dynamic XAML

  • I am wanting to add picture and designs to my form how do I place it in a specific place on my form?

    I am wanting to add picture and designs to my form how do I place it in a specific place on my form?

    Currently we only support one field/item on a line. We are working on adding a feature that allows you to put multiple fields/items on a single line. We hope to have that out in a few months.
    Randy

  • Problem of putting the label in the middle.

    I am having a problem of putting the label in the middle.
    below are my codes.
    import java.awt.*;
    import java.applet.Applet;
    public class glay1 extends Applet{
    public void init(){
    Button button1;
    TextField textfield1;
    button1 = new Button("Set fuel Price(p)");
    textfield1 = new TextField(" ");
    Label label1, label2, label3, label4;
    label1 = new Label("pump1", Label.CENTER);
    label1.setBackground(Color.white);
    label2 = new Label("pump2", Label.CENTER);
    label2.setBackground(Color.white);
    label3 = new Label("pump3", Label.CENTER);
    label3.setBackground(Color.lightGray);
    label4 = new Label("pump4", Label.CENTER);
    label4.setBackground(Color.lightGray);
    Panel pc = new Panel();
    Panel ps = new Panel();
    setLayout(new BorderLayout());
    setBackground(Color.black);
    pc.setLayout(new GridLayout(2,2,220,50));
    pc.add(label1);
    pc.add(label2);
    pc.add(label3);
    pc.add(label4);
    ps.add(button1);
    ps.add(textfield1);
    add("South", ps);
    add("Center", pc);
    My problem is the label all are put on each corner.
    How to put the label to the middle?
    but I don't want the label to join one another,
    just put them more to the middle only.

    btw, I haven't actually used GridLayout myself but wouldn't it work for you to use the methods setHgap(int hgap) and setVgap(int vgap)?
    With GridBagLayout you can add components of varying sizes and you can easily position them on the grid. Look at this example, adding components to a JPanel called resultsPanel:
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,0,0,1,1,5,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,0,1,1,1,1,1);
              FrameTools.addComponent(resultsPanel,resultFileLabel,c,gridbag,1,1,1,1,1,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,2,1,1,1,1,1);
              FrameTools.addComponent(resultsPanel,loadResultFileButton,c,gridbag,3,1,0,0,1,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,4,1,1,1,1,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,0,2,1,1,5,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,0,3,1,1,1,1);
              FrameTools.addComponent(resultsPanel,linkerFileLabel,c,gridbag,1,3,1,1,1,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,2,3,1,1,1,1);
              FrameTools.addComponent(resultsPanel,linkerButton,c,gridbag,3,3,0,0,1,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,4,3,1,1,1,1);
              FrameTools.addComponent(resultsPanel,new JLabel(" "),c,gridbag,0,4,1,1,5,1);
                   The addComponent(...) method looks like this:
         public static void addComponent(Container target, JComponent comp, GridBagConstraints c, GridBagLayout g, int x, int y, int wx, int wy, int w, int h)
                  c.fill = GridBagConstraints.BOTH;
                  c.gridx = x;
                  c.gridy = y;
                  c.weightx = wx;
                  c.weighty = wy;
                  c.gridwidth = w;
                  c.gridheight = h;
                  g.setConstraints(comp, c);
                  target.add(comp);
           }     x and y specifies the position of the component and w and h the size of it. But really, read the tutorials!
    /P

  • TS3297 I have an iPhone and recently bought an iPod 4 touch what l am using the same apple ID for. The iPod won't let me download songs from iTunes it is asking me security questions which I don't think I have put answers to in the first place.

    I have an iPhone and recently bought an iPod 4 touch what l am using the same apple ID for. The iPod won't let me download songs from iTunes it is asking me security questions which I don't think I have put answers to in the first place, because of this it won't let me buy any songs on iTunes

    It's probably asking you to verify the security code found on the back of your credit card.
    Regards.

  • How can I put a time delay between specific events in a while loop?

    How can I put a time delay between specific events within the same while loop? I'm already using the "wait" command to control the overall loop iteration speed. But I want to time the individual events as well.

    Hi Jesse,
    You can use a flat sequence. In each box you can put your individual events and attached wait.
    Don't forget to reduce your total loop time of the time you added in the individual sequences.
    Doc-Doc
    Doc-Doc
    http://www.machinevision.ch
    http://visionindustrielle.ch
    Please take time to rate this answer

  • How do I link to a specific place on a "different" page?

    I understand how a Named Anchor works on a given page. How do I click a link on one page, and go to a specific place on another page? Can that be done?
    Thanks!

    Same page anchor:
    <a href ="#anchoronthispage">
    Different page anchor:
    <a href="anotherpage.html#anotheranchor">

  • External hyperlink to specific place in a PDF file?

    Is it possible to have an external hyperlink on a webpage to a specific place in a PDF file? If so, how is it done? I would be creating the PDF file in InDesign. Thanks

    Great...many thanks
    Mark

  • Hi thee.  As logical as I can be.  I export a file back into my folder as a PSD.  I then export that file to PS.  Work on it, save and return it to LR.  The file goes to the bottom of the folder from where I have to put it back in the original place, taki

    Hi thee.  As logical as I can be.  I export a file back into my folder as a PSD.  I then export that file to PS.  Work on it, save and return it to LR.  The file goes to the bottom of the folder from where I have to put it back in the original place, taking time and driving me slightly mad.  Why does the file not go back to where it came from please?  Thanks  Tim

    Umm, but this folder is an amalgam of two cameras and five cards, and I want them in capture order!  Thanks for trying though Jim.
    Cheers
    Tim

  • I want to link to a specific place within a document from Captivate4

    Hi all,
    I want to link to a specific place within a document from Captivate4.
    I have no trouble linking to the beginning of a document; however I want to be able to link to a specific chapter/area of a document I am creating. My plan is that as people progress through the e-learning they can link back to the electronic manual I am creating from where they left off.
    I have a workaround of creating separate documents and linking to them, but this is time consuming and messy.
    Does anyone have any ideas of what I can do; I have looked around the forums and, unless I am blind, can't find anything like I am looking for.
    Cheers
    thedarf

    Hi there
    What type of document are you trying to link to? PDF? Word Doc?
    Cheers... Rick
    Helpful and Handy Links
    Captivate Wish Form/Bug Reporting Form
    Adobe Certified Captivate Training
    SorcerStone Blog
    Captivate eBooks

  • How do I put a label in a null field in the result set of a group-by query

    I run the query:
    select section, count(*) from nparcel group by section
    and get the result
    section1 100
    section2 200
    section3 300
    400
    I would like to modify the query to put a label, perhaps 'BLANK', in the last returned row. Is that possible and if so how.
    Thanks in advance for helping.

    Hi,
    In SQL*Plus you have some additional options, that apply to all queries until you override them or end the session.
    COLUMN section NULL BLANKapplies only to the column called "section".
    SET NULL BLANKApplies to all columns.

  • Linking to specific place in newsletter

    Hello:
    I am trying to find out how to link from an email digest to a specific place in our newsletter. The newsletter page will have several stories on it, which can be read by scrolling down. I would like to email a digest with brief synopsis of the stories, which will link back to the exact story on the newsletter page, not just the newsletter page itself.
    Thanks for any help you can offer.
    Chip Mitchell

    You could use anchors, then set the links to those specific anchors.
    Unless I'm missing something.

  • How can I put a selected picture in multiple places in slideshow

    As the heading states: how can I put a selected picture in multiple places in slideshow
    There is a slideshow with 50 pics. I want to use one of these pics in several places, in the front, middle, middle end and then the end.
    How can I place this pic in multiple locations in the slideshow
    redundently stated I know

    Dah!!!
    Thanks so much,
    I was unaware of that feature, it works.

  • I have bought a new iPad 2 charger and it works absolutely fine but then I wanted to upload some pictures on the computer so I put the wire in the right places but it doesn't work so I tried my iPhone and it worked fine?

    I have bought a new iPad 2 charger and it works absolutely fine but then I wanted to upload some pictures on the computer so I put the wire in the right places but it doesn't work so I tried my iPhone and it worked fine?

    Plz answer as soon as possible

Maybe you are looking for

  • MacBook Pro mid-2012 : Slow. Sudden kernel panics. Random Restarts.

    My mid 2012 MacBook Pro 15" (Running Mavericks) has recently been getting slower, performance-wise. Apps such as Safari, MS Office and OSX Mail are increasingly frequently - not always -  hanging and performing sluggishly. The sudden kernel panics ar

  • Upgrade 8.6 to OSX

    Can a G3 with OS8.6 be upgraded to OSX??

  • BI publisher Addin for Excel not showing up

    Hi all, Recently i installed the latest version of bi publisher desktop into my computer. First i was not able to see the BI publisher tab in both the word and excel (Office 2013). but after running the "TBMenusSetup" i am able to see the tab in word

  • SAProuter Service Is Not Starting

    Hi, I have installed saprouter on my end.service is created sucessfully. when i am start saprouter service its given me error SAProuter Service Is Not Starting locally error 1067. I am tried to start this service windows administrator as well sncadm.

  • Ethernet problem on Sawtooth

    Hello, I am running Mac OS X 10.5.8 on my second PM G4 sawtooth, and only recently has the ethernet adapter not been working properly. I was connecting it to a network drive that I used for time machine, but all of a sudden it stopped working. It cla