1
他の場所の文字列のArrayListを通じて読み込まれているJListを持っています。同じリストに対して、私のディレクトリに保存されているImageIconを表示します。どこかに。今のところ私はリストに追加されたアイテム(またはリストに現在あるアイテム)に同じアイコンを表示したい。別のオブジェクトを使用してJListデータを読み込むJListにImageIconを表示
私のリストは次のようになります。ICON学生NAMEを... ICON学生NAME
問題(画像アイコンが正しい高さを示しており、それが捕獲されているが、で、リストには表示されません。実行時
ここリストにデータを追加し、私のアクションリスナーがある。
public class StudentListener implements ActionListener{
private Main_Menu menu;
private ArrayList<String> arrayList = new ArrayList<String>();;
Iterator iterator = arrayList.iterator();
JList sList;
Map<Object, Icon> icons = new HashMap<Object, Icon>();
/**
*
* @param menu the referenced menu from our main menu
*/
public StudentListener(Main_Menu menu){
this.menu = menu;
}
@Override
public void actionPerformed(ActionEvent ae) {
Icon iCon = new ImageIcon("/Project/src/Images/1312046124_picture.png"); // icons
int iHeight = iCon.getIconHeight();
icons.put("name", iCon);
//add all the students to our List
try {
StudentModel = new Student_Model();
} catch (SQLException ex) {
Logger.getLogger(Student_Controller.class.getName()).log(Level.SEVERE, null, ex);
}
//arrayList = StudentModel.getStudents(); // modify to use an arrayList of string
arrayList.add("John");
arrayList.add("Smith");
iterator = arrayList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next().toString());
}
sList = this.menu.getStudentList();
sList.setListData(arrayList.toArray());
sList.setFont(new Font("Arial", Font.BOLD, 14));
System.out.println("height of icon " + iHeight); // displays the correct height
sList.setCellRenderer(new IconListRenderer(icons));
}
}
IconListCellRendererを
public class IconListRenderer
extends DefaultListCellRenderer {
private Map<Object, Icon> icons = null;
public IconListRenderer(Map<Object, Icon> icons) {
this.icons = icons;
}
@Override
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
// Get the renderer component from parent class
JLabel label =
(JLabel) super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
// Get icon to use for the list item value
Icon icon = icons.get(value);
// Set icon to display for value
label.setIcon(icon);
return label;
}
}
! StuentModelまたはIconListRendererにアクセスすることはできません。私たちはSSCCEのStudentModelも必要としません。あなたがする必要があることは、あなたのArrayListの学生名をハードコアすることだけです。私は問題がレンダラであると推測しています。 **ヘルプが必要な場合は、適切なSSCCEを掲示してください。私は質問を投稿するたびにあなたに思い出させるのにうんざりしています。 – camickr
私はcamickrに同意しなければなりません。小さな実用例がなければ、コードがうまくいかない理由を推測することはできません。 SSCCEを提供したい場合は、実際にあなた次第です - どのくらいの助けが必要ですか? –
@Camickr、アニメーションを取得する必要はありません。これを解決するには本当に近いですし、おそらくListCellRendererプロパティを正しく使用していなかったと思います。これが参照コードを投稿した理由です。より良い返答を得るために私の投稿を更新します。 – Warz