getComponentByName (फ्रेम, नाम)
आप NetBeans या किसी अन्य आईडीई कि डिफ़ॉल्ट रूप से निजी चर (क्षेत्रों) बनाता है अपने AWT/स्विंग के सभी घटकों धारण करने के लिए उपयोग कर रहे हैं, उसके बाद निम्न कोड काम कर सकते हैं तुम्हारे लिए। इस प्रकार का उपयोग करें:
// get a button (or other component) by name
JButton button = Awt1.getComponentByName(someOtherFrame, "jButton1");
// do something useful with it (like toggle it's enabled state)
button.setEnabled(!button.isEnabled());
यहाँ ऊपर संभव बनाने के लिए कोड है ...
import java.awt.Component;
import java.awt.Window;
import java.lang.reflect.Field;
/**
* additional utilities for working with AWT/Swing.
* this is a single method for demo purposes.
* recommended to be combined into a single class
* module with other similar methods,
* e.g. MySwingUtilities
*
* @author http://javajon.blogspot.com/2013/07/java-awtswing-getcomponentbynamewindow.html
*/
public class Awt1 {
/**
* attempts to retrieve a component from a JFrame or JDialog using the name
* of the private variable that NetBeans (or other IDE) created to refer to
* it in code.
* @param <T> Generics allow easier casting from the calling side.
* @param window JFrame or JDialog containing component
* @param name name of the private field variable, case sensitive
* @return null if no match, otherwise a component.
*/
@SuppressWarnings("unchecked")
static public <T extends Component> T getComponentByName(Window window, String name) {
// loop through all of the class fields on that form
for (Field field : window.getClass().getDeclaredFields()) {
try {
// let us look at private fields, please
field.setAccessible(true);
// compare the variable name to the name passed in
if (name.equals(field.getName())) {
// get a potential match (assuming correct <T>ype)
final Object potentialMatch = field.get(window);
// cast and return the component
return (T) potentialMatch;
}
} catch (SecurityException | IllegalArgumentException
| IllegalAccessException ex) {
// ignore exceptions
}
}
// no match found
return null;
}
}
यह प्रतिबिंब का उपयोग करता वर्ग क्षेत्रों के माध्यम से देखने के लिए यह एक घटक है कि कहा जाता है मिल सकता है देखने के लिए एक ही नाम के एक चर द्वारा।
नोट: उपरोक्त कोड जेनिक्स का उपयोग परिणामों को कास्ट करने के लिए करता है ताकि आप जो भी प्रकार की उम्मीद कर रहे हों, इसलिए कुछ मामलों में आपको टाइप कास्टिंग के बारे में स्पष्ट होना पड़ सकता है। उदाहरण के लिए यदि myOverloadedMethod
दोनों JButton
और JTextField
, आप स्पष्ट रूप से अधिभार आप कॉल करना चाहते परिभाषित करने के लिए आवश्यकता हो सकती है ...
myOverloadedMethod((JButton) Awt1.getComponentByName(someOtherFrame, "jButton1"));
स्वीकार करता है और अगर आप सुनिश्चित नहीं हैं, तो आप एक Component
हो और instanceof
साथ यह जाँच कर सकते हैं ...
// get a component and make sure it's a JButton before using it
Component component = Awt1.getComponentByName(someOtherFrame, "jButton1");
if (component instanceof JButton) {
JButton button = (JButton) component;
// do more stuff here with button
}
आशा है कि इससे मदद मिलती है!
Window.getWindows() और फिर स्कैन करें जो आपको चाहिए – bestsss
दुनिया में आप ऐसा क्यों करना चाहेंगे?ऐसा करके, आप दो महत्वपूर्ण स्थिर कंपाइलर चेक खो देते हैं: - सबसे पहले, फ़ील्ड मौजूद है। - दूसरा, यह सही प्रकार का है। इसके अलावा, गतिशील लुकअप क्षेत्र के संदर्भ के मुकाबले धीमी है। – fortran
उदाहरण के लिए, डरावनी एपीआई की वजह से, जहां आपके पास कुछ घटकों तक प्रत्यक्ष पहुंच नहीं है, जिन्हें आप संशोधित करना चाहते हैं। –