Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2007

Tip: Looking for answers? Try searching our database.

Drag lables/button on JForm/JPanel

Thread view: 
Chanchal - 19 Jul 2007 05:40 GMT
Hello All,

In a swing application, i have a requirement that when the program is
running i should place a number of jbuttons and lables onto a jpanel
or jframe and the user should be able to drag them around on the
jframe/jpanel. It is some thing like the form designer of netbeans.
Please advice on how such a feature can be implemented.

thanks in advance

Chanchal
Jeff Higgins - 19 Jul 2007 09:23 GMT
> Hello All,
>
[quoted text clipped - 5 lines]
>
> thanks in advance

<http://form.netbeans.org/source/browse/form/src/org/netbeans/modules/form/>
Jeff Higgins - 20 Jul 2007 18:26 GMT
> Hello All,
>
[quoted text clipped - 3 lines]
> jframe/jpanel. It is some thing like the form designer of netbeans.
> Please advice on how such a feature can be implemented.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;

public class LabelTest
{
 private JFrame frame;
 private GraphicPanel graphicPanel;
 private JPanel controlPanel;
 private JPanel buttonPanel;
 private JPanel linePanel;
 private JButton addButton;
 private JButton removeButton;
 private JButton addLine;
 private JButton removeLine;
 private JButton lineSelectionSource;
 private JButton lineSelectionDestination;
 private JTextField message;
 private boolean isAddingLine = true;
 private boolean isRemovingButton = false;
 private boolean inLineSelection = false;
 private ActionListener actionHandler;
 private MouseMotionHandler mouseMotionHandler;
 private MouseHandler mouseHandler;
 private String defaultMessage;

 LabelTest()
 {
   actionHandler = new ActionHandler();
   mouseMotionHandler = new MouseMotionHandler();
   mouseHandler = new MouseHandler();
   frame = new JFrame("LabelTest");
   graphicPanel = new GraphicPanel();
   controlPanel = new JPanel(new BorderLayout());
   buttonPanel = new JPanel();
   linePanel = new JPanel();
   addButton = new JButton("Add Button");
   removeButton = new JButton("Remove Button");
   addLine = new JButton("Add Line");
   removeLine = new JButton("Remove Line");
   lineSelectionSource = null;
   lineSelectionDestination = null;
   defaultMessage = "Add a couple Buttons.";
   message = new JTextField(defaultMessage);

   buttonPanel.add(addButton);
   buttonPanel.add(removeButton);
   linePanel.add(addLine);
   linePanel.add(removeLine);
   controlPanel.add(buttonPanel, BorderLayout.NORTH);
   controlPanel.add(linePanel, BorderLayout.SOUTH);

   frame.setSize(600, 480);
   frame.setLayout(new BorderLayout());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.add(message, BorderLayout.SOUTH);
   frame.add(graphicPanel, BorderLayout.CENTER);
   frame.add(controlPanel, BorderLayout.NORTH);

   graphicPanel.setLayout(null);
   graphicPanel.setBorder(BorderFactory.
       createLineBorder(Color.GRAY));
   controlPanel.setBorder(BorderFactory.
       createLoweredBevelBorder());
   // thanks to Michael Dunn for rescue here
   graphicPanel.setPreferredSize(new Dimension(600, 350));

   addButton.addActionListener(actionHandler);
   removeButton.addActionListener(actionHandler);
   addLine.addActionListener(actionHandler);
   removeLine.addActionListener(actionHandler);

   frame.setVisible(true);
 }

 class MouseMotionHandler
 extends MouseMotionAdapter
 {
   public void mouseDragged(MouseEvent e)
   {
     Component c = e.getComponent();
     c.setLocation(c.getX() + e.getX(),
         c.getY() + e.getY());
     graphicPanel.repaint();
   }
 }

 class MouseHandler extends MouseInputAdapter
 {
   public void mouseClicked(MouseEvent e)
   {
     if (e.getSource() instanceof
         JButton && isRemovingButton == true)
     {
       graphicPanel.removeButton(
           (JButton) e.getSource());
       message.setText(defaultMessage);
     }
     else if (e.getSource() instanceof JButton
         && inLineSelection == true
         && lineSelectionSource == null)
     {
       lineSelectionSource = (JButton) e.getSource();
       message.setText("Select Destination Button");
     }
     else if (e.getSource() instanceof
         JButton && inLineSelection == true
         && lineSelectionSource != null)
     {
       lineSelectionDestination = (JButton) e.getSource();
       if (isAddingLine == true)
       {
         graphicPanel.addLine();
       }
       else if (isAddingLine == false)
       {
         graphicPanel.removeLine();
       }
     }
   }
 }

 class ActionHandler implements ActionListener
 {

   public void actionPerformed(ActionEvent e)
   {
     if (e.getSource().equals(addButton))
     {
       graphicPanel.addButton();
     }
     else if (e.getSource().equals(removeButton))
     {
       isRemovingButton = true;
       message.setText(("Select Button to remove."));
     }
     else if (e.getSource().equals(addLine))
     {
       inLineSelection = true;
       isAddingLine = true;
       message.setText(("Select Source Button."));
     }
     else if (e.getSource().equals(removeLine))
     {
       inLineSelection = true;
       isAddingLine = false;
       message.setText(("Select Source Button."));
     }
   }
 }

 class Line
 {
   private JButton source;
   private JButton destination;

   public boolean isValid()
   {
     if (null != source && null != destination)
       return true;
     else
       return false;
   }

   JButton getSourceButton()
   { return source; }

   JButton getDestinationButton()
   { return destination; }

   Point getSourceLocation()
   {
     Point p = source.getLocation();
     Dimension d = source.getSize();
     p.x = d.width / 2 + p.x;
     p.y = d.height / 2 + p.y;
     return p;
   }

   Point getDestinationLocation()
   {
     Point p = destination.getLocation();
     Dimension d = destination.getSize();
     p.x = d.width / 2 + p.x;
     p.y = d.height / 2 + p.y;
     return p;
   }

   public void setSource(JButton b)
   { source = b; }

   public void setDestination(JButton b)
   { destination = b; }
 }

 @SuppressWarnings("serial")
 class GraphicPanel extends JPanel
 {
   Set<Line> lineSet = new HashSet<Line>();
   int buttonCount = 0;

   protected void paintComponent(Graphics g)
   {
     super.paintComponent(g);
     Graphics2D g2 = (Graphics2D) g;
     for (Line l : lineSet)
     {
       g2.drawLine(l.getSourceLocation().x,
           l.getSourceLocation().y, l
           .getDestinationLocation().x,
           l.getDestinationLocation().y);
     }
   }

   private void setDefaultMessage()
   {
     if(buttonCount == 0)
       defaultMessage = "Add a couple Buttons.";
     else if(buttonCount == 1)
       defaultMessage = "Move Button and add another.";
     else defaultMessage =
       "Add, Remove or Move Button, or Add, Remove Line.";
   }

   public void addButton()
   {
     JButton b = new JButton("     ");
     b.addMouseListener(mouseHandler);
     b.addMouseMotionListener(mouseMotionHandler);
     Point p = new Point(getBounds()
         .width / 2, getBounds().height / 2);
     b.setBounds(p.x, p.y, b.getPreferredSize().width,
         b.getPreferredSize().height);
     add(b);
     buttonCount++;
     setDefaultMessage();
     message.setText(defaultMessage);
     validate();
     repaint();
   }

   public void removeButton(JButton b)
   {
     removeLines(b);
     remove(b);
     buttonCount--;
     setDefaultMessage();
     message.setText(defaultMessage);
     isRemovingButton = false;
     validate();
     repaint();
   }

   public void addLine()
   {
     Line l = new Line();
     if (lineSelectionSource != null &&
         lineSelectionDestination != null)
     {
       l.setSource(lineSelectionSource);
       l.setDestination(lineSelectionDestination);
       lineSet.add(l);
     }
     lineSelectionSource = null;
     lineSelectionDestination = null;
     message.setText(defaultMessage);
     repaint();
   }

   public void removeLine()
   {
     Line l = new Line();
     Iterator it = lineSet.iterator();
     if (lineSelectionSource != null &&
         lineSelectionDestination != null)
     {
       while (it.hasNext())
       {
         l = (Line) it.next();
         if (
           (l.getSourceButton()
             .equals(lineSelectionSource)
           && l.getDestinationButton()
             .equals(lineSelectionDestination))
           ||
           (l.getSourceButton()
             .equals(lineSelectionDestination)
           && l.getDestinationButton()
             .equals(lineSelectionSource)))
         {
           lineSet.remove(l);
           break;
         }
       }
       lineSelectionSource = null;
       lineSelectionDestination = null;
       message.setText(defaultMessage);
       repaint();
     }
   }

   public void removeLines(JButton b)
   {
     Set<Line> removalSet = new HashSet<Line>();
     Iterator it = lineSet.iterator();
     Line l;
     while (it.hasNext())
     {
       l = (Line) it.next();
       if (l.source.equals(b) || l.destination.equals(b))
         removalSet.add(l);
     }
     for(Line r : removalSet)
     { lineSet.remove(r); }
     repaint();
   }
 }

 public static void main(String[] args)
 {
   @SuppressWarnings("unused")
   LabelTest test = new LabelTest();
 }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.