java - getComponentAt() not finding a component? -


i using jframe set game of solitaire, using cards extend jlabel can dragged around screen. however, 1 of requirements able double click card , snaps 4 stacks of aces. not problem. causing problems set in array, aces go spot corresponds suit of ace, have spots rearrange if card dragged there. if double clicked, aces must go first spot (from left) second, , on. had planned use getcomponentat() find out @ first spot , if place ace there, if not move on second , on. reason though, if hard code in parameters getcomponentat() spots know have component, still returning null.

this relevant portion of code:

container contentpane = getcontentpane(); contentpane.setlayout(null); ... for(int = 0; < 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)); 

this returning null every time, no matter in component put coordinates. explanations?

*updated sscce: main class, solitaire:

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 = 0; < 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 = 0; < contentpane.getcomponents().length; i++)         system.out.println(contentpane.getcomponents()[i]);          system.out.println(contentpane.getcomponentat(475, 25));        repaint();     } } 

and class card:

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;     } } 

this ran me, comment if need more solve problem.

there lots of problems using getcomponentat

the first is, parent container must realized , sized. if not, return null. method capable of returning container if no children exist @ location.

i child component#contains test see if given point feel within child components directly, wants point translated child's coordinate space...

so instead, went straight getbounds...

import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.container; import java.awt.eventqueue; import java.awt.point; import javax.swing.borderfactory; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class testcomponentlocation {      public static void main(string[] args) {         new testcomponentlocation();     }      public testcomponentlocation() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.pack();                 frame.setlocationrelativeto(null);                   container contentpane = frame.getcontentpane();                 contentpane.setlayout(null);                 (int = 0; < 4; i++) {                     jpanel panel = new jpanel();                     panel.setbounds((i * 75) + 475, 25, 75, 100);                     system.out.println(panel.getbounds());                     contentpane.add(panel);                     panel.setborder(borderfactory.createlineborder(color.black, 3));                 }                 system.out.println(getcomponentat(contentpane, new point(475, 25)));                 system.out.println(getcomponentat(contentpane, new point(100, 25)));                  frame.setvisible(true);              }         });     }      public component getcomponentat(container parent, point p) {         component comp = null;         (component child : parent.getcomponents()) {             if (child.getbounds().contains(p)) {                 comp = child;             }         }         return comp;     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -