0

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:

Initial JFrame

JButtons moved for all the JPanels, I need that when I left the JButton on GreyPanel ('Tablero') The sysout print: 'class battleship.Tablero'

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!!!

  • *"problems when I try to move a JButton between Panels"* Tip: Don't do that. Instead maintain a `BattleShipModel` that tells GUI how to render (e.g. the icon or text) of the buttons of each player. General tips: 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson May 04 '17 at 19:17
  • .. 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 5) To expand on point (1), see the `MineFieldModel` used in [this example](http://stackoverflow.com/a/43789823/418556). 6) Don't add the tag for the IDE, it is not the problem. – Andrew Thompson May 04 '17 at 19:17
  • 1
    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. **Minimal** please. – GhostCat May 04 '17 at 19:31
  • More minimal?? I need some help.. – Eduard Masip May 05 '17 at 19:37
  • *"More minimal??"* No. it needs to be more **Complete,** more **Verifiable** and an **example** we can run without changes. *Read the links offered.* And a tip: Add @GhostCat (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson May 06 '17 at 14:51
  • @AndrewThompson the @ ping is not required to notify the OP of the question (see [here](https://meta.stackexchange.com/questions/125208/when-exactly-do-i-get-comment-notifications) ). And even if the OP would not be notified; seriously: it is his question. And that initial posting simply had way too much code. If one puts down 1000 lines of code (just talking about "big magnitude) then it doesn't matter so much if it is verifiable. Because chances of people willing to debug more than 50 lines of everything are pretty close to 0 in my experience. – GhostCat May 06 '17 at 18:12
  • @GhostCat *"ping is not required to notify the OP of the question"* Yep. I'm aware of that, OTOH it **is** necessary when the OP is commenting to one of two or more *other* people who have already made comments. I give them that tip (without all the caveats, like "it's not needed when replying to another person when there has only been one person to make a comment"). Don't want to confuse them with unnecessary details. ;) *"If one puts down 1000 lines of code (just talking about "big magnitude) then it doesn't matter so much if it is verifiable."* True. I'd forgotten the original code .. – Andrew Thompson May 06 '17 at 18:42
  • @GhostCat .. and made my reply based on the short, uncompilable snippets the OP had posted, in conjunction with their question in comment.. *"More minimal??"*. The double '?' caused me to think they were referring to the code snippets & had not made an edit. But whichever way they went (original code or current code snippets), they were apparently not getting the concept of the MCVE. (Which makes me think they did not follow the links and read about it.) – Andrew Thompson May 06 '17 at 18:45

0 Answers0