Draggable JButton and problem with scroll pane
- From: Chanchal <chanchal.jacob@xxxxxxxxx>
- Date: Fri, 30 Nov 2007 00:52:23 -0800 (PST)
Hello All,
The following code adds a JButton to a JPanel in a JScollPane, when a
JButton is clicked. A MouseMotionAdapted is regitered to the added
JButton to make it draggable. The problem i'm facing is that the
scrollbars of the JSrollPane is not getting activated when the added
button is dragged outside the viewable area. The result i want is
that, if the added JButton is dragged out side the viewable area, is
should be posible to bring it back to the viewable area by scrolling
the scollbars. Kindly advice how can i create this result.
<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DragTest extends javax.swing.JFrame {
private JPanel dragArea;
private JButton jButton1;
private JScrollPane jScrollPane1;
public DragTest() {
GridBagConstraints gridBagConstraints;
jScrollPane1 = new JScrollPane();
dragArea = new JPanel();
jButton1 = new JButton();
getContentPane().setLayout(new GridBagLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setAutoscrolls(true);
GroupLayout dragAreaLayout = new GroupLayout(dragArea);
dragArea.setLayout(dragAreaLayout);
dragAreaLayout.setHorizontalGroup(
dragAreaLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0,
376, Short.MAX_VALUE)
);
dragAreaLayout.setVerticalGroup(
dragAreaLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0,
217, Short.MAX_VALUE)
);
jScrollPane1.setViewportView(dragArea);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
getContentPane().add(jScrollPane1, gridBagConstraints);
jButton1.setText("Click Me");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = GridBagConstraints.SOUTH;
getContentPane().add(jButton1, gridBagConstraints);
pack();
}
private void jButton1ActionPerformed(ActionEvent evt)
{
JButton jButton = new JButton("Drag Me");
jButton.setBounds(50,50,100,50);
jButton.addMouseMotionListener(new DragHandler());
dragArea.add(jButton);
dragArea.repaint();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new DragTest().setVisible(true);
}
});
}
/* Inner class to handle dragging of items on the workspace */
class DragHandler extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e) {
Component c = e.getComponent();
c.setLocation(c.getX() + e.getX()-c.getWidth()/2, c.getY()
+ e.getY()-c.getHeight()/2);
}
}
}
</code>
Thanks in advance
Chanchal.
.
- Follow-Ups:
- Re: Draggable JButton and problem with scroll pane
- From: Larry A Barowski
- Re: Draggable JButton and problem with scroll pane
- Prev by Date: exception error
- Next by Date: Re: exception error
- Previous by thread: exception error
- Next by thread: Re: Draggable JButton and problem with scroll pane
- Index(es):