Results 1 to 4 of 4
  1. #1
    xna
    xna is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default control setLocation setBounds

    I am trying to experiment and move the label with the mouse drag. But as soon as I resize the Frame, my label control goes back to its original location.
    --
    I am new to java and any solution will be highly appreciated.

    Regards,
    xna





    package graphics;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.util.*;
    import java.text.SimpleDateFormat;

    public class TheGui extends JPanel implements ActionListener {
    static JFrame frame;
    JLabel result;
    String currentPattern;
    NodeLabel nd;
    public TheGui(){

    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

    nd = new NodeLabel("Sample Label");

    add(nd);

    reformat();
    }
    public void reformat() {
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String newSelection = (String)cb.getSelectedItem();
    currentPattern = newSelection;
    reformat();
    }
    }

    class NodeLabel extends JLabel
    {
    int c1=0;int c2=0;
    boolean b1=false;

    public NodeLabel(final String text)
    {
    setText(text);
    addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
    public void mouseDragged(java.awt.event.MouseEvent evt) {
    jLabelMouseDragged(evt);
    }
    public void mouseReleased(java.awt.event.MouseEvent evt) {
    jLabelMouseReleased(evt);
    }
    });
    }
    //mouseReleased
    public void jLabelMouseReleased(MouseEvent e){
    b1=false;
    }

    private void jLabelMouseDragged(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    Component C=evt.getComponent();
    if(b1==false){
    b1=true;
    Point p=new Point(evt.getPoint());
    c1=p.x;
    c2=p.y;
    }

    Point z=new Point(evt.getPoint());
    Point q=new Point(C.getLocation());

    C.setBounds(q.x+(z.x-c1),q.y+(z.y-c2),C.getSize().
    width,C.getSize().height);

    //_lastMousePosition = currentPoint;
    //_body.mouseDragged(e);
    //this.setLocation(evt.getLocationOnScreen());

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    for a recent example, look here: Swing - Newbie question - how to set location for label

    But as soon as I resize the Frame, my label control goes back to its original location.
    Your JLabel is just trying to follow the rules of the BoxLayout that the JPanel is set to. If you want things to remain put in a JPanel, then set the layout to null. Just don't forget that if you do so, you are responsible for setting the size and position of the component. For e.g.,
    Java Code:
       public TheGui() {
          //setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
          setLayout(null);
          nd = new NodeLabel("Sample Label");
          nd.setLocation(0, 0);
          nd.setSize(nd.getPreferredSize());
          add(nd);
          reformat();
       }
    p.s., welcome to the forum! Also, please read my signature about use of code tags here.
    Last edited by Fubarable; 11-11-2009 at 05:27 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Your TheGui instance has a box layout, and this layout manager is the thing which positions the various elements of the gui.

    If you want the elements to have arbitrary size and location you could use a absolute positioning.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Positioning using setBounds
    By thayalan in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2009, 02:59 AM
  2. Using setBounds (x.y,width,height) constructor
    By hitmen in forum AWT / Swing
    Replies: 3
    Last Post: 03-06-2009, 01:12 PM
  3. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 08:35 AM
  4. Problem here guys: Set location or setBounds
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-03-2008, 08:19 PM
  5. control app width based on certain control
    By thebillybobjr in forum SWT / JFace
    Replies: 0
    Last Post: 05-15-2008, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •