Example usage for javax.swing.table TableModel getValueAt

List of usage examples for javax.swing.table TableModel getValueAt

Introduction

In this page you can find the example usage for javax.swing.table TableModel getValueAt.

Prototype

public Object getValueAt(int rowIndex, int columnIndex);

Source Link

Document

Returns the value for the cell at columnIndex and rowIndex.

Usage

From source file:au.com.jwatmuff.eventmanager.export.CSVExporter.java

public static void generateFromTable(TableModel tm, OutputStream out) {
    int rows = tm.getRowCount();
    int cols = tm.getColumnCount();
    Object[][] table = new Object[rows + 1][cols];
    for (int j = 0; j < cols; j++)
        table[0][j] = tm.getColumnName(j);
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            table[i + 1][j] = tm.getValueAt(i, j);

    generateFromArray(table, out);/*from  ww  w .java 2 s.  c  o  m*/
}

From source file:Main.java

/**
 * Save the contents of a table to a TSV file
 * Note:  uses toString() on the header cells as well as the data cells.  If you've got funny columns,
 * expect funny behavior//from   w w  w  . j  a v  a 2s . co  m
 * @param table
 * @param outFile
 * @throws IOException
 */
public static void SaveTableAsTSV(JTable table, File outFile) throws IOException {
    PrintWriter outPW = new PrintWriter(outFile);

    TableModel tableModel = table.getModel();
    TableColumnModel columnModel = table.getColumnModel();

    StringBuffer headerLineBuf = new StringBuffer();
    for (int i = 0; i < columnModel.getColumnCount(); i++) {
        if (i > 0)
            headerLineBuf.append("\t");
        headerLineBuf.append(columnModel.getColumn(i).getHeaderValue().toString());
    }
    outPW.println(headerLineBuf.toString());
    outPW.flush();
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        StringBuffer lineBuf = new StringBuffer();
        for (int j = 0; j < tableModel.getColumnCount(); j++) {
            if (j > 0)
                lineBuf.append("\t");
            lineBuf.append(tableModel.getValueAt(i, j).toString());
        }
        outPW.println(lineBuf.toString());
        outPW.flush();
    }
    outPW.close();
}

From source file:Main.java

/**
 * Adjusts the widths and heights of the cells of the supplied table to fit their contents.
 */// www  .j a v  a2s  .co m
public static void sizeToContents(JTable table) {
    TableModel model = table.getModel();
    TableColumn column = null;
    Component comp = null;
    int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0;

    for (int cc = 0; cc < ccount; cc++) {
        int headerWidth = 0, cellWidth = 0;
        column = table.getColumnModel().getColumn(cc);
        try {
            comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(),
                    false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
        } catch (NullPointerException e) {
            // getHeaderRenderer() this doesn't work in 1.3
        }

        for (int rr = 0; rr < rcount; rr++) {
            Object cellValue = model.getValueAt(rr, cc);
            comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table,
                    cellValue, false, false, 0, cc);
            Dimension psize = comp.getPreferredSize();
            cellWidth = Math.max(psize.width, cellWidth);
            cellHeight = Math.max(psize.height, cellHeight);
        }
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }

    if (cellHeight > 0) {
        table.setRowHeight(cellHeight);
    }
}

From source file:com.cch.aj.entryrecorder.frame.CheckJFrame.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    boolean result = true;
    int count = this.tblCheck.getRowCount();
    TableModel model = tblCheck.getModel();
    for (int i = 0; i < count; i++) {
        Boolean isChecked = (Boolean) model.getValueAt(i, 1);
        if (!isChecked) {
            result = false;/*from   w w w  .  j  a  va  2  s. c  om*/
            break;
        }
    }

    if (result) {
        AppHelper.currentEntry.setIsChecked(true);
        this.entryService.UpdateEntity(AppHelper.currentEntry);
        this.setVisible(false);
        this.dispose();
    } else {
        JOptionPane.showMessageDialog(this, "Please check all items", "Warming", JOptionPane.OK_OPTION);
    }
}

From source file:com.aw.swing.mvp.cmp.RowSelectorMediator.java

/**
 * Select(highlight) a row in a table//w ww. j  a  va  2s  . c  o m
 *
 * @param index
 */
private void selectTableRow(int index) {
    Rectangle rect = table.getCellRect(index, columnIndex, true);
    table.scrollRectToVisible(rect);
    table.clearSelection();
    table.setRowSelectionInterval(index, index);
    TableModel m = table.getModel();
    String text = (String) m.getValueAt(table.getSelectedRow(), columnIndex);
    textField.getDocument().removeDocumentListener(documentListener);
    textField.setText(text);
    textField.getDocument().addDocumentListener(documentListener);

    int beginPos = 0;
    int endPos = text.length();
    if (caretAtBeginning) {
        textField.setCaretPosition(endPos);
        textField.moveCaretPosition(beginPos);
    } else {
        textField.setCaretPosition(beginPos);
        textField.moveCaretPosition(endPos);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Setting current row at " + index);
    }
    table.setRowSelectionInterval(index, index);
}

From source file:Main.java

public Main() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel()) {

        //Implement table cell tool tips.
        public String getToolTipText(MouseEvent e) {
            String tip = null;//w w w .  j  a va2s.  c om
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            int realColumnIndex = convertColumnIndexToModel(colIndex);

            if (realColumnIndex == 2) { //Sport column
                tip = "This person's favorite sport to " + "participate in is: "
                        + getValueAt(rowIndex, colIndex);
            } else if (realColumnIndex == 4) { //Veggie column
                TableModel model = getModel();
                String firstName = (String) model.getValueAt(rowIndex, 0);
                String lastName = (String) model.getValueAt(rowIndex, 1);
                Boolean veggie = (Boolean) model.getValueAt(rowIndex, 4);
                if (Boolean.TRUE.equals(veggie)) {
                    tip = firstName + " " + lastName + " is a vegetarian";
                } else {
                    tip = firstName + " " + lastName + " is not a vegetarian";
                }
            } else {
                //You can omit this part if you know you don't 
                //have any renderers that supply their own tool 
                //tips.
                tip = super.getToolTipText(e);
            }
            return tip;
        }

        //Implement table header tool tips. 
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex = columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    };

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:components.TableToolTipsDemo.java

public TableToolTipsDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel()) {

        //Implement table cell tool tips.
        public String getToolTipText(MouseEvent e) {
            String tip = null;/*from  w w  w.  j  av  a 2s . co m*/
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            int realColumnIndex = convertColumnIndexToModel(colIndex);

            if (realColumnIndex == 2) { //Sport column
                tip = "This person's favorite sport to " + "participate in is: "
                        + getValueAt(rowIndex, colIndex);
            } else if (realColumnIndex == 4) { //Veggie column
                TableModel model = getModel();
                String firstName = (String) model.getValueAt(rowIndex, 0);
                String lastName = (String) model.getValueAt(rowIndex, 1);
                Boolean veggie = (Boolean) model.getValueAt(rowIndex, 4);
                if (Boolean.TRUE.equals(veggie)) {
                    tip = firstName + " " + lastName + " is a vegetarian";
                } else {
                    tip = firstName + " " + lastName + " is not a vegetarian";
                }
            } else {
                //You can omit this part if you know you don't 
                //have any renderers that supply their own tool 
                //tips.
                tip = super.getToolTipText(e);
            }
            return tip;
        }

        //Implement table header tool tips. 
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex = columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    };

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:TableToolTipsDemo.java

public TableToolTipsDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel()) {

        //Implement table cell tool tips.
        public String getToolTipText(MouseEvent e) {
            String tip = null;//ww  w.j  a va 2s .c  om
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            int realColumnIndex = convertColumnIndexToModel(colIndex);

            if (realColumnIndex == 2) { //Sport column
                tip = "This person's favorite sport to " + "participate in is: "
                        + getValueAt(rowIndex, colIndex);
            } else if (realColumnIndex == 4) { //Veggie column
                TableModel model = getModel();
                String firstName = (String) model.getValueAt(rowIndex, 0);
                String lastName = (String) model.getValueAt(rowIndex, 1);
                Boolean veggie = (Boolean) model.getValueAt(rowIndex, 4);
                if (Boolean.TRUE.equals(veggie)) {
                    tip = firstName + " " + lastName + " is a vegetarian";
                } else {
                    tip = firstName + " " + lastName + " is not a vegetarian";
                }
            } else {
                //You can omit this part if you know you don't
                //have any renderers that supply their own tool
                //tips.
                tip = super.getToolTipText(e);
            }
            return tip;
        }

        //Implement table header tool tips.
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex = columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    };

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:com.oracle.cch.swingtest.MainJFrame.java

@Override
public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
        TableModel model = jTable1.getModel();
        Integer i = (Integer) model.getValueAt(jTable1.getSelectedRow(), 1);
        Product product = storeService.findProduct(i);
        ProductForm pf = new ProductForm(product);
        pf.setStoreService(AppContext.getApplicationContext().getBean("storeService", StoreService.class));
        pf.setVisible(true);/*from  www  . ja  v  a 2  s. co  m*/
    }
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * It will adjust the column width to match the widest element.
 * (You might not want to use this for every column, consider some columns might be really long)
 * @param model/*  w ww. j  av  a 2 s. c o  m*/
 * @param columnIndex
 * @param table
 * @return
 */
public static void adjustColumnWidth(TableModel model, int columnIndex, int maxWidth, int rightPadding,
        JTable table) {

    if (columnIndex > model.getColumnCount() - 1) {
        //invalid column index
        return;
    }

    if (!model.getColumnClass(columnIndex).equals(String.class)) {
        return;
    }

    String longestValue = "";
    for (int row = 0; row < model.getRowCount(); row++) {
        String strValue = (String) model.getValueAt(row, columnIndex);
        if (strValue != null && strValue.length() > longestValue.length()) {
            longestValue = strValue;
        }
    }

    Graphics g = table.getGraphics();

    try {
        int suggestedWidth = (int) g.getFontMetrics(table.getFont()).getStringBounds(longestValue, g)
                .getWidth();
        table.getColumnModel().getColumn(columnIndex)
                .setPreferredWidth(((suggestedWidth > maxWidth) ? maxWidth : suggestedWidth) + rightPadding);
    } catch (Exception e) {
        table.getColumnModel().getColumn(columnIndex).setPreferredWidth(maxWidth);
        e.printStackTrace();
    }

}