このコードを考えると、
tableButton = new JButton(new ImageIcon("80F_FG2015.GIF"));
String
他の人と比較できるように、どうすればそれを私に返すことができString
ますか?
問題を解決する1つの方法は、作成することです
tableButton = new JButton(new ImageIcon("80F_FG2015.GIF", "80F_FG2015.GIF"));
2番目の引数は説明です。また、ImageIcon.toString()メソッドは説明を返します。したがって、この説明を別の画像アイコンの説明と非常によく比較できます。
次のように説明を取り戻すことができます。
System.out.println(((ImageIcon)tableButton.getIcon()).getDescription());
//or
System.out.println(((ImageIcon)tableButton.getIcon()).toString());
Reflection を使用してそれを実現できます。
ImageIcon icon = new ImageIcon(pathImg);
JButton jButton1 = new JButton(icon);
Field field = null;
try {
Class<? extends ImageIcon> clazz = ((ImageIcon) jButton1.getIcon()).getClass();
//Get the field "filename" where the Image path is stored.
field = clazz.getDeclaredField("filename");
field.setAccessible(true);
String path = (String) field.get(icon);
} catch (NoSuchFieldException ex) {
Logger.getLogger(AAMainWindow.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(AAMainWindow.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(AAMainWindow.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(AAMainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
JButton を拡張する新しいクラスを作成できます。文字列をクラスに格納します。
class MyButton extends JButton
{
private String filename;
public MyButton(String filename)
{
super(new ImageIcon(filename));
this.filename = filename;
}
public String getFilename()
{
return filename;
}
}