下面列出了怎么用javax.swing.ListSelectionModel的API类实例代码及写法,或者点击链接到github查看源代码。
public void init() {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
tableIcon = FormUtils.getIcon(tableIcon, DEFAULT_TABLE_ICON);
for (PanelHolder p : panels)
p.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
list = new JList(new ListListModel(panels));
list.setBorder(BorderFactory.createEmptyBorder(5, 5 , 5, 5));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setVisibleRowCount(-1);
list.addListSelectionListener(this);
list.setCellRenderer(renderer);
list.setSelectedIndex(0);
if (cellHeight != 0)
list.setFixedCellHeight(cellHeight);
JScrollPane scroll = new JScrollPane(list);
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroll, editorPanel);
split.setResizeWeight(0);
split.setDividerLocation(150);
add(split);
}
private void initBottomPanel()
{
MessagesTableModel messagesTableModel = new MessagesTableModel();
messagesTable = new JTable(messagesTableModel);
messagesTable.getColumnModel().getColumn(0).setPreferredWidth(90);
messagesTable.getColumnModel().getColumn(1).setPreferredWidth(5);
messagesTable.getColumnModel().getColumn(2).setPreferredWidth(75);
messagesTable.getColumnModel().getColumn(3).setPreferredWidth(5);
messagesTable.getColumnModel().getColumn(4).setPreferredWidth(510);
messagesTable.setDefaultRenderer(String.class,
new MessagesTableCellRender());
messagesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
messagesTable.addMouseListener(new MessagesTableMouseListener(this));
messenger.getApplication().addMessageListener(messagesTableModel);
bottomPanelScrollPane = new JScrollPane(messagesTable);
bottomPanelScrollPane.setPreferredSize(new Dimension(
bottomPanelScrollPane.getPreferredSize().width, 120));
bottomPanelScrollPane.getViewport().add(messagesTable);
add(bottomPanelScrollPane, BorderLayout.SOUTH);
}
public ClassPathFormImpl(Bindings bindings, JFileChooser fc) {
bindings.addOptComponent("classPath", ClassPath.class, _classpathCheck)
.add("classPath.mainClass", _mainclassField)
.add("classPath.paths", _classpathList);
_fileChooser = fc;
ClasspathCheckListener cpl = new ClasspathCheckListener();
_classpathCheck.addChangeListener(cpl);
cpl.stateChanged(null);
_classpathList.setModel(new DefaultListModel());
_classpathList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
_classpathList.addListSelectionListener(new ClasspathSelectionListener());
_newClasspathButton.addActionListener(new NewClasspathListener());
_acceptClasspathButton.addActionListener(
new AcceptClasspathListener(_classpathField));
_removeClasspathButton.addActionListener(new RemoveClasspathListener());
_importClasspathButton.addActionListener(new ImportClasspathListener());
_classpathUpButton.addActionListener(new MoveUpListener());
_classpathDownButton.addActionListener(new MoveDownListener());
}
@Override
public void setSelectionModel(ListSelectionModel newModel) {
if (newModel == null) {
throw new IllegalArgumentException("Cannot set a null SelectionModel");
}
ListSelectionModel oldModel = selectionModel;
if (newModel != oldModel) {
if (oldModel != null) {
oldModel.removeListSelectionListener(this);
}
selectionModel = newModel;
newModel.addListSelectionListener(this);
}
}
public SorterTableColumnModel(Sortables[] columnHeaders) {
MainFrame mainFrame = MainFrame.getInstance();
int x = 0;
for (Sortables c : columnHeaders) {
if (!c.isAvailable(mainFrame)) {
continue;
}
shown.add(c);
TableColumn tc = makeTableColumn(x, c);
columnList.add(tc);
x++;
}
dlsm = new DefaultListSelectionModel();
dlsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
orderUpdate();
check();
}
private void buildPanel() {
tableModel = new LocationReferencesTableModel(locationReferencesProvider);
tablePanel = new GhidraThreadedTablePanel<>(tableModel, 250);
table = tablePanel.getTable();
table.setHTMLRenderingEnabled(true);
table.setPreferredScrollableViewportSize(new Dimension(300, 120));
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
setLayout(new BorderLayout(10, 10));
PluginTool tool = locationReferencesProvider.getTool();
GoToService goToService = tool.getService(GoToService.class);
table.installNavigation(goToService, goToService.getDefaultNavigatable());
GhidraTableFilterPanel<LocationReference> tableFilterPanel =
new GhidraTableFilterPanel<>(table, tableModel);
add(tablePanel, BorderLayout.CENTER);
add(tableFilterPanel, BorderLayout.SOUTH);
}
public ListCategoryPane() {
pane = new JPanel(new MigLayout("fill"));
table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)
&& e.getClickCount() == 2
&& table.getSelectedRow() != -1) {
Category category = tableModel.getRow(table
.getSelectedRow());
categoryDetailsPane.show(category, getPane());
}
}
});
final JScrollPane scrollPane = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane.add(scrollPane, "grow");
}
public ClassPathFormImpl(Bindings bindings, JFileChooser fc) {
bindings.addOptComponent("classPath", ClassPath.class, _classpathCheck)
.add("classPath.mainClass", _mainclassField)
.add("classPath.paths", _classpathList);
_fileChooser = fc;
ClasspathCheckListener cpl = new ClasspathCheckListener();
_classpathCheck.addChangeListener(cpl);
cpl.stateChanged(null);
_classpathList.setModel(new DefaultListModel());
_classpathList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
_classpathList.addListSelectionListener(new ClasspathSelectionListener());
_newClasspathButton.addActionListener(new NewClasspathListener());
_acceptClasspathButton.addActionListener(
new AcceptClasspathListener(_classpathField));
_removeClasspathButton.addActionListener(new RemoveClasspathListener());
_importClasspathButton.addActionListener(new ImportClasspathListener());
_classpathUpButton.addActionListener(new MoveUpListener());
_classpathDownButton.addActionListener(new MoveDownListener());
}
/**
* Set the lead and anchor without affecting selection.
*/
public static void setLeadAnchorWithoutSelection(ListSelectionModel model,
int lead, int anchor) {
if (anchor == -1) {
anchor = lead;
}
if (lead == -1) {
model.setAnchorSelectionIndex(-1);
model.setLeadSelectionIndex(-1);
} else {
if (model.isSelectedIndex(lead)) {
model.addSelectionInterval(lead, lead);
} else {
model.removeSelectionInterval(lead, lead);
}
model.setAnchorSelectionIndex(anchor);
}
}
private void jRemoveItemsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveItemsButtonActionPerformed
ListSelectionModel lsm = jToList.getSelectionModel();
if (lsm.isSelectionEmpty())
return;
// Multiple interval selection. Loop thru them
int minIndex=lsm.getMinSelectionIndex();
int maxIndex=lsm.getMaxSelectionIndex();
for(int i=minIndex; i<=maxIndex; i++){
if (lsm.isSelectedIndex(i)){
String objName=(String) jToList.getModel().getElementAt(i);
if (currentGroup.contains(objName))
currentGroup.remove(dSet.get(objName));
}
}
updateCurrentGroup();
// TODO add your handling code here:
}
public void selectAllCells() throws Throwable {
final JTable table = (JTable) ComponentUtils.findComponent(JTable.class, frame);
final LoggingRecorder lr = new LoggingRecorder();
siw(new Runnable() {
@Override
public void run() {
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
table.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
int rowCount = table.getRowCount();
int colCount = table.getColumnCount();
table.addRowSelectionInterval(0, rowCount - 1);
table.addColumnSelectionInterval(0, colCount - 1);
RTable rTable = new RTable(table, null, null, lr);
rTable.focusLost(null);
}
});
Call call = lr.getCall();
AssertJUnit.assertEquals("select", call.getFunction());
AssertJUnit.assertEquals("all", call.getState());
}
public AnnotationPanel(final ImagePanel imagePanel, boolean readOnly) {
putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
setModel(new AnnotationTableModel(imagePanel));
if (!readOnly)
setDefaultEditor(Annotation.class, new AnnotationEditor());
setDefaultRenderer(Annotation.class, new AnnotationRenderer());
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
int index = getSelectedRow();
if (index == -1)
return;
imagePanel.setSelectedAnnotation((Annotation) imagePanel.getAnnotations().get(index), false);
}
});
}
@NbBundle.Messages({
"MSG_Detecting_Wait=Detecting installations, please wait..."
})
private void initList() {
installationList.setListData(new Object[]{
Bundle.MSG_Detecting_Wait()
});
installationList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
installationList.setEnabled(false);
RequestProcessor.getDefault().post(new Runnable() {
@Override
public void run() {
final List<Installation> installations =
InstallationManager.detectAllInstallations();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
updateListForDetectedInstallations(installations);
}
});
}
});
}
/**
* Remove a reaction
*/
private void removeReaction() {
int index = reactions.getSelectedIndex();
if (index != -1) {
String selected = ((String) reactions.getSelectedValue()).split("\\[| ")[0];
Reaction reaction = bioModel.getSBMLDocument().getModel().getReaction(selected);
if (BioModel.isProductionReaction(reaction)) {
modelEditor.removePromoter(SBMLutilities.getPromoterId(reaction));
} else {
bioModel.removeReaction(selected);
reactions.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
reacts = (String[]) Utility.remove(reactions, reacts);
reactions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (index < reactions.getModel().getSize()) {
reactions.setSelectedIndex(index);
}
else {
reactions.setSelectedIndex(index - 1);
}
}
modelEditor.setDirty(true);
modelEditor.makeUndoPoint();
}
}
public void setSelectedItem(Object anItem) {
int index = -1;
for (int i = 0; i < getSize(); i++) {
Object elementAt = listModel.getElementAt(i);
if (elementAt != null && elementAt.equals(anItem)) {
index = i;
break;
}
}
listSelectionModel
.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel.setSelectionInterval(index, index);
}
public void createCourseSpecificTab(
ObjectFinder finder,
ProjectOpener opener,
String course,
JTabbedPane tabbedPaneBase,
CourseAndExerciseManager courseAndExerciseManager) {
logger.info("Creating course specific tab. @CourseTabFactory");
final JBScrollPane panel = new JBScrollPane();
final JBList list = new JBList();
list.setCellRenderer(new ProjectListRenderer());
DefaultListModel defaultListModel = new DefaultListModel();
panel.setBorder(BorderFactory.createTitledBorder(""));
ProjectListManagerHolder.get()
.addExercisesToList(finder, course, defaultListModel, courseAndExerciseManager);
if (defaultListModel.getSize() <= 0) {
return;
}
list.setName(course);
list.setModel(defaultListModel);
MouseListener mouseListener = createMouseListenerForWindow(opener, panel, list);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addMouseListener(mouseListener);
panel.setName(course);
panel.setViewportView(list);
ProjectListManagerHolder.get().addList(list);
tabbedPaneBase.addTab(course, panel);
tabbedPaneBase.addMouseListener(tabMouseListener(tabbedPaneBase));
setScrollBarToBottom(course, tabbedPaneBase, panel);
}
/**
* Getter method for component schemaList.
*
* @return the initialized component
*/
public DefaultList getSchemaList() {
if (schemaList == null) {
schemaList = new DefaultList();
schemaList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
return schemaList;
}
public void valueChanged(ListSelectionEvent evt) {
ListSelectionModel selectionModel = getSelectionModel();
int r = selectionModel.getMinSelectionIndex();
panel.upButton.setEnabled(r > 0);
panel.downButton.setEnabled(r > -1 && r < getModel().getRowCount() - 1);
panel.removeButton.setEnabled(r > -1);
panel.editButton.setEnabled(r > -1);
}
private void onRemoveClick() {
ListSelectionModel selectionModel = getSelectionModel();
int r = selectionModel.getMinSelectionIndex();
if(r > -1) {
getModel().removeRow(r);
}
int size = getModel().getRowCount();
if(size > 0) {
if (r > size - 1) {
r = size - 1;
}
selectionModel.setSelectionInterval(r, r);
}
}
private void get_Fields() {
this.jList_Fields.removeAll();
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < _selectLayer.getFieldNumber(); i++) {
listModel.addElement(_selectLayer.getField(i).getColumnName());
}
this.jList_Fields.setModel(listModel);
this.jList_Fields.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.jList_Fields.setSelectedIndex(0);
}
/**
* Initializes the request table.
*/
private void initRequestTable() {
requestTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
requestTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
selectedItemChanged();
}
});
}
public void setTopicMapsList() {
if(getApiKey() != null) {
try {
JSONObject list = MaianaUtils.listAvailableTopicMaps(getApiEndPoint(), getApiKey());
if(list.has("msg")) {
WandoraOptionPane.showMessageDialog(window, list.getString("msg"), "API says", WandoraOptionPane.WARNING_MESSAGE);
//System.out.println("REPLY:"+list.toString());
}
if(list.has("data")) {
mapTable = new JTable();
mapTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
if(list != null) {
JSONArray datas = list.getJSONArray("data");
TopicMapsTableModel myModel = new TopicMapsTableModel(datas);
mapTable.setModel(myModel);
mapTable.setRowSorter(new TableRowSorter(myModel));
mapTable.setColumnSelectionAllowed(false);
mapTable.setRowSelectionAllowed(true);
mapTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
TableColumn column = null;
for (int i=0; i < mapTable.getColumnCount(); i++) {
column = mapTable.getColumnModel().getColumn(i);
column.setPreferredWidth(myModel.getColumnWidth(i));
}
tableScrollPane.setViewportView(mapTable);
}
}
}
catch(Exception e) {
Wandora.getWandora().displayException("Exception '"+e.getMessage()+"' occurred while getting the list of topic maps.", e);
}
}
}
private Component createTemplateSelector() {
TableModel<SceneTemplateModel> templates =
TableModelBuilder
.builder(ServiceLocator.getInstance(SceneController.class).getTemplates())
.columnBuilder()
.withName("ID")
.withGetter(SceneTemplateModel::getId)
.add()
.columnBuilder()
.withName("Name")
.withGetter(SceneTemplateModel::getName)
.add()
.columnBuilder()
.withName("Available?")
.withGetter(SceneTemplateModel::getAvailable)
.add()
.build();
Table<SceneTemplateModel> table = new Table<>(templates);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener((e) -> {
if(e.getValueIsAdjusting()) {
return;
}
this.value = templates.getValue(table.getSelectedRow());
if(this.value != null) {
nextButton.setEnabled(true);
}
});
return new JScrollPane(table);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
selectFileLabel = new JLabel();
selectFileScrollPane = new JScrollPane();
selectFileList = new JList<>();
selectFileLabel.setLabelFor(selectFileList);
Mnemonics.setLocalizedText(selectFileLabel, NbBundle.getMessage(SelectFilePanel.class, "SelectFilePanel.selectFileLabel.text")); // NOI18N
selectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectFileScrollPane.setViewportView(selectFileList);
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(selectFileScrollPane, GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
.addComponent(selectFileLabel))
.addContainerGap())
);
layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(selectFileLabel)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(selectFileScrollPane, GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
);
}
/**
* Constructeur.
*
* @param dataModel
* Modèle pour les données (par exemple, MTableModel)
*/
public MBasicTable(final TableModel dataModel) {
super(dataModel);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// setAutoCreateColumnsFromModel(false);
// par défaut, on laisse AUTO_RESIZE_SUBSEQUENT_COLUMNS
// setAutoResizeMode(AUTO_RESIZE_OFF);
// setDragEnabled(true);
addKeyListener(KEY_HANDLER);
}
/**
* Remove a compartment
*/
private void removeCompartment() {
int index = compartments.getSelectedIndex();
if (index != -1) {
if (!Utils.compartmentInUse(bioModel.getSBMLDocument(),
((String) compartments.getSelectedValue()).split("\\[| ")[0])) {
if (!SBMLutilities.variableInUse(bioModel.getSBMLDocument(), ((String) compartments.getSelectedValue()).split("\\[| ")[0], false, true, this, null)) {
Compartment tempComp = bioModel.getSBMLDocument().getModel().getCompartment(((String) compartments.getSelectedValue()).split("\\[| ")[0]);
ListOf<Compartment> c = bioModel.getSBMLDocument().getModel().getListOfCompartments();
for (int i = 0; i < bioModel.getSBMLDocument().getModel().getCompartmentCount(); i++) {
if (c.get(i).getId().equals(tempComp.getId())) {
c.remove(i);
}
}
for (int i = 0; i < bioModel.getSBMLCompModel().getListOfPorts().size(); i++) {
Port port = bioModel.getSBMLCompModel().getListOfPorts().get(i);
if (port.isSetIdRef() && port.getIdRef().equals(tempComp.getId())) {
bioModel.getSBMLCompModel().getListOfPorts().remove(i);
break;
}
}
compartments.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Utility.remove(compartments);
compartments.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (index < compartments.getModel().getSize()) {
compartments.setSelectedIndex(index);
}
else {
compartments.setSelectedIndex(index - 1);
}
modelEditor.setDirty(true);
modelEditor.makeUndoPoint();
}
}
}
}
private void setLookAndFeel() {
jlist.setOpaque(true);
jlist.setBackground(Color.WHITE);
jlist.setForeground(Color.BLACK);
jlist.setSelectionBackground(HIGHLIGHT_BACK);
jlist.setSelectionForeground(HIGHLIGHT_FORE);
jlist.setFocusable(true);
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
@Override
public void restoreModels(ModelMap models)
{
models.get(FilterHandler.class).install();
models.get(SkillFilterHandler.class).install();
skillpointTable.setModel(models.get(SkillPointTableModel.class));
skillpointTable.setSelectionModel(models.get(ListSelectionModel.class));
skillTable.setDefaultEditor(Float.class, models.get(SkillRankSpinnerEditor.class));
models.get(SkillTreeViewModel.class).install(skillTable);
models.get(InfoHandler.class).install();
models.get(LevelSelectionHandler.class).install();
models.get(SkillSheetHandler.class).install();
}
/**
* Creates a new JList for a given source of a configurable
*
* @param source
* can be null for local configurables, otherwise name of the source
* @return the created JList
*/
private JList<Configurable> createNewConfigurableJList(String source) {
final JList<Configurable> createdConfigList = new JList<>();
createdConfigList.setModel(source == null ? localConfigListModel : remoteConfigListModels.get(source));
createdConfigList.setCellRenderer(new ConfigurableRenderer());
createdConfigList.setFixedCellHeight(40);
createdConfigList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
createdConfigList.setBackground(LIGHTER_GRAY);
return createdConfigList;
}
public ToolbarList(ToolbarData base) {
this.base = base;
this.model = new Model();
setModel(model);
setCellRenderer(new ListRenderer());
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
AppPreferences.GATE_SHAPE.addPropertyChangeListener(model);
base.addToolbarListener(model);
base.addToolAttributeListener(model);
}