Java Code Examples for javax.swing.JTable#convertRowIndexToModel()

The following examples show how to use javax.swing.JTable#convertRowIndexToModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JTableJavaElement.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private void validate(int viewRow, int viewCol) {
	JTable table = (JTable) getComponent();
	try {
		int row = table.convertRowIndexToModel(viewRow);
		int col = table.convertColumnIndexToModel(viewCol);
		TableModel model = table.getModel();
		if (row >= 0 && row < model.getRowCount() && col >= 0 && col < model.getColumnCount()) {
			if (table.isCellEditable(viewRow, viewCol)) {
				return;
			} else {
				throw new NoSuchElementException(
						"The cell is not editable on JTable: (" + viewRow + ", " + viewCol + ")", null);
			}
		}
	} catch (IndexOutOfBoundsException e) {
	}
	throw new NoSuchElementException("Invalid row/col for JTable: (" + viewRow + ", " + viewCol + ")", null);
}
 
Example 2
Source File: GenericTableModel.java    From armitage with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Object[] getSelectedValues(JTable t) {
	synchronized (this) {
		int row[] = t.getSelectedRows();
		Object[] rv = new Object[row.length];

		for (int x = 0; x < row.length; x++) {
			int r = t.convertRowIndexToModel(row[x]);
			if (r < rows.size() && r >= 0)
				rv[x] = ( (Map)rows.get(r) ).get(leadColumn);
			else
				rv[x] = null;
		}

		return rv;
	}
}
 
Example 3
Source File: ZettelkastenViewUtil.java    From Zettelkasten with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method updates a jTable and a possible linked list which holds
 * filtered values from the jTables, by completely removing an entry/value
 * from the tablemodel and the linked list.
 * <br><br>
 * If no complete removal is requested, but a decrease in the frequencies,
 * call
 * {@link #updateTableFrequencyDelete(javax.swing.JTable, java.util.LinkedList) updateTableFrequencyDelete(javax.swing.JTable, java.util.LinkedList)}
 * instead.
 *
 * @param table the table were we have to add a new value with frequency
 * @param list the possible linked list were we have to add a new value with
 * frequency
 * @param zettelkastenView
 * @return an updated linked list that was passed as parameter {@code list}
 */
public static LinkedList<Object[]> updateTableFrequencyRemove(JTable table, LinkedList<Object[]> list, ZettelkastenView zettelkastenView) {
    // get table model
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    // retrieve selected rows
    int[] rows = table.getSelectedRows();
    for (int cnt = rows.length - 1; cnt >= 0; cnt--) {
        try {
            int selectedrow = table.convertRowIndexToModel(rows[cnt]);
            if (list != null) {
                Object[] o = new Object[2];
                o[0] = dtm.getValueAt(selectedrow, 0);
                o[1] = dtm.getValueAt(selectedrow, 1);
                int pos = findInLinkedList(list, o);
                if (pos != -1) {
                    list.remove(pos);
                }
            }
            dtm.removeRow(selectedrow);
        } catch (ArrayIndexOutOfBoundsException e) {
            Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
        }
    }
    return list;
}
 
Example 4
Source File: GamedataEditorRenderFactory.java    From gameserver with Apache License 2.0 6 votes vote down vote up
@Override
public TableCellEditor getCellEditor(int row, int column, 
		String columnName, TableModel tableModel, JTable table) {
	if ( "value".equals(columnName) || "default".equals(columnName) ) {
		int modelColIndex = table.convertColumnIndexToModel(column);
		int modelRowIndex = table.convertRowIndexToModel(row);
		Object value =  this.tableModel.getValueAt(modelRowIndex, modelColIndex);
		if ( value instanceof BasicDBList ) {
			MongoDBArrayEditor editor =  new MongoDBArrayEditor();
			editor.setDBObject((BasicDBList)value);
			return editor;
		} else {
			JXTextField field = new JXTextField(value.toString());
			field.setPreferredSize(new Dimension(100, 36));
			field.setBorder(null);
			return new DefaultCellEditor(field);
		}
	}
	return null;
}
 
Example 5
Source File: TableUtils.java    From IrScrutinizer with GNU General Public License v3.0 5 votes vote down vote up
void printTableSelectedRows(JTable table) {
    NamedIrSignal.LearnedIrSignalTableModel tableModel = (NamedIrSignal.LearnedIrSignalTableModel) table.getModel();
    int[] selected = table.getSelectedRows();
    for (int selectedRow : selected) {
        int modelRow = table.convertRowIndexToModel(selectedRow);
        String str = tableModel.toPrintString(modelRow);
        guiUtils.message(str);
    }
}
 
Example 6
Source File: GenericTableModel.java    From armitage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object getValueAtColumn(JTable t, int row, String col) {
	synchronized (this) {
		row = t.convertRowIndexToModel(row);

		Map temp = (Map)rows.get(row);
		return temp.get(col);
	}
}
 
Example 7
Source File: GenericTableModel.java    From armitage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object getValueAt(JTable t, int row, String column) {
	synchronized (this) {
		row = t.convertRowIndexToModel(row);
		if (row == -1)
			return null;

		return ( (Map)rows.get(row) ).get(column);
	}
}
 
Example 8
Source File: GenericTableModel.java    From armitage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object[][] getSelectedValuesFromColumns(JTable t, String cols[]) {
	synchronized (this) {
		int row[] = t.getSelectedRows();
		Object[][] rv = new Object[row.length][cols.length];

		for (int x = 0; x < row.length; x++) {
			int r = t.convertRowIndexToModel(row[x]);
			for (int y = 0; y < cols.length; y++) {
				rv[x][y] = ( (Map)rows.get(r) ).get(cols[y]);
			}
		}

		return rv;
	}
}
 
Example 9
Source File: MyAbstractCellEditor.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
		boolean isSelected, int row, int column) {
	int modelRow    = table.convertRowIndexToModel(row);
	int modelColumn = table.convertColumnIndexToModel(column);
	return getTableCellEditorComponentAtModel(table, value, isSelected, modelRow, modelColumn);
}
 
Example 10
Source File: CSVFileWriter.java    From DiskBrowser with GNU General Public License v3.0 5 votes vote down vote up
static void write (DiskTableModel diskTableModel, JTable table)
// ---------------------------------------------------------------------------------//
{
  String csvFile =
      System.getProperty ("user.home") + File.separator + "DiskBrowser.csv";

  FileWriter writer;

  JFileChooser fileChooser = new JFileChooser ();
  fileChooser.setSelectedFile (new File (csvFile));
  int returnValue = fileChooser.showSaveDialog (null);
  if (returnValue != JFileChooser.APPROVE_OPTION)
    return;

  File selectedFile = fileChooser.getSelectedFile ();

  try
  {
    writer = new FileWriter (selectedFile);
    writer.append (String
        .format ("Path,Name,Type,Size,Duplicate Name, Duplicate Data, Checksum%n"));

    for (int i = 0; i < table.getRowCount (); i++)
    {
      int actualRow = table.convertRowIndexToModel (i);
      String line = diskTableModel.getCSV (actualRow);
      writer.append (line);
    }

    writer.flush ();
    writer.close ();
  }
  catch (IOException e)
  {
    e.printStackTrace ();
  }
}
 
Example 11
Source File: TableUtils.java    From IrScrutinizer with GNU General Public License v3.0 5 votes vote down vote up
public List<Integer> modelLinesSelected(JTable table) {
    int[] selected = table.getSelectedRows();
    List<Integer> result = new ArrayList<>(selected.length);
    for (int selectedRow : selected) {
        int modelRow = table.convertRowIndexToModel(selectedRow);
        result.add(modelRow);
    }
    return result;
}
 
Example 12
Source File: TableUtils.java    From IrScrutinizer with GNU General Public License v3.0 5 votes vote down vote up
void duplicateTableSelectedRow(JTable table) throws ErroneousSelectionException, GirrException {
    barfIfNotExactlyOneSelected(table);
    int selectedRow = table.getSelectedRow();
    int modelRow = table.convertRowIndexToModel(selectedRow);
    NamedIrSignal.LearnedIrSignalTableModel tableModel = (NamedIrSignal.LearnedIrSignalTableModel) table.getModel();
    tableModel.duplicate(modelRow);
}
 
Example 13
Source File: UITools.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public static <T> List<T> getTableSelections(JTable tableCards,int columnID) {
	int[] viewRow = tableCards.getSelectedRows();
	List<T> listCards = new ArrayList<>();
	for (int i : viewRow) {
		int modelRow = tableCards.convertRowIndexToModel(i);
		T mc = (T) tableCards.getModel().getValueAt(modelRow, columnID);
		listCards.add(mc);
	}
	return listCards;
}
 
Example 14
Source File: GUIUtils.java    From open-ig with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Converts the selected view indexes to model indexes.
 * @param table the table
 * @return the selected model indices
 */
public static int[] convertSelectionToModel(JTable table) {
	int[] selected = table.getSelectedRows();
	for (int i = 0; i < selected.length; i++) {
		selected[i] = table.convertRowIndexToModel(selected[i]);
	}
	return selected;
}
 
Example 15
Source File: StringTableCellEditor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, final int row, final int column) {
    final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column);      
    
    this.tableModel = table.getModel();
    this.columnName = table.getColumnName(column);
    this.modelRow = table.convertRowIndexToModel(row);
    this.modelColumn = table.convertColumnIndexToModel(column);  
    this.tc = c instanceof JTextComponent ? (JTextComponent) c : null;

    JPanel panel = new JPanel(new BorderLayout()) {
        @Override
        public void addNotify() {
            super.addNotify();
            c.requestFocus();
        }
    };
    panel.add(c);
    if (suppressEditorBorder) {
        c.setBorder(BorderFactory.createEmptyBorder());
    }
    panel.add(customEditorButton, BorderLayout.EAST);
    panel.revalidate();
    panel.repaint();

    return panel;
}
 
Example 16
Source File: VCSStatusTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public T getPreviousNode (T node) {
    T prev = null;
    if (node != null) {
        int index = Arrays.asList(tableModel.getNodes()).indexOf(node);
        if (index >= 0) {
            JTable table = getTable();
            index = table.convertRowIndexToView(index);
            if (--index >= 0) {
                prev = tableModel.getNodes()[table.convertRowIndexToModel(index)];
            }
        }
    }
    return prev;
}
 
Example 17
Source File: VCSStatusTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public T getNextNode (T node) {
    T next = null;
    if (node != null) {
        int index = Arrays.asList(tableModel.getNodes()).indexOf(node);
        if (index >= 0) {
            JTable table = getTable();
            index = table.convertRowIndexToView(index);
            if (++index < table.getRowCount()) {
                next = tableModel.getNodes()[table.convertRowIndexToModel(index)];
            }
        }
    }
    return next;
}
 
Example 18
Source File: InventoryDialog.java    From WorldGrower with GNU General Public License v3.0 4 votes vote down vote up
private InventoryItem getSelectedValue(JTable inventoryTable) {
	int selectedRow = inventoryTable.convertRowIndexToModel(inventoryTable.getSelectedRow());
	InventoryModel inventoryModel = (InventoryModel) inventoryTable.getModel();
	return inventoryModel.getInventoryItem(selectedRow);
}
 
Example 19
Source File: GenericTableModel.java    From armitage with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public int getSelectedRow(JTable t) {
	synchronized (this) {
		return t.convertRowIndexToModel(t.getSelectedRow());
	}
}
 
Example 20
Source File: FlowExtension.java    From burp-flow with MIT License 4 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    int modelRow = table.convertRowIndexToModel(row);
    FlowEntry entry = flow.get(modelRow);
    if (ThemeHelper.isDarkTheme() == false) {
        // c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
        //if (!isSelected && tempy != null && flowTable.convertRowIndexToModel(row) == flow.indexOf(tempy)) {
        //    c.setForeground(Color.blue);
        //} else {
        //    c.setForeground(Color.black);
        //}
        int r = 0, g = 0, b = 0;
        if (entry.status == 404 || entry.status == 403) {
            r += 196;
            g += 128;
            b += 128;
        }
        if (entry.status == 500) {
            g += 196;
        }
        if (entry.toolFlag != IBurpExtenderCallbacks.TOOL_PROXY) {
            b += 196;
        }
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        c.setForeground(new Color(r, g, b));

        c.setBackground(ThemeHelper.cellBackground(table.getRowCount(), row, isSelected));
    }

    final ArrayList<String> reflections = new ArrayList<>();
    final ArrayList<IParameter> allReflections = entry.getReflections();
    int reflectionsAmount = allReflections.size();
    if (reflectionsAmount > showReflectionsCount) {
        reflectionsAmount = showReflectionsCount;
    }
    if (allReflections.size() > 0) {
        for (IParameter reflection : allReflections.subList(0, reflectionsAmount)) {
            String param = reflection.getName();
            if (param.length() > 50) {
                param = param.substring(0, 50 - 3) + "...";
            }
            String val = reflection.getValue();
            if (val.length() > 50) {
                val = val.substring(0, 50 - 3) + "...";
            }
            reflections.add(new StringBuilder(" &nbsp; &nbsp; (").append(paramType(reflection.getType())).append(") ").append(param).append("=").append(val).toString());
        }
        if (allReflections.size() > showReflectionsCount) {
            reflections.add("...");
        }
        ((JLabel) c).setToolTipText(new StringBuilder("<html>Reflections:<br/>").append(String.join("<br/>", reflections)).append("</html>").toString());
    } else {
        ((JLabel) c).setToolTipText(null);
    }

    return c;
}