下面列出了javax.swing.JList#setSelectionModel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ListPanel(FilterModel fltrmodel, Function<T, String> mapper) {
setLayout(new java.awt.BorderLayout());
JScrollPane sp = new javax.swing.JScrollPane();
list = new JList();
list.setModel(fltrmodel);
list.setCellRenderer(new CheckBoxListRenderer(mapper));
sp.setViewportView(list);
add(sp, BorderLayout.CENTER);
list.setSelectionModel(new MultiSelectionModel(this::onSelect));
list.addKeyListener(onDelete());
list.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl A"), "SelectAll");
list.getActionMap().put("SelectAll", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
list.setSelectionInterval(0, list.getModel().getSize() - 1);
}
});
}
private void initUI() {
categoriesModel = new CategoriesListModel();
categoriesSelection = new CategoriesSelectionModel();
scrollIncrement = new JCheckBox("XXX").getPreferredSize().height; // NOI18N
JList<ProfilerOptionsPanel> categoriesList = new JList<ProfilerOptionsPanel>(categoriesModel) {
public Dimension getPreferredSize() {
Dimension dim = super.getPreferredSize();
dim.width = Math.max(dim.width + 20, 140);
return dim;
}
};
categoriesList.setVisibleRowCount(0);
categoriesList.setSelectionModel(categoriesSelection);
categoriesList.setCellRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String panelName = " " + ((ProfilerOptionsPanel)value).getDisplayName() + " "; // NOI18N
return super.getListCellRendererComponent(list, panelName, index, isSelected, cellHasFocus);
}
});
JScrollPane categoriesScroll = new JScrollPane(categoriesList);
JLabel categoriesLabel = new JLabel();
categoriesLabel.setHorizontalAlignment(JLabel.LEADING);
Mnemonics.setLocalizedText(categoriesLabel, Bundle.ProfilerOptionsContainer_Categories());
categoriesLabel.setLabelFor(categoriesList);
int labelOffset = 6;
JPanel categoriesPanel = new JPanel(new BorderLayout(0, labelOffset));
categoriesPanel.add(categoriesLabel, BorderLayout.NORTH);
categoriesPanel.add(categoriesScroll, BorderLayout.CENTER);
content = new JPanel(new BorderLayout());
content.setBorder(BorderFactory.createEmptyBorder(categoriesLabel.getPreferredSize().height + labelOffset, 11, 0, 0));
content.setMinimumSize(new Dimension(0, 0));
content.setPreferredSize(new Dimension(0, 0));
setLayout(new BorderLayout());
add(categoriesPanel, BorderLayout.WEST);
add(content, BorderLayout.CENTER);
}
private void initComponents() {
okButton = new JButton(NbBundle.getMessage(JmxConnectionConfigurator.class, "LBL_OK")); // NOI18N
hintLabel = new JLabel("") { // NOI18N
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = Math.max(d.height, okButton.getPreferredSize().height);
return d;
}
};
hintLabel.setForeground(UIManager.getColor("Label.disabledForeground")); // NOI18N
setLayout(new BorderLayout());
connectionTypeLabel = new JLabel();
Mnemonics.setLocalizedText(connectionTypeLabel,
NbBundle.getMessage(JmxConnectionConfigurator.class, "LBL_Connection_type")); // NOI18N
createBorder(connectionTypeLabel, BorderFactory.createEmptyBorder(15, 10, 0, 10));
add(connectionTypeLabel, BorderLayout.NORTH);
connectionTypeListModel = new DefaultListModel();
connectionTypeList = new JList(connectionTypeListModel) {
public String getToolTipText(MouseEvent evt) {
JmxConnectionCustomizer cust = getCustomizer(evt.getPoint());
return cust == null ? null : cust.getPropertiesDescription();
}
};
connectionTypeLabel.setLabelFor(connectionTypeList);
connectionTypeList.setSelectionModel(new DefaultListSelectionModel() {
public void removeSelectionInterval(int i1, int i2) {}
});
connectionTypeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final ListCellRenderer defaultRenderer = connectionTypeList.getCellRenderer();
Component c = defaultRenderer.getListCellRendererComponent(connectionTypeList, "X", 0, false, false); // NOI18N
connectionTypeList.setFixedCellHeight(c.getPreferredSize().height + 2);
connectionTypeList.setCellRenderer(new ListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
return defaultRenderer.getListCellRendererComponent(list, " " + value + " ", index, isSelected, cellHasFocus); // NOI18N
}
});
connectionTypeScroll = new JScrollPane(connectionTypeList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
public Dimension getPreferredSize() {
Dimension preferredSize = super.getPreferredSize();
preferredSize.width = Math.min(preferredSize.width, 300);
preferredSize.width = Math.max(preferredSize.width, 120);
return preferredSize;
}
};
createBorder(connectionTypeScroll, BorderFactory.createEmptyBorder(5, 10, 0, 0));
add(connectionTypeScroll, BorderLayout.WEST);
customizerPanel = new JPanel(new BorderLayout());
customizerPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
customizerPanelScroll = new ScrollableContainer(customizerPanel,
ScrollableContainer.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollableContainer.HORIZONTAL_SCROLLBAR_NEVER);
customizerPanelScroll.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 5));
add(customizerPanelScroll, BorderLayout.CENTER);
}