下面列出了javax.swing.JList#getSelectedValues ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected String exportString(JComponent c) {
JList list = (JList) c;
indices = list.getSelectedIndices();
@SuppressWarnings("deprecation")
Object[] values = list.getSelectedValues();
StringBuffer buff = new StringBuffer();
for (int i = 0; i < values.length; i++) {
Object val = values[i];
buff.append(val == null ? "" : val.toString());
if (i != values.length - 1) {
buff.append("\n");
}
}
return buff.toString();
}
@Override
public void focusLost(RComponent next) {
JList list = (JList) component;
String currentCellValue = getListCellValue(list, index);
if (currentCellValue != null && !currentCellValue.equals(cellValue)) {
recorder.recordSelect2(this, currentCellValue, true);
return;
}
Object[] selectedValues = list.getSelectedValues();
if (next == null || getComponent() != next.getComponent()) {
if (selectedValues == null || selectedValues.length == 0) {
recorder.recordSelect(this, "[]");
} else if (selectedValues.length > 1) {
String currentListSelectionText = getSelectionText((JList) component);
recorder.recordSelect(this, currentListSelectionText);
}
}
}
@Override
protected Transferable createTransferable(JComponent c) {
if (c instanceof JList) {
JList list = (JList) c;
Object[] values = list.getSelectedValues();
if (values == null || values.length == 0) {
return null;
}
List<TaskNode> nodes = new ArrayList<TaskNode>(values.length);
for (int i = 0; i < values.length; i++) {
Object val = values[i];
if (val instanceof TaskNode) {
nodes.add((TaskNode) val);
} else {
return null;
}
}
return new DashboardTransferable(nodes.toArray(new TaskNode[nodes.size()]));
}
return null;
}
/**
* Returns a list of all the objects in the given JList.
*/
public static String[] getList(Object[] size, JList objects) {
String[] list;
if (size.length == 0) {
list = new String[0];
}
else {
int[] select = new int[size.length];
for (int i = 0; i < size.length; i++) {
select[i] = i;
}
objects.setSelectedIndices(select);
size = objects.getSelectedValues();
list = new String[size.length];
for (int i = 0; i < size.length; i++) {
list[i] = (String) size[i];
}
}
return list;
}
@Messages({
"LBL_ProvidedTokens_T=Provided &Tokens:",
"ACS_ProvidedTokensTitle=Required tokens panel",
"ACS_LBL_ProvidedTokens=Required tokens",
"ACS_CTL_ProvidedTokensVerticalScroll=Required tokens vertical scroll bar",
"ACSD_CTL_ProvidedTokensVerticalScroll=Required tokens vertical scroll bar",
"ACS_CTL_ProvidedTokensHorizontalScroll=Required tokens horizontal scroll bar",
"ACSD_CTL_ProvidedTokensHorizontalScroll=Required tokens horizontal scroll bar",
"LBL_ProvidedTokens_NoMnem=Provided Tokens:"
})
private void addToken(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToken
// create add panel
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
panel.setLayout(new BorderLayout(0, 2));
JList tokenList = new JList(getProperties().getAllTokens());
JScrollPane tokenListSP = new JScrollPane(tokenList);
JLabel provTokensTxt = new JLabel();
provTokensTxt.setLabelFor(tokenList);
Mnemonics.setLocalizedText(provTokensTxt, LBL_ProvidedTokens_T());
panel.getAccessibleContext().setAccessibleDescription(ACS_ProvidedTokensTitle());
tokenList.getAccessibleContext().setAccessibleDescription(ACS_LBL_ProvidedTokens());
tokenListSP.getVerticalScrollBar().getAccessibleContext().setAccessibleName(ACS_CTL_ProvidedTokensVerticalScroll());
tokenListSP.getVerticalScrollBar().getAccessibleContext().setAccessibleDescription(ACSD_CTL_ProvidedTokensVerticalScroll());
tokenListSP.getHorizontalScrollBar().getAccessibleContext().setAccessibleName(ACS_CTL_ProvidedTokensHorizontalScroll());
tokenListSP.getHorizontalScrollBar().getAccessibleContext().setAccessibleDescription(ACSD_CTL_ProvidedTokensHorizontalScroll());
panel.add(provTokensTxt, BorderLayout.NORTH);
panel.add(tokenListSP, BorderLayout.CENTER);
DialogDescriptor descriptor = new DialogDescriptor(panel,
LBL_ProvidedTokens_NoMnem());
Dialog d = DialogDisplayer.getDefault().createDialog(descriptor);
d.setVisible(true);
d.dispose();
if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
Object[] selected = tokenList.getSelectedValues();
CustomizerComponentFactory.RequiredTokenListModel model = (CustomizerComponentFactory.RequiredTokenListModel) reqTokenList.getModel();
for (int i = 0; i < selected.length; i++) {
model.addToken((String) selected[i]);
}
if (selected.length > 0) {
reqTokenList.clearSelection();
reqTokenList.setSelectedValue(selected[0], true);
}
}
reqTokenList.requestFocusInWindow();
}
static List<BuildExecutionSupport.Item> selectProcessToKill(List<BuildExecutionSupport.Item> toStop) {
// Add all threads, sorted by display name.
DefaultListModel model = new DefaultListModel();
StopBuildingAlert alert = new StopBuildingAlert();
final JList list = alert.buildsList;
Comparator<BuildExecutionSupport.Item> comp = new Comparator<BuildExecutionSupport.Item>() {
private final Collator coll = Collator.getInstance();
@Override
public int compare(BuildExecutionSupport.Item t1, BuildExecutionSupport.Item t2) {
String n1 = t1.getDisplayName();
String n2 = t2.getDisplayName();
int r = coll.compare(n1, n2);
if (r != 0) {
return r;
} else {
// Arbitrary. XXX Note that there is no way to predict which is
// which if you have more than one build running. Ideally it
// would be subsorted by creation time, probably.
return System.identityHashCode(t1) - System.identityHashCode(t2);
}
}
};
SortedSet<BuildExecutionSupport.Item> items = new TreeSet<BuildExecutionSupport.Item>(comp);
items.addAll(toStop);
for (BuildExecutionSupport.Item t : items) {
model.addElement(t);
}
list.setModel(model);
list.setSelectedIndex(0);
// Make a dialog with buttons "Stop Building" and "Cancel".
DialogDescriptor dd = new DialogDescriptor(alert, NbBundle.getMessage(StopBuildingAlert.class, "TITLE_SBA"));
dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
final JButton stopButton = new JButton(NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_stop"));
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
stopButton.setEnabled(list.getSelectedValue() != null);
}
});
dd.setOptions(new Object[] {stopButton, DialogDescriptor.CANCEL_OPTION});
DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
List<BuildExecutionSupport.Item> toRet = new ArrayList<BuildExecutionSupport.Item>();
if (dd.getValue() == stopButton) {
Object[] selectedItems = list.getSelectedValues();
for (Object o : selectedItems) {
toRet.add((BuildExecutionSupport.Item)o);
}
}
return toRet;
}