Java
JList의 CustomReder (JList에 JLabel붙이기)
- -
package example;
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
public class CountryList extends JFrame {
public CountryList() {
setSize(400,700);
setVisible(true);
getContentPane().setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
DefaultListModel<JLabel> listModel = new DefaultListModel<JLabel>();
ImageIcon icon = new ImageIcon("Country/Canada.png");
JLabel ca = new JLabel("Canada", icon, SwingConstants.CENTER);
listModel.addElement(ca);
icon = new ImageIcon("Country/France.png");
JLabel fr = new JLabel("France", icon, SwingConstants.CENTER);
listModel.addElement(fr);
icon = new ImageIcon("Country/Germany.png");
JLabel gr = new JLabel("Germany", icon, SwingConstants.CENTER);
listModel.addElement(gr);
icon = new ImageIcon("Country/Japan.png");
JLabel jp = new JLabel("Japan", icon, SwingConstants.CENTER);
listModel.addElement(jp);
icon = new ImageIcon("Country/Korea.png");
JLabel kr = new JLabel("Korea", icon, SwingConstants.CENTER);
listModel.addElement(kr);
icon = new ImageIcon("Country/UK.png");
JLabel uk = new JLabel("UK", icon, SwingConstants.CENTER);
listModel.addElement(uk);
icon = new ImageIcon("Country/USA.png");
JLabel usa = new JLabel("USA", icon, SwingConstants.CENTER);
listModel.addElement(usa);
JList<JLabel> CountryList = new JList<JLabel>(listModel);
scrollPane.setViewportView(CountryList);
}
public static void main(String[] args) {
CountryList countrylist = new CountryList();
}
}
실행결과 :
디버깅을 해보니 JList에 표시되는 내용은 JLabel의 toString()의 값을 표시하고있었다.
JList에 CustomRender을 추가하여 수정하면
package example;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
public class CountryList extends JFrame {
public class CustomListRenderer extends JLabel implements ListCellRenderer<JLabel> {
@Override
public Component getListCellRendererComponent(JList<? extends JLabel> list, JLabel label, int index, boolean isSelected,
boolean cellHasFocuse) {
return label;
}
}
public CountryList() {
setSize(400,700);
setVisible(true);
getContentPane().setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
DefaultListModel<JLabel> listModel = new DefaultListModel<JLabel>();
ImageIcon icon = new ImageIcon("Country/Canada.png");
JLabel ca = new JLabel("Canada", icon, SwingConstants.CENTER);
listModel.addElement(ca);
icon = new ImageIcon("Country/France.png");
JLabel fr = new JLabel("France", icon, SwingConstants.CENTER);
listModel.addElement(fr);
icon = new ImageIcon("Country/Germany.png");
JLabel gr = new JLabel("Germany", icon, SwingConstants.CENTER);
listModel.addElement(gr);
icon = new ImageIcon("Country/Japan.png");
JLabel jp = new JLabel("Japan", icon, SwingConstants.CENTER);
listModel.addElement(jp);
icon = new ImageIcon("Country/Korea.png");
JLabel kr = new JLabel("Korea", icon, SwingConstants.CENTER);
listModel.addElement(kr);
icon = new ImageIcon("Country/UK.png");
JLabel uk = new JLabel("UK", icon, SwingConstants.CENTER);
listModel.addElement(uk);
icon = new ImageIcon("Country/USA.png");
JLabel usa = new JLabel("USA", icon, SwingConstants.CENTER);
listModel.addElement(usa);
JList<JLabel> CountryList = new JList<JLabel>(listModel);
CustomListRenderer MyRender = new CustomListRenderer();
CountryList.setCellRenderer(MyRender);
scrollPane.setViewportView(CountryList);
}
public static void main(String[] args) {
CountryList countrylist = new CountryList();
}
}
실행결과 :
정상적으로 잘 수행된 모습이다.
다른방법으로는 Country Class를 만들고 그에 맞게 CustomRederer를 재작성해주면 된다.
//Country.Java
package example;
public class Country{
private String name;
private String image_name;
public Country(String name, String image_name) {
this.name = name;
this.image_name = image_name;
}
public String getName() {
return this.name;
}
public String getImageName() {
return this.image_name;
}
@Override
public String toString() {
return this.name;
}
}
//CountryList.java
package example;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
public class CountryList extends JFrame {
public class CustomListRenderer extends JLabel implements ListCellRenderer<Country> {
@Override
public Component getListCellRendererComponent(JList<? extends Country> list, Country country, int index,
boolean isSelected, boolean cellHasFocuse) {
ImageIcon icon = new ImageIcon(country.getImageName());
setIcon(icon);
setText(country.getName());
return this;
}
}
public CountryList() {
setSize(400, 700);
setVisible(true);
getContentPane().setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
DefaultListModel<Country> listModel = new DefaultListModel<Country>();
JList<Country> CountryList = new JList<Country>(listModel);
CustomListRenderer MyRender = new CustomListRenderer();
CountryList.setCellRenderer(MyRender);
Country usa = new Country("USA", "Country/USA.png");
Country ca = new Country("Canada", "Country/Canada.png");
Country fr = new Country("France", "Country/France.png");
Country gr = new Country("Germany", "Country/Germany.png");
Country jp = new Country("Japan", "Country/Japan.png");
Country kr = new Country("Korea", "Country/Korea.png");
Country uk = new Country("UK", "Country/UK.png");
listModel.addElement(usa);
listModel.addElement(ca);
listModel.addElement(fr);
listModel.addElement(gr);
listModel.addElement(jp);
listModel.addElement(kr);
listModel.addElement(uk);
scrollPane.setViewportView(CountryList);
}
public static void main(String[] args) {
CountryList countrylist = new CountryList();
}
}
실행결과 :
Contents
소중한 공감 감사합니다