Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: drag item or insert item into new Jlabel in JPanel

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation drag item or insert item into new Jlabel in JPanel

    Hello all programmers, i need help! please help me... how to input or insert text, icon, or table into new label in panel field? and how to drag them?
    here my code... i only can do drag new label from jtoolbar to jpanel... but i dunno how to drag and drop item when there already in field.. help please..
    public class DragLabel extends JFrame
        {
         JPanel tpan = new JPanel();
         JToolBar fpan = new JToolBar();
     
         Cursor dc = new Cursor(Cursor.DEFAULT_CURSOR);
         Cursor yd = DragSource.DefaultMoveDrop;
         Point mp;
     
        public DragLabel()
            {
             super(" From .......> To");
             addWindowListener(new WindowAdapter()
                 {
                 public void windowClosing(WindowEvent ev)
                     {
                     dispose();
                     System.exit(0);
                 }
             });
             setBounds(10,10,650,450);
             fpan.setPreferredSize(new Dimension(1,26));
             fpan.setBorder(BorderFactory.createRaisedBevelBorder());
             tpan.setLayout(null);
             tpan.setBackground(Color.white);
             getContentPane().add("North",fpan);
             getContentPane().add("Center",tpan);
             add_comp(new JLabel(" N1 "),Color.red);
             add_comp(new JLabel(" N2 "),Color.green);
             setVisible(true);
        }
     
     
        private void add_comp(JLabel l, Color c){
             fpan.addSeparator();
             l.setOpaque(true);
             l.setHorizontalAlignment(SwingConstants.CENTER);
             l.setForeground(Color.black);
             l.setBackground(c);
             fpan.add(l);
             mak_lis(l);
        }
        private void mak_lis(final JLabel l){
             l.addMouseListener(new MouseAdapter(){
                 public void mousePressed(MouseEvent m) {
                     setCursor(yd);
                     l.setBorder(new MatteBorder(1,1,1,1,Color.black));
     
     
                 }
                 public void mouseReleased(MouseEvent m){
                     l.setBorder(null);
                     setCursor(dc);
                     int x = m.getX()+l.getX();
                     int y = m.getY()+l.getY()-tpan.getY();
                     if (y > 0 && x > 0 && y < tpan.getHeight() && x < tpan.getWidth()){
                         tpan.add(new_lab(l,x,y));
                         tpan.repaint();
     
                     }
                 }
                 public void mouseMoved(MouseEvent m){
     
                 }
                 public void mouseDragged(MouseEvent m){
     
                 }
             });
        }
        private Component new_lab(JLabel co, int x, int y) //function to draw label in new posn
            {
             JLabel label = new JLabel(co.getText());
             label.setOpaque(true);
             label.setHorizontalAlignment(SwingConstants.CENTER);
             label.setForeground(co.getForeground());
             label.setBackground(co.getBackground());
             label.setBounds(x,y,co.getWidth(),co.getHeight());
             label.setText(" New Label ");
             label.setSize(100,30);
             return(label);
        }
        public static void main (String[] args)
            {
             new DragLabel();
        }
     
    }

    will ya?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: drag item or insert item into new Jlabel in JPanel

    how to drag and drop item when they're already in field
    Can you explain this please?
    What's an item and where is it?
    What is the 'field'?

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: drag item or insert item into new Jlabel in JPanel

    "Field" mean JPanel.when i drag the label into the panel..then how to put text or icon in that label. means, how i want to edit that label..and how to move that label when i already drag it on the Panel

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: drag item or insert item into new Jlabel in JPanel

    To recognize the mouse presses and movements, you need to use mouse listeners.
    When the mouse is pressed in one of the labels, you'll need to keep track of where the mouse is moved to and reposition the label to the new location.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: drag item or insert item into new Jlabel in JPanel

    thanks for your information...

    but here my code (i modify it)...

    public class DragLabel extends JFrame
        {
         JPanel tpan = new JPanel();
         JToolBar fpan = new JToolBar();
     
         Cursor dc = new Cursor(Cursor.DEFAULT_CURSOR);
         Cursor yd = DragSource.DefaultMoveDrop;
         Point mp;
     
        public DragLabel()
            {
             super(" From .......> To");
             addWindowListener(new WindowAdapter()
                 {
                 public void windowClosing(WindowEvent ev)
                     {
                     dispose();
                     System.exit(0);
                 }
             });
             setBounds(10,10,650,450);
             fpan.setPreferredSize(new Dimension(1,26));
             fpan.setBorder(BorderFactory.createRaisedBevelBorder());
             tpan.setLayout(null);
             tpan.setBackground(Color.white);
             getContentPane().add("North",fpan);
             getContentPane().add("Center",tpan);
             add_comp(new JLabel(" N1 "),Color.red);
             add_comp(new JLabel(" N2 "),Color.green);
             setVisible(true);
        }
     
     
        private void add_comp(JLabel l, Color c){
             fpan.addSeparator();
             l.setOpaque(true);
             l.setHorizontalAlignment(SwingConstants.CENTER);
             l.setForeground(Color.black);
             l.setBackground(c);
             fpan.add(l);
             mak_lis(l);
        }
        private void mak_lis(final JLabel l){
             l.addMouseListener(new MouseAdapter(){
                 public void mousePressed(MouseEvent m) {
                     setCursor(yd);
                     l.setBorder(new MatteBorder(1,1,1,1,Color.black));
     
     
                 }
                 public void mouseReleased(MouseEvent m){
                     l.setBorder(null);
                     setCursor(dc);
                     int x = m.getX()+l.getX();
                     int y = m.getY()+l.getY()-tpan.getY();
                     if (y > 0 && x > 0 && y < tpan.getHeight() && x < tpan.getWidth()){
                         tpan.add(new_lab(l,x,y));
                         tpan.repaint();
     
                     }
                 }
     
             });
        }
        private Component new_lab(JLabel co, int x, int y) //function to draw label in new posn
            {
             final JLabel label = new JLabel(co.getText());
             label.setOpaque(true);
             label.setHorizontalAlignment(SwingConstants.CENTER);
             label.setForeground(co.getForeground());
             label.setBackground(co.getBackground());
             label.setBounds(x,y,co.getWidth(),co.getHeight());
             label.setText(" New ChildNode ");
             label.addMouseListener(new MouseAdapter() {
             public void mousePressed(MouseEvent m){
             setCursor(yd);
             label.setBorder(new MatteBorder(1,1,1,1,Color.black));
     
                   if (m.getClickCount()==2){
                    final JTextField text=new JTextField();
                    text.setSize(100,30);
                    label.add(text);
                    text.setVisible(true);
     
                 ActionListener actionListener = new ActionListener() {
                         public void actionPerformed(ActionEvent actionEvent) {
                            text.setVisible(false);
                            label.setText(""+text.getText()+"\n");
                            label.setSize(text.getPreferredSize());
                            }
                        };
     
                        text.addActionListener(actionListener);
                   }
                 }               
     
                    public void mouseReleased(MouseEvent m){
                    label.setBorder(null);
                     setCursor(dc);
                     int x = m.getX()+label.getX();
                     int y = m.getY()+label.getY()-tpan.getY();
                     if (y > 0 && x > 0 && y < tpan.getHeight() && x < tpan.getWidth()){
                         tpan.add(new_lab(label,x,y));
                         tpan.repaint();
     
                     }
     
                     tpan.remove(label);
                 }
     
             });
     
             label.setSize(100,30);
             return(label);
        }
        public static void main (String[] args)
            {
             new DragLabel();
        }
     
    }

    the only thing i want is, how can i drag the label and edit text in label (my code now use 2 click) when i already drag it into the panel(tpan). the code i give above, the problem is when i drag it(label), i can't double click it to input the text. if i can put the text in label, then i want drag the label(already put text) to any position i want in panel(tpan), and the label no change anything(same be4 drag it)... i hope u know what i want... im sorry for my english...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: drag item or insert item into new Jlabel in JPanel

    Your code is not really drag and drop. It's only following mouse presses and movements with a change of cursor.
    Do a Google on DragSource or DragTarget to see code that uses DnD classes.

    To drag your label, add a Mouse Listener that detects when the mouse is pressed inside of a label and then follow the mouse's movement and set the label's location to correspond with the mouse's movement.

    Regarding your English. Have you tried using Google translate? Enter you language and get out English.

Similar Threads

  1. Funny business with JFrame, JPanel and JLabel
    By JeffC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2010, 01:26 PM
  2. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  3. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM
  4. [SOLVED] how to delete an item from a form
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 30th, 2009, 01:06 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM