-1

I have created a frame and two panels.

Jpanel pane11=new JPanel();
Jpanel pane12=new JPanel();

Added the panel2 to the panel1

panel1.add(panel2);
getContentPane().add(panel1);

Now I set the panel2 background white and it is working correctly

I have created 4 JTextPanes in the panel2 and made it draggable.

*Now what my problem is: If I drag a text pane from (0,0) to (10,10) and I save it the text pane has to reappear at (10,10) when I close and re open the frame. So how to get the location of jtextpanes in flowlayout *

I have created save button to save the changes in panel and I am using flow layout for panel2.

I am using XML file for saving the locations.

  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/) of what you have so far. 2) You've described a problem, but have so far not asked a question (let alone a specific, answerable question). What *is* your question? – Andrew Thompson Jun 24 '13 at 04:58
  • 1
    See [this answer](http://stackoverflow.com/a/7778332/418556) for saving the properties of a `JFrame`. – Andrew Thompson Jun 24 '13 at 05:06

3 Answers3

2

By design, FlowLayout will restore the panel to its calculated location when the enclosing Window is resized. As an alternative, consider JInternalFrame, which supports dragging in a JDesktopPane directly. Use the Preferences API to persist each internal frame's location.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

try this, save the finalx and finaly variables.

@Override
public void mousePressed(MouseEvent e) {
    if (panel.contains(e.getPoint())) {
        dX = e.getLocationOnScreen().x - panel.getX();
        dY = e.getLocationOnScreen().y - panel.getY();
        panel.setDraggable(true);
    }
}

@Override
public void mouseDragged(MouseEvent e) {
    if (panel.isDraggable()) {
        finalx=e.getLocationOnScreen().x - dX;//save finalx and finaly
        finaly=e.getLocationOnScreen().y - dY;
        dX = e.getLocationOnScreen().x - panel.getX();
        dY = e.getLocationOnScreen().y - panel.getY();
    }
}
drarkayl
  • 1,017
  • 7
  • 17
-1

You cannot save location while using flow layout.

If you would like to customize the whole layout, you could use

container.setLayout(null)

and give each component the location and size explicitly

setLocation(x,y)
setSize(w,h)
Jeremy Zhou
  • 49
  • 1
  • 4
  • Don't suggest `null` layouts until ***all*** other possibilities have been explored. In this case, other possibilities are.. the OP actually want to store the *index* of the items in the `FlowLayout`, the layout might best be set to a custom `DragAndDropLayout`, the items are better displayed in a `JList`.. There is a distinct lack of information needed to guide the OP to the best answer, so I feel *any* answer at this stage is largely speculative. – Andrew Thompson Jun 24 '13 at 05:12