Results 1 to 4 of 4
  1. #1
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default Overlapping Components using Netbeans GUI Interface

    Can someone teach me how to do this? Whenever I wish to drag and drop another component above another. The other component will shift away. What should I do. Eg I want to put a smaller label over bigger label .:confused:

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You cannot put a label on another label. Best option you have is use a container, JPanel may be and on that add all your components.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Eranga View Post
    You cannot put a label on another label.
    You can't?
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    public class LabelOnLabel
    {
      private static final int LABEL_WIDTH = 100;
      private static String[] labelStrings = {"Label 1", "Label 2"};
      private static Color[] BACKGROUND_COLORS = {Color.white, Color.lightGray};
      private static Color[] BORDER_COLORS = {Color.blue, Color.red};
    
      private static void createAndShowGUI()
      {
        JPanel mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(400, 400));
        mainPanel.setLayout(null);
        
        for (int i = 0; i < labelStrings.length; i++)
        {
          JLabel label = new JLabel(labelStrings[i], SwingConstants.CENTER);
          label.setBounds(i*60 + 20, i*60 + 20, LABEL_WIDTH, LABEL_WIDTH);
          label.setBorder(BorderFactory.createLineBorder(BORDER_COLORS[i], 4));
          label.setOpaque(true);
          label.setBackground(BACKGROUND_COLORS[i]);
          MyMouseAdapter labelMouseAdapter = new MyMouseAdapter(label);
          label.addMouseListener(labelMouseAdapter);
          label.addMouseMotionListener(labelMouseAdapter);
          mainPanel.add(label);
        }
        
        JFrame frame = new JFrame("LabelOnLabel Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
      
      private static class MyMouseAdapter extends MouseAdapter
      {
        Point deltaPoint = null;
        JLabel label;
        
        public MyMouseAdapter(JLabel label)
        {
          this.label = label;
        }
        
        @Override
        public void mousePressed(MouseEvent e)
        {
          Point labelPoint = label.getLocation();
          Point pointOnScreen = e.getLocationOnScreen();
          deltaPoint = new Point(pointOnScreen.x - labelPoint.x, pointOnScreen.y - labelPoint.y);
        }
        
        @Override
        public void mouseReleased(MouseEvent e)
        {
          deltaPoint = null;
        }
        
        @Override
        public void mouseDragged(MouseEvent e)
        {
          if (deltaPoint != null)
          {
            Point pointOnScreen = e.getLocationOnScreen();
            Point p = new Point(pointOnScreen.x - deltaPoint.x, pointOnScreen.y - deltaPoint.y);
            label.setLocation(p);
          }
        }
      }
    
      public static void main(String[] args)
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
      }
    }
    Last edited by Fubarable; 03-05-2009 at 12:11 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Thanks for the correct me lol. I've try it once ago, and unable to do it. May be lost with the layouts. I'll check it again and let you know what happen there. Thanks a lot again, and it's my fault. I must test the each code before commenting again. :)

Similar Threads

  1. overlapping forms
    By keerthi_y19 in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 10:30 PM
  2. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 04:04 AM

Posting Permissions

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