具有三个组件的Java GUI Swing Jlist [英] Java GUI Swing Jlist with three Components
问题描述
我需要用三个组件创建一个 Java Swing JList
。
每个JList行应该有一个JCheckBox ,一个ImageIcon和一个JLabel。
问题是JLabel只能有两个元素。所以我需要一个方法来添加一个JCheckBox ...
Jlist有三个组成部分: -
没有任何真实的信息,我能建议的最好的方法是从请查看
公共静态类CustomListCellRenderer扩展JPanel实现ListCellRenderer< Data> {
private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1,1,1,1);
私有JCheckBox checkBox;
私人JLabel标签;
public CustomListCellRenderer(){
setOpaque(false);
setLayout(new FlowLayout(FlowLayout.LEFT));
setBorder(DEFAULT_NO_FOCUS_BORDER);
checkBox = new JCheckBox();
label = new JLabel();
checkBox.setOpaque(false);
add(checkBox);
add(label);
}
@Override
public Component getListCellRendererComponent(JList<?extends Data> list,Data value,int index,boolean isSelected,boolean cellHasFocus){
checkBox。的setSelected(value.isSelecetd());
label.setIcon(new ImageIcon(value.getImage()));
label.setText(value.getText());
颜色fg = list.getForeground();
if(isSelected){
setBackground(list.getSelectionBackground());
fg = list.getSelectionForeground();
}
label.setForeground(fg);
setOpaque(isSelected);
Border border = null;
if(cellHasFocus){
if(isSelected){
border = UIManager.getBorder(List.focusSelectedCellHighlightBorder);
}
if(border == null){
border = UIManager.getBorder(List.focusCellHighlightBorder);
}
}其他{
border = DEFAULT_NO_FOCUS_BORDER;
}
setBorder(border);
返回此;
}
}
I need to create a Java Swing JList
with three Components.
Each JList row should have one JCheckBox, one ImageIcon and one JLabel. The problem is that JLabel could have only two elements. So i need a methode to add a JCheckBox...
Jlist with three components:-
Without any real information, the best I can suggest is start by having a look at Concepts: Editors and Renderers and Writing a Custom Cell Renderer for how cell renders work.
Based on you basic requirements, you need to start with a container class of some sort and add your components to it, you then need to populate the values of the components each time getListCellRendererComponent
is called with the data it provides.
You will also need to take care of the selection rendering, since that's normally taken care of by the DefaultListCellRenderer
As an example...
public static class CustomListCellRenderer extends JPanel implements ListCellRenderer<Data> {
private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
private JCheckBox checkBox;
private JLabel label;
public CustomListCellRenderer() {
setOpaque(false);
setLayout(new FlowLayout(FlowLayout.LEFT));
setBorder(DEFAULT_NO_FOCUS_BORDER);
checkBox = new JCheckBox();
label = new JLabel();
checkBox.setOpaque(false);
add(checkBox);
add(label);
}
@Override
public Component getListCellRendererComponent(JList<? extends Data> list, Data value, int index, boolean isSelected, boolean cellHasFocus) {
checkBox.setSelected(value.isSelecetd());
label.setIcon(new ImageIcon(value.getImage()));
label.setText(value.getText());
Color fg = list.getForeground();
if (isSelected) {
setBackground(list.getSelectionBackground());
fg = list.getSelectionForeground();
}
label.setForeground(fg);
setOpaque(isSelected);
Border border = null;
if (cellHasFocus) {
if (isSelected) {
border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = UIManager.getBorder("List.focusCellHighlightBorder");
}
} else {
border = DEFAULT_NO_FOCUS_BORDER;
}
setBorder(border);
return this;
}
}
这篇关于具有三个组件的Java GUI Swing Jlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!