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?