下面列出了javax.swing.ListSelectionModel#isSelectedIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Selection(ListSelectionModel sm) {
selectionMode = sm.getSelectionMode();
anchor = sm.getAnchorSelectionIndex();
lead = sm.getLeadSelectionIndex();
int min = sm.getMinSelectionIndex();
int max = sm.getMaxSelectionIndex();
int i1 = -1;
for (int i = min; i <= max; i++) {
if (sm.isSelectedIndex(i)) {
if (i1 == -1) {
i1 = i;
}
} else {
if (i1 != -1) {
intervals.add(new int[] { i1, i});
i1 = -1;
}
}
}
if (i1 != -1) {
intervals.add(new int[] { i1, max});
}
}
/**
* 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);
}
}
public <E extends TableRowData>TableData<E> getSelectedData(){
ListSelectionModel lsm = getSelectionModel();
List<Integer> indexes = new ArrayList<Integer>();
int start = lsm.getMinSelectionIndex();
int end = lsm.getMaxSelectionIndex();
if (start >= 0){
for(int i = start; i <= end; i++){
if (lsm.isSelectedIndex(i)){
indexes.add(i);
}
}
}
return ((AstrosoftTableModel) getModel()).getData(indexes);
}
@Override
public String toString() {
String ret = "[\n";
for (int col=0; col<listSelectionModels.size(); col++) {
ret += "\'"+col+"\'={";
ListSelectionModel lsm = getListSelectionModelAt(col);
int startRow = lsm.getMinSelectionIndex();
int endRow = lsm.getMaxSelectionIndex();
for(int row=startRow; row<endRow; row++) {
if(lsm.isSelectedIndex(row))
ret += row + ", ";
}
if(lsm.isSelectedIndex(endRow))
ret += endRow;
ret += "}\n";
}
ret += "]";
/*String ret = "";
for (int col=0; col<listSelectionModels.size(); col++) {
ret += "\'"+col+"\'={"+getListSelectionModelAt(col)+"}";
}*/
return ret;
}
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
boolean isAdjusting = e.getValueIsAdjusting();
Log.println("Event for indexes "
+ firstIndex + " - " + lastIndex
+ "; isAdjusting is " + isAdjusting
+ "; selected indexes:");
if (lsm.isSelectionEmpty()) {
Log.println(" <none>");
} else {
// Find out which indexes are selected.
int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
for (int i = minIndex; i <= maxIndex; i++) {
if (lsm.isSelectedIndex(i)) {
Log.println(" " + i);
}
}
}
Log.println("");
}
/**
* Paint one List cell: compute the relevant state, get the "rubber stamp"
* cell renderer component, and then use the CellRendererPane to paint it.
* Subclasses may want to override this method rather than paint().
*
* @see #paint
*/
protected void paintCell(
Graphics g,
int row,
Rectangle rowBounds,
ListCellRenderer cellRenderer,
ListModel dataModel,
ListSelectionModel selModel,
int leadIndex)
{
Object value = dataModel.getElementAt(row);
boolean cellHasFocus = list.hasFocus() && (row == leadIndex);
boolean isSelected = selModel.isSelectedIndex(row);
Component rendererComponent =
cellRenderer.getListCellRendererComponent(list, value, row, isSelected, cellHasFocus);
int cx = rowBounds.x;
int cy = rowBounds.y;
int cw = rowBounds.width;
int ch = rowBounds.height;
rendererPane.paintComponent(g, rendererComponent, list, cx, cy, cw, ch, true);
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty())
return;
if (lsm.equals(jAllGroupsList.getSelectionModel())){
// Due to SINGLE_SELECTION mode we'll break on first match
int minIndex=lsm.getMinSelectionIndex();
int maxIndex=lsm.getMaxSelectionIndex();
for(int i=minIndex; i<=maxIndex; i++){
if (lsm.isSelectedIndex(i)){
Object obj=jAllGroupsList.getModel().getElementAt(i);
if (obj instanceof ObjectGroup){
currentGroup = (ObjectGroup) obj;
updateCurrentGroup();
}
}
}
}
}
public ArrayList<int[]> getSelectedCells() {
ArrayList<int[]> selected = new ArrayList();
TableSelectionModel selection = getTableSelectionModel();
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
//System.out.println("----");
for(int c=0; c<colCount; c++) {
int cc = convertColumnIndexToModel(c);
ListSelectionModel columnSelectionModel = selection.getListSelectionModelAt(cc);
if(columnSelectionModel != null && !columnSelectionModel.isSelectionEmpty()) {
for(int r=0; r<rowCount; r++) {
if(columnSelectionModel.isSelectedIndex(r)) {
selected.add( new int[] { r, c } );
//System.out.println("found cell "+cc+","+r);
}
}
}
}
return selected;
}
@Override
public void valueChanged(ListSelectionEvent e) {
JList<?> list = (JList<?>) e.getSource();
ListModel<?> model = list.getModel();
ListSelectionModel listSelectionModel = list.getSelectionModel();
int minSelectionIndex = listSelectionModel.getMinSelectionIndex();
int maxSelectionIndex = listSelectionModel.getMaxSelectionIndex();
StringBuilder textBuilder = new StringBuilder();
for (int i = minSelectionIndex; i <= maxSelectionIndex; i++) {
if (listSelectionModel.isSelectedIndex(i)) {
Object elementAt = model.getElementAt(i);
formatElement(elementAt, textBuilder, i);
}
}
setText(textBuilder.toString());
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed( final ActionEvent e ) {
if ( isEnabled() == false ) {
return;
}
final DrillDownParameter[] data = parameterTableModel.getGroupedData();
final ListSelectionModel listSelectionModel = table.getSelectionModel();
final ArrayList<DrillDownParameter> result = new ArrayList<DrillDownParameter>( data.length );
for ( int i = 0; i < data.length; i++ ) {
final DrillDownParameter parameter = data[ i ];
if ( parameter == null ) {
continue;
}
if ( listSelectionModel.isSelectedIndex( model.mapFromModel( i ) ) == false
|| parameter.getType() != DrillDownParameter.Type.MANUAL ) {
result.add( data[ i ] );
}
}
parameterTableModel.setData( result.toArray( new DrillDownParameter[ result.size() ] ) );
}
private ArrayList<Integer> getSelectedPlanes() {
final ArrayList<Integer> selected = new ArrayList<>();
final ListSelectionModel lsm = planeList.getSelectionModel();
if (lsm.getMinSelectionIndex() != -1) {
for (int ix = lsm.getMinSelectionIndex(); ix <= lsm.getMaxSelectionIndex(); ix++) {
if (lsm.isSelectedIndex(ix)) {
selected.add(ix);
}
}
}
return selected;
}
public void selectRows() {
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
ListSelectionModel columnSelectionModel = null;
for(int r=0; r<rowCount; r++) {
boolean selectRow = false;
for(int c=0; c<colCount; c++) {
columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
if(columnSelectionModel.isSelectedIndex(r)) {
selectRow = true;
break;
}
}
}
if(selectRow) {
for(int c=0; c<colCount; c++) {
columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
columnSelectionModel.addSelectionInterval(r, r);
}
}
}
}
}
this.repaint();
}
@Override
@SuppressWarnings("unchecked")
public void valueChanged (ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
List<VCSStatusNode> selectedNodes = new ArrayList<VCSStatusNode>();
ListSelectionModel selection = table.getSelectionModel();
final TopComponent tc = (TopComponent) SwingUtilities.getAncestorOfClass(TopComponent.class, table);
int min = selection.getMinSelectionIndex();
if (min != -1) {
int max = selection.getMaxSelectionIndex();
for (int i = min; i <= max; i++) {
if (selection.isSelectedIndex(i)) {
int idx = table.convertRowIndexToModel(i);
selectedNodes.add(tableModel.getNode(idx));
}
}
}
final T[] nodeArray = selectedNodes.toArray((T[]) java.lang.reflect.Array.newInstance(tableModel.getItemClass(), selectedNodes.size()));
Mutex.EVENT.readAccess(new Runnable() {
@Override
public void run() {
File[] selectedFiles = new File[nodeArray.length];
for (int i = 0; i < nodeArray.length; ++i) {
selectedFiles[i] = nodeArray[i].getFile();
}
support.firePropertyChange(PROP_SELECTED_FILES, null, selectedFiles);
if (tc != null) {
tc.setActivatedNodes(nodeArray);
}
}
});
}
public static void reSelect(final ListSelectionModel lsm)
{
final int maxSelectionIndex = lsm.getMaxSelectionIndex();
for (int i = 0; i <= maxSelectionIndex; i++)
if (lsm.isSelectedIndex( i ))
{
lsm.removeSelectionInterval( i, i );
lsm.addSelectionInterval( i, i );
}
}
private void jDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDeleteButtonActionPerformed
//jAllGroupsList.r
ListSelectionModel lsm = jAllGroupsList.getSelectionModel();
if (lsm.isSelectionEmpty())
return;
int minIndex=lsm.getMinSelectionIndex();
int maxIndex=lsm.getMaxSelectionIndex();
//remove listeners temporarily
lsm.removeListSelectionListener(this);
for(int i=minIndex; i<=maxIndex; i++){
if (lsm.isSelectedIndex(i)){
Object obj=jAllGroupsList.getModel().getElementAt(i);
if (obj instanceof ObjectGroup){
ObjectGroup groupToDelete = (ObjectGroup) obj;
// Find an alternate group and make it current
if (jAllGroupsList.getModel().getSize()==1){
throw new UnsupportedOperationException("Not yet implemented");
}
else {
currentGroup=(ObjectGroup) jAllGroupsList.getModel().getElementAt(0);
dSet.removeGroup(groupToDelete.getName());
}
}
}
}
updateGroupsList();
// Restore listeners
lsm.addListSelectionListener(this);
}
Vector<ModelPose> getSelectedPoses() {
Vector<ModelPose> selected = new Vector<ModelPose>(2);
ListSelectionModel selectionModel=jList1.getSelectionModel();
int startIndex=selectionModel.getMinSelectionIndex();
int endIndex=selectionModel.getMaxSelectionIndex();
for(int i=startIndex; i<=endIndex;i++){
if (selectionModel.isSelectedIndex(i))
selected.add((ModelPose)posesList.elementAt(i));
}
return selected;
}
@Override
public void doGridSelection(Wandora admin, TopicGrid grid) {
setDefaultLogger();
setLogTitle("Grid selection info");
int rowsCounter = grid.getRowCount();
int colsCounter = grid.getColumnCount();
//int rowSelectionCounter = 0;
//int colSelectionCounter = 0;
int cellSelectionCounter = 0;
TableSelectionModel selection = grid.getTableSelectionModel();
for(int c=0; c<colsCounter; c++) {
ListSelectionModel columnSelectionModel = selection.getListSelectionModelAt(c);
if(columnSelectionModel != null && !columnSelectionModel.isSelectionEmpty()) {
for(int r=0; r<rowsCounter; r++) {
if(columnSelectionModel.isSelectedIndex(r)) {
cellSelectionCounter++;
}
}
}
}
String message = "Grid contains " + rowsCounter + " rows";
if(colsCounter > 1) message += " and " + (rowsCounter*colsCounter) + " cells.";
else message += ".";
if(cellSelectionCounter == 1) message += "\nSelection contains " + cellSelectionCounter + " cell.";
if(cellSelectionCounter > 1) message += "\nSelection contains " + cellSelectionCounter + " cells.";
log(message);
setState(WandoraToolLogger.WAIT);
}
private void writeHtmlTable(final MBasicTable table, final boolean isSelection,
final Writer out, final String eol) throws IOException {
out.write(
"<table width=\"100%\" border=\"1\" cellspacing=\"0\" bordercolor=\"#000000\" cellpadding=\"2\">");
out.write(eol);
out.write(eol);
out.write(" <tr align=\"center\" class=\"smallFont\">");
out.write(eol);
final int rowCount = table.getRowCount();
final int columnCount = table.getColumnCount();
// titres des colonnes
for (int i = 0; i < columnCount; i++) {
out.write(" <th id=");
out.write(String.valueOf(i));
out.write("> ");
final Object value = table.getColumnModel().getColumn(i).getHeaderValue();
String text = value != null ? value.toString() : "";
text = formatHtml(text);
out.write(text);
out.write(" </th>");
out.write(eol);
}
out.write(" </tr>");
out.write(eol);
// les données proprement dites (ligne par ligne puis colonne par colonne)
final ListSelectionModel selectionModel = table.getSelectionModel();
for (int k = 0; k < rowCount; k++) {
if (isSelection && !selectionModel.isSelectedIndex(k)) {
continue;
}
out.write(eol);
out.write(" <tr id=");
out.write(String.valueOf(k));
out.write(" class=\"smallFont\">");
out.write(eol);
for (int i = 0; i < columnCount; i++) {
writeHtmlTd(table, out, eol, k, i);
}
out.write(" </tr>");
out.write(eol);
}
out.write(eol);
out.write("</table>");
out.write(eol);
}
/**
* @return true, if the specified cell is selected.
*/
public boolean isSelected(int row, int column) {
ListSelectionModel lsm = getListSelectionModelAt(column);
if(lsm != null) return lsm.isSelectedIndex(row);
return false;
}
/**
* Method save
*/
public void save() {
JFileChooser dialog = new JFileChooser(".");
int rc = dialog.showSaveDialog(this);
if (rc == JFileChooser.APPROVE_OPTION) {
try {
File file = dialog.getSelectedFile();
FileOutputStream out = new FileOutputStream(file);
ListSelectionModel lsm =
connectionTable.getSelectionModel();
rc = lsm.getLeadSelectionIndex();
int n = 0;
for (Iterator i = connections.iterator(); i.hasNext();
n++) {
Connection conn = (Connection) i.next();
if (lsm.isSelectedIndex(n + 1)
|| (!(i.hasNext())
&& (lsm.getLeadSelectionIndex() == 0))) {
rc = Integer.parseInt(portField.getText());
out.write("\n==============\n".getBytes());
out.write(((TCPMon.getMessage("listenPort01",
"Listen Port:")
+ " " + rc + "\n")).getBytes());
out.write((TCPMon.getMessage("targetHost01",
"Target Host:")
+ " " + hostField.getText()
+ "\n").getBytes());
rc = Integer.parseInt(tPortField.getText());
out.write(((TCPMon.getMessage("targetPort01",
"Target Port:")
+ " " + rc + "\n")).getBytes());
out.write((("==== "
+ TCPMon.getMessage("request01", "Request")
+ " ====\n")).getBytes());
out.write(conn.inputText.getText().getBytes());
out.write((("==== "
+ TCPMon.getMessage("response00", "Response")
+ " ====\n")).getBytes());
out.write(conn.outputText.getText().getBytes());
out.write("\n==============\n".getBytes());
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}