I reconsider my ask, for a easy interpretation. My code don't throw any error or crashes...
My question is how I can obtain a container(JPanel) where I left the button on somewhere? I have this code for a get the parent.
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
if(e.getSource() instanceof JButton){
JButton btnBarco = (JButton) e.getSource();
-----------> System.out.println(btnBarco.getParent().getParent().getClass()); <---------------
-----------> btnBarco.getParent().getParent().add(btnBarco); <---------------
btnBarco.setLocation(e.getX() - draggedAtX + btnBarco.getLocation().x,
e.getY() - draggedAtY + btnBarco.getLocation().y);
btnBarco.getParent().getParent().repaint();
}
}
}
When I move the JButton in the Left Panel on the JPanel that have the grid, the sysout only returns this:
class battleship.PanelBarcos
class javax.swing.JPanel
class battleship.TableroPrincipal
class javax.swing.JLayeredPane
class javax.swing.JRootPane
class battleship.Inicio
class javax.swing.JLayeredPane
class javax.swing.JRootPane
And after only appears ALL TIME:
class battleship.Inicio
class javax.swing.JLayeredPane
class javax.swing.JRootPane
If I only execute ONE TIME the method getParent(), is the same but instead class battleship.Inicio print this: class battleship.TableroPrincipal.
I need get the JPanel added on the 'TableroPrincipal', on the image are the JPanel that have a grid, and are called 'Tablero'.
I put a ScreenShoots to see the GUI structure:
Anybody have idea how I can solve this problem? My goal is put this JButton in any position of the GridBagLayout, but for do it i need to obtain the GridBagLayout of 'Tablero', but I can't get the 'Tablero' never!!
I don't know if I'm not focusing well the problem, but I stalled...
Thanks for Help!!!