javax.swing.JTable#setValueAt ( )源码实例Demo

下面列出了javax.swing.JTable#setValueAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Reads clipboard data and converts it into supported format and fills the
 * tmodel cells
 *
 * @param table the target tmodel
 */
private static void pasteFromClipboard(JTable table) {
    int startRow = table.getSelectedRows()[0];
    int startCol = table.getSelectedColumns()[0];
    String pasteString;
    try {
        pasteString = (String) (CLIPBOARD.getContents(CLIPBOARD).getTransferData(DataFlavor.stringFlavor));
    } catch (UnsupportedFlavorException | IOException ex) {
        Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }
    String[] lines = pasteString.split(LINE_BREAK);
    for (int i = 0; i < lines.length; i++) {
        String[] cells = lines[i].split(CELL_BREAK);
        if (table.getRowCount() <= startRow + i) {
            ((DefaultTableModel) table.getModel()).addRow(nullRow);
        }
        for (int j = 0; j < cells.length; j++) {
            if (table.getColumnCount() > startCol + j) {
                if (table.isCellEditable(startRow + i, startCol + j)) {
                    table.setValueAt(cells[j], startRow + i, startCol + j);
                }
            }
        }
    }
}
 
private static AbstractAction getEncryptAction(final JTable table) {
    return new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent me) {
            try {
                int col = table.getSelectedColumn();
                int row = table.getSelectedRow();
                if (col > -1 && row > -1) {
                    String data = table.getValueAt(row, col).toString();
                    table.setValueAt(TMIntegration.encrypt(data), row, col);
                }
            } catch (HeadlessException ex) {
                Logger.getLogger(TMSettingsControl.class.getName())
                        .log(Level.SEVERE, ex.getMessage(), ex);
            }

        }
    };
}
 
源代码3 项目: snap-desktop   文件: MosaicExpressionsPanel.java
private MouseListener createExpressionEditorMouseListener(final JTable table, final boolean booleanExpected) {
    final MouseAdapter mouseListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                final int column = table.getSelectedColumn();
                if (column == 1) {
                    table.removeEditor();
                    final int row = table.getSelectedRow();
                    final String[] value = new String[]{(String) table.getValueAt(row, column)};
                    final int i = editExpression(value, booleanExpected);
                    if (ModalDialog.ID_OK == i) {
                        table.setValueAt(value[0], row, column);
                    }
                }
            }
        }
    };
    return MouseEventFilterFactory.createFilter(mouseListener);
}
 
public static void pasteFromAbove(JTable table) {
    int startRow = table.getSelectedRows()[0];
    int[] cols = table.getSelectedColumns();
    for (int col : cols) {
        table.setValueAt(table.getValueAt(startRow - 1, col), startRow, col);
    }
}
 
/**
 * clear selection by setting empty values
 *
 * @param table to be cleared
 */
private static void ClearSelection(JTable table) {
    int[] srow = table.getSelectedRows();
    int[] scol = table.getSelectedColumns();
    int lastSrow = srow.length;
    int lastScol = scol.length;
    for (int i = 0; i < lastSrow; i++) {
        for (int j = 0; j < lastScol; j++) {
            if (table.isCellEditable(srow[i], scol[j])) {
                table.setValueAt("", srow[i], scol[j]);
            }
        }
    }
}
 
/**
 * Reads the cell values of selected cells of the <code>tmodel</code> and
 * uploads into clipboard in supported format
 *
 * @param isCut CUT flag,<code>true</code> for CUT and <code>false</code>
 * for COPY
 * @param table the source for the action
 * @see #escape(java.lang.Object)
 */
private static void copyToClipboard(boolean isCut, JTable table) {
    try {
        int numCols = table.getSelectedColumnCount();
        int numRows = table.getSelectedRowCount();
        int[] rowsSelected = table.getSelectedRows();
        int[] colsSelected = table.getSelectedColumns();
        if (numRows != rowsSelected[rowsSelected.length - 1] - rowsSelected[0] + 1 || numRows != rowsSelected.length
                || numCols != colsSelected[colsSelected.length - 1] - colsSelected[0] + 1 || numCols != colsSelected.length) {
            JOptionPane.showMessageDialog(null, "Invalid Selection", "Invalid Selection", JOptionPane.ERROR_MESSAGE);
            return;
        }
        StringBuilder excelStr = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                excelStr.append(escape(table.getValueAt(rowsSelected[i], colsSelected[j])));
                if (isCut) {
                    if (table.isCellEditable(rowsSelected[i], colsSelected[j])) {
                        table.setValueAt("", rowsSelected[i], colsSelected[j]);
                    }
                }
                if (j < numCols - 1) {
                    excelStr.append(CELL_BREAK);
                }
            }
            if (i < numRows - 1) {
                excelStr.append(LINE_BREAK);
            }
        }
        if (!excelStr.toString().isEmpty()) {
            StringSelection sel = new StringSelection(excelStr.toString());
            CLIPBOARD.setContents(sel, sel);
        }

    } catch (HeadlessException ex) {
        Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
public static void emptyTable(JTable table, int column) {
    for (int i = 0; i < table.getRowCount(); i++) {
        for (int j = 0; j < table.getColumnCount(); j++) {
            try {
                if (column != j) {
                    table.setValueAt(null, i, j);
                }
            } catch (Exception ex) {
                Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
 
源代码8 项目: snap-desktop   文件: MosaicExpressionsPanel.java
private static void addRow(final JTable table, final Object[] rowData) {
    table.removeEditor();
    ((DefaultTableModel) table.getModel()).addRow(rowData);
    final int row = table.getRowCount() - 1;
    final int numCols = table.getColumnModel().getColumnCount();
    for (int i = 0; i < Math.min(numCols, rowData.length); i++) {
        Object o = rowData[i];
        table.setValueAt(o, row, i);
    }
    selectRows(table, row, row);
}
 
public static void copyToClipboard(JTable table, boolean isCut) {
    int numCols = table.getSelectedColumnCount();
    int numRows = table.getSelectedRowCount();
    int[] rowsSelected = table.getSelectedRows();
    int[] colsSelected = table.getSelectedColumns();
    if (numRows != rowsSelected[rowsSelected.length - 1] - rowsSelected[0] + 1 || numRows != rowsSelected.length
            || numCols != colsSelected[colsSelected.length - 1] - colsSelected[0] + 1 || numCols != colsSelected.length) {

        Logger.getLogger(XTableUtils.class.getName()).info("Invalid Copy Selection");
        return;
    }
    if (table.getModel() instanceof UndoRedoModel) {
        ((UndoRedoModel) table.getModel()).startGroupEdit();
    }

    StringBuilder excelStr = new StringBuilder();

    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            excelStr.append(escape(table.getValueAt(rowsSelected[i], colsSelected[j])));
            if (isCut) {
                table.setValueAt("", rowsSelected[i], colsSelected[j]);
            }
            if (j < numCols - 1) {
                excelStr.append(CELL_BREAK);
            }
        }
        excelStr.append(LINE_BREAK);
    }

    if (table.getModel() instanceof UndoRedoModel) {
        ((UndoRedoModel) table.getModel()).stopGroupEdit();
    }

    StringSelection sel = new StringSelection(excelStr.toString());

    CLIPBOARD.setContents(sel, sel);
}
 
public static void pasteFromClipboard(JTable table) {
    int startRow = table.getSelectedRows()[0];
    int startCol = table.getSelectedColumns()[0];

    String pasteString;
    try {
        pasteString = (String) (CLIPBOARD.getContents(null).getTransferData(DataFlavor.stringFlavor));
    } catch (Exception e) {
        Logger.getLogger(XTableUtils.class.getName()).log(Level.WARNING, "Invalid Paste Type", e);
        return;
    }
    if (table.getModel() instanceof UndoRedoModel) {
        ((UndoRedoModel) table.getModel()).startGroupEdit();
    }
    String[] lines = pasteString.split(LINE_BREAK);

    for (int i = 0; i < lines.length; i++) {
        String[] cells = lines[i].split(CELL_BREAK);
        for (int j = 0; j < cells.length; j++) {
            if (table.getRowCount() <= startRow + i) {
                if (table.getModel() instanceof DataModel) {
                    if (!((DataModel) table.getModel()).addRow()) {
                        return;
                    }
                }
            }
            if (table.getRowCount() > startRow + i && table.getColumnCount() > startCol + j) {
                table.setValueAt(cells[j], startRow + i, startCol + j);
            }
        }
    }
    if (table.getModel() instanceof UndoRedoModel) {
        ((UndoRedoModel) table.getModel()).stopGroupEdit();
    }
}
 
@Override
public void mouseReleased(MouseEvent e) {
    if (startLocation != null && isInDragOperation) {
        Object s = e.getSource();
        JTable t = (JTable) s;
        for (Integer[] index : rowsRColumns) {
            t.setValueAt(startLocation.getData(), index[0], index[1]);
        }
        startLocation = null;
    }
    rowsRColumns.clear();
    isInDragOperation = false;
}
 
private void putTestData(JTable table, int row) {
    TestCase testCase = (TestCase) table.getModel();
    testCase.startGroupEdit();
    TestDataDetail td = (TestDataDetail) dropObject;
    for (String col : td.getColumnNames()) {
        if (row > table.getRowCount() - 1) {
            testCase.addNewStep();
        }
        table.setValueAt(td.getSheetName() + ":" + col, row++, inputColumn);
    }
    testCase.stopGroupEdit();
}
 
源代码13 项目: Repeat   文件: SwingUtil.java
public static void clearTable(JTable table) {
	for (int i = 0; i < table.getRowCount(); i++) {
		for (int j = 0; j < table.getColumnCount(); j++) {
			table.setValueAt("", i, j);
		}
	}
}
 
源代码14 项目: Repeat   文件: SwingUtil.java
public static void clearSelectedTable(JTable table) {
	int[] columns = table.getSelectedColumns();
	int[] rows = table.getSelectedRows();

	for (int i = 0; i < rows.length; i++) {
		for (int j = 0; j < columns.length; j++) {
			table.setValueAt("", rows[i], columns[j]);
		}
	}
}
 
源代码15 项目: snap-desktop   文件: VariablesTableAdapterTest.java
@Test
public void variablesProperty() {
    final JTable table = new JTable();
    bindingContext.bind("variables", new VariablesTableAdapter(table));
    assertTrue(table.getModel() instanceof DefaultTableModel);

    final DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    tableModel.addRow(new String[]{"", ""});
    tableModel.addRow(new String[]{"", ""});

    assertEquals(2, table.getRowCount());

    table.setValueAt("a", 0, 0);
    assertEquals("a", table.getValueAt(0, 0));
    table.setValueAt("A", 0, 1);
    assertEquals("A", table.getValueAt(0, 1));

    table.setValueAt("b", 1, 0);
    assertEquals("b", table.getValueAt(1, 0));
    table.setValueAt("B", 1, 1);
    assertEquals("B", table.getValueAt(1, 1));

    bindingContext.getPropertySet().setValue("variables", new MosaicOp.Variable[]{
            new MosaicOp.Variable("d", "D")
    });

    assertEquals(1, table.getRowCount());
    assertEquals("d", table.getValueAt(0, 0));
    assertEquals("D", table.getValueAt(0, 1));
}
 
源代码16 项目: jts   文件: GuiUtil.java
public static void commitChanges(JTable table) {
    if (table.isEditing()) {
        String text = ((JTextComponent) table.getEditorComponent()).getText();
        table.setValueAt(text, table.getEditingRow(), table.getEditingColumn());
        table.getCellEditor().cancelCellEditing();
    }
}
 
源代码17 项目: Zettelkasten   文件: ZettelkastenViewUtil.java
/**
 * This method updates a jTable and a possible linked list which holds
 * filtered values from the jTables, by increasing ({@code diff} must be 1)
 * or decreasing ({@code diff} must be -1) an entry's occurences or
 * frequencies from the tablemodel and the linked list.
 * <br><br>
 * If no increase or decrease of frequencies (occurences) is requested, but
 * a complete removal, call
 * {@link #updateTableFrequencyRemove(javax.swing.JTable, java.util.LinkedList) updateTableFrequencyRemove(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 value the new value, for instance the author-string or
 * keyword-value
 * @param diff either +1, if a value was added, so frequency is increased by
 * 1. or -1, if a value was removed, so frequency is decreaded.
 * @return an updated linked list that was passed as parameter {@code list}
 */
public static LinkedList<Object[]> updateTableFrequencyChange(JTable table, LinkedList<Object[]> list, String value, int diff) {
    // iterate all table rows
    for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
        // check whether we have found the value that should be changed
        if (value.equals(table.getValueAt(cnt, 0).toString())) {
            // retrieve table data
            Object[] o = new Object[2];
            o[0] = table.getValueAt(cnt, 0);
            o[1] = table.getValueAt(cnt, 1);
            // convert frquency-counter to int
            int freq = Integer.parseInt(table.getValueAt(cnt, 1).toString());
            // set new value
            table.setValueAt(freq + diff, cnt, 1);
            // check whether we have a filtered list
            if (list != null) {
                // if so, iterate list
                for (int pos = 0; pos < list.size(); pos++) {
                    Object[] v = list.get(pos);
                    // check whether we have found the value that should be changed
                    if (o[0].toString().equals(v[0].toString())) {
                        // change frequency
                        o[1] = freq + diff;
                        list.set(pos, o);
                        break;
                    }
                }
            }
        }
    }
    return list;
}
 
源代码18 项目: HBaseClient   文件: EditSubmitActionListener.java
@Override
public void onClick(ActionEvent arg0)
{
    HData v_HData = new HData();
    
    v_HData.setRowKey(    ((JTextComponent)XJava.getObject("Edit_RowKey"))     .getText().trim());
    v_HData.setFamilyName(((JComboBox)     XJava.getObject("Edit_FamilyName")) .getSelectedItem().toString().trim());
    v_HData.setColumnName(((JComboBox)     XJava.getObject("Edit_ColumnName")) .getSelectedItem().toString().trim());
    v_HData.setValue(     ((JTextComponent)XJava.getObject("Edit_ColumnValue")).getText().trim());
    
    if ( JavaHelp.isNull(v_HData.getRowKey()) )
    {
        this.getAppFrame().showHintInfo("提交时,行主键不能为空!" ,Color.RED);
        ((JComponent)XJava.getObject("Edit_RowKey")).requestFocus();
        return;
    }
    
    if ( JavaHelp.isNull(v_HData.getFamilyName()) )
    {
        this.getAppFrame().showHintInfo("提交时,列族名不能为空!" ,Color.RED);
        ((JComponent)XJava.getObject("Edit_FamilyName")).requestFocus();
        return;
    }
    
    if ( JavaHelp.isNull(v_HData.getColumnName()) )
    {
        this.getAppFrame().showHintInfo("提交时,字段名不能为空!" ,Color.RED);
        ((JComponent)XJava.getObject("Edit_ColumnName")).requestFocus();
        return;
    }
    
    try
    {
        HData v_OldHData = (HData)this.getHBase().getValue(this.getTableName() ,v_HData);
        
        this.getHBase().update(this.getTableName() ,v_HData);
        
        // 重新从数据库是查询一次,主要想获取时间戳
        HData v_NewHData = (HData)this.getHBase().getValue(this.getTableName() ,v_HData);
        
        JTable  v_JTable  = (JTable)XJava.getObject("xtDataList");
        int     v_RowNo   = v_JTable.getSelectedRow();
        int     v_OptType = 1; // 操作类型(0:修改   1:添加)
        
        if ( v_JTable.getSelectedRowCount() == 1 )
        {
            if ( v_NewHData.getRowKey().equals(v_JTable.getValueAt(v_RowNo ,1)) )
            {
                if ( v_NewHData.getFamilyName().equals(v_JTable.getValueAt(v_RowNo ,2)) )
                {
                    if ( v_NewHData.getColumnName().equals(v_JTable.getValueAt(v_RowNo ,3)) )
                    {
                        v_OptType = 0;
                    }
                }
            }
        }
        
        if ( v_OptType == 0 )
        {
            v_JTable.setValueAt(v_NewHData.getValue().toString()    ,v_RowNo ,4);
            v_JTable.setValueAt(v_NewHData.getTimestamp()           ,v_RowNo ,5);
            v_JTable.setValueAt(v_NewHData.getTime().getFullMilli() ,v_RowNo ,6);
            this.getAppFrame().showHintInfo("修改完成!" ,Color.BLUE);
        }
        else
        {
            this.getAppFrame().setRowCount(this.getAppFrame().getRowCount() + 1);
            this.getAppFrame().getTableModel().addRow(SubmitActionListener.$MY.toObjects(this.getAppFrame().getRowCount() ,v_NewHData));
            
            if ( v_OldHData != null )
            {
                this.getAppFrame().showHintInfo("修改完成,请刷新查询结果。" ,Color.BLUE);
            }
            else
            {
                this.getAppFrame().showHintInfo("添加完成!" ,Color.BLUE);
            }
        }
        
    }
    catch (Exception exce)
    {
        this.getAppFrame().showHintInfo("修改异常:" + exce.getMessage() ,Color.RED);
    }
}
 
private void putRelativeObject(JTable table, int row) {
    String val = ((ObjectRepDnD) dropObject).getObjectName(((ObjectRepDnD) dropObject).getValues().get(0));
    if (val != null) {
        table.setValueAt(val, row, conditionColumn);
    }
}
 
private void putInput(JTable table, int row) {
    table.setValueAt("@" + ((ObjectRepDnD) dropObject).getPageName(((ObjectRepDnD) dropObject).getValues().get(0)), row, inputColumn);
}
 
 方法所在类
 同类方法