Note that there are some explanatory texts on larger screens.

plurals
  1. POgetComponentAt() not finding a component?
    primarykey
    data
    text
    <p>I am using a <code>JFrame</code> to set up a game of Solitaire, using Cards that extend <code>JLabel</code> so that they can be dragged around the screen. However, one of my requirements is that I be able to double click a card and it snaps up to the 4 stacks of aces. This is not a problem. what is causing problems is that I set this up in an array, so that the aces would go to the spot that corresponds with the suit of the ace, then I have the spots rearrange if a card is dragged up there. If it is double clicked, the aces must go to the first spot (from the left) then the second, and so on. I had planned to use <code>getComponentAt()</code> to find out what was at the first spot and if I could place the ace there, if not then i move on to the second and so on. For some reason though, even if I hard code in the parameters into <code>getComponentAt()</code> to spots I know have a component, it is still returning null.</p> <p>this is the relevant portion of my code: </p> <pre><code>Container contentPane = getContentPane(); contentPane.setLayout(null); ... for(int i = 0; i &lt; 4; i++) { aces[i] = new Card(); aces[i].setBounds((i * 75) + 475, 25, 75, 100); contentPane.add(aces[i]); aces[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); aces[i].setSuit(i + 1); } System.out.println(contentPane.getComponentAt(475, 25)); </code></pre> <p>This is returning <code>null</code> every time, no matter where in the component i put the coordinates. Any explanations?</p> <p>*Updated SSCCE: This is the main class, solitaire:</p> <pre><code>import java.util.*; import javax.swing.*; import java.awt.*; public class Solitaire extends JFrame { private Container contentPane; private Card aces[]; public static void main(String args[]) { Solitaire frame = new Solitaire(); frame.setVisible(true); } public Solitaire() { super(); contentPane = getContentPane(); contentPane.setLayout(null); contentPane.setBackground(Color.GREEN.darker().darker()); setTitle("Solitaire"); setSize(800,800); setResizable(false); setLocation(250, 50); setDefaultCloseOperation(EXIT_ON_CLOSE); play(); } public void play() { contentPane.removeAll(); aces = new Card[4]; for(int i = 0; i &lt; 4; i++) { aces[i] = new Card(0,i + 1); aces[i].setBounds((i * 75) + 475, 25, 75, 100); contentPane.add(aces[i]); aces[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); } for(int i = 0; i &lt; contentPane.getComponents().length; i++) System.out.println(contentPane.getComponents()[i]); System.out.println(contentPane.getComponentAt(475, 25)); repaint(); } } </code></pre> <p>and this is Class Card:</p> <pre><code>import javax.swing.*; public class Card extends JLabel { private int numb; private int value; private int suit; public Card(int n, int s) { super(); numb = n; suit = s; } } </code></pre> <p>This ran for me, comment if you need more to solve the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload