Example usage for javax.swing JTable getModel

List of usage examples for javax.swing JTable getModel

Introduction

In this page you can find the example usage for javax.swing JTable getModel.

Prototype

public TableModel getModel() 

Source Link

Document

Returns the TableModel that provides the data displayed by this JTable .

Usage

From source file:MainWindowLogic.java

static Point2D[] rewritePointsTableWitoutDuplicats(JTable jTable1) {
    deleteDuplicatsFromTable(jTable1);//w  ww.  jav a  2  s .c  o  m
    DefaultTableModel tmp = (DefaultTableModel) jTable1.getModel();

    Point2D[] rewritedTableFromTable = new Point2D[tmp.getRowCount()];
    for (int i = 0; i < rewritedTableFromTable.length; i++) {
        rewritedTableFromTable[i] = new Point2D.Double((double) tmp.getValueAt(i, 1),
                (double) tmp.getValueAt(i, 2));
    }

    return rewritedTableFromTable;
}

From source file:Main.java

/**
 *  Calculates the optimal width for the column of the given table. The
 *  calculation is based on the preferred width of the header and cell
 *  renderer.//from  www .  ja v  a2  s.c  om
 *  <br>
 *  Taken from the newsgoup de.comp.lang.java with some modifications.<br>
 *  Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br>
 *
 *  @param table    the table to calculate the column width
 *  @param col      the column to calculate the widths
 *  @return         the width, -1 if error
 */
public static int calcColumnWidth(JTable table, int col) {
    int width = calcHeaderWidth(table, col);
    if (width == -1)
        return width;

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    int rowCount = data.getRowCount();
    TableColumn column = columns.getColumn(col);
    try {
        for (int row = rowCount - 1; row >= 0; --row) {
            Component c = table.prepareRenderer(table.getCellRenderer(row, col), row, col);
            width = Math.max(width, c.getPreferredSize().width + 10);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return width;
}

From source file:PagingTester.java

public static JScrollPane createPagingScrollPaneForTable(JTable jt) {
    JScrollPane jsp = new JScrollPane(jt);
    TableModel tmodel = jt.getModel();

    // Don't choke if this is called on a regular table . . .
    if (!(tmodel instanceof PagingModel)) {
        return jsp;
    }//from  w w w.  j av  a  2  s . c o m

    // Okay, go ahead and build the real scrollpane
    final PagingModel model = (PagingModel) tmodel;
    final JButton upButton = new JButton(new ArrowIcon(ArrowIcon.UP));
    upButton.setEnabled(false); // starts off at 0, so can't go up
    final JButton downButton = new JButton(new ArrowIcon(ArrowIcon.DOWN));
    if (model.getPageCount() <= 1) {
        downButton.setEnabled(false); // One page...can't scroll down
    }

    upButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            model.pageUp();

            // If we hit the top of the data, disable the up button.
            if (model.getPageOffset() == 0) {
                upButton.setEnabled(false);
            }
            downButton.setEnabled(true);
        }
    });

    downButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            model.pageDown();

            // If we hit the bottom of the data, disable the down button.
            if (model.getPageOffset() == (model.getPageCount() - 1)) {
                downButton.setEnabled(false);
            }
            upButton.setEnabled(true);
        }
    });

    // Turn on the scrollbars; otherwise we won't get our corners.
    jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    // Add in the corners (page up/down).
    jsp.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, upButton);
    jsp.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, downButton);

    return jsp;
}

From source file:OAT.ui.util.UiUtil.java

public static void appendRow(JTable table, Object[] row) {
    ((DefaultTableModel) table.getModel()).addRow(row);
}

From source file:MainWindowLogic.java

static void readPointsFile(File fil_hndl, JTable tabela) throws IOException, InvalidFormatInFileException {
    ReadPointsFromFile pointsReader = new ReadPointsFromFile(fil_hndl.getPath());
    System.out.println("cieka wcztytywanego pliku: " + fil_hndl.getPath());
    Point2D[] readed = pointsReader.readPoints();

    DefaultTableModel defaultModelTabeli = (DefaultTableModel) tabela.getModel();

    for (Point2D point : readed) {
        defaultModelTabeli.addRow(/*from w  ww .  j  a  v  a 2 s. co  m*/
                new Object[] { defaultModelTabeli.getRowCount() + 1 + ".", point.getX(), point.getY(), false });
    }

}

From source file:Main.java

/**
 *  Calculates the optimal width for the header of the given table. The
 *  calculation is based on the preferred width of the header renderer.
 *
 *  @param table    the table to calculate the column width
 *  @param col      the column to calculate the widths
 *  @return         the width, -1 if error
 *///  www .j a  v a2  s. c  om
public static int calcHeaderWidth(JTable table, int col) {
    if (table == null)
        return -1;

    if (col < 0 || col > table.getColumnCount()) {
        System.out.println("invalid col " + col);
        return -1;
    }

    JTableHeader header = table.getTableHeader();
    TableCellRenderer defaultHeaderRenderer = null;
    if (header != null)
        defaultHeaderRenderer = header.getDefaultRenderer();
    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    TableColumn column = columns.getColumn(col);
    int width = -1;
    TableCellRenderer h = column.getHeaderRenderer();
    if (h == null)
        h = defaultHeaderRenderer;
    if (h != null) {
        // Not explicitly impossible
        Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, col);
        width = c.getPreferredSize().width + 5;
    }

    return width;
}

From source file:Main.java

public static void SetJTableAlignment(javax.swing.JTable jTable, ComponentOrientation componentOrientation) {
    Font tahoma = new Font("Tahoma", Font.PLAIN, 11);
    int labelAlighnment = JLabel.RIGHT;
    int headerAlighnment = JLabel.RIGHT;
    if (componentOrientation == ComponentOrientation.LEFT_TO_RIGHT) {
        labelAlighnment = JLabel.LEFT;
        headerAlighnment = JLabel.LEFT;
    }/*from   ww w .  jav a2 s .  c  om*/

    DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
    defaultTableCellRenderer.setHorizontalAlignment(labelAlighnment);
    // defaultTableCellRenderer.setFont(tahoma);

    for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) {
        if (jTable.getModel().getColumnClass(columnIndex).equals(Boolean.class)) {
            jTable.getColumnModel().getColumn(columnIndex).setWidth(60);
            jTable.getColumnModel().getColumn(columnIndex).setMaxWidth(90);
            jTable.getColumnModel().getColumn(columnIndex).setMinWidth(10);
            jTable.getColumnModel().getColumn(columnIndex).setPreferredWidth(60);
            continue;
        }
        if (jTable.getModel().getColumnClass(columnIndex).equals(ImageIcon.class)) {
            continue;
        }
        jTable.getColumnModel().getColumn(columnIndex).setCellRenderer(defaultTableCellRenderer);

    }
    DefaultTableCellRenderer renderer;
    renderer = (DefaultTableCellRenderer) jTable.getTableHeader().getDefaultRenderer();
    renderer.setHorizontalAlignment(headerAlighnment);

    jTable.getTableHeader().setDefaultRenderer(renderer);

}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Calculates and sets the each column to it preferred size
 * @param table the table to fix ups//from  w  ww .  ja v a  2  s  .  c  o m
 */
public static void calcColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader();

    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null) {
        defaultHeaderRenderer = header.getDefaultRenderer();
    }

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();

    int margin = columns.getColumnMargin(); // only JDK1.3

    int rowCount = data.getRowCount();

    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        TableColumn column = columns.getColumn(i);

        int columnIndex = column.getModelIndex();

        int width = -1;

        TableCellRenderer h = column.getHeaderRenderer();

        if (h == null)
            h = defaultHeaderRenderer;

        if (h != null) // Not explicitly impossible
        {
            Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i);

            width = c.getPreferredSize().width;
        }

        for (int row = rowCount - 1; row >= 0; --row) {
            TableCellRenderer r = table.getCellRenderer(row, i);

            Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                    false, row, i);

            width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer
        }

        if (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        }

        totalWidth += column.getPreferredWidth();
    }

    // If you like; This does not make sense for two many columns!
    Dimension size = table.getPreferredScrollableViewportSize();
    //if (totalWidth > size.width)
    {
        size.height = Math.min(size.height, table.getRowHeight() * visibleRows);
        size.width = totalWidth;
        table.setPreferredScrollableViewportSize(size);
    }

}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Get the StatDataItem at a given row./*from www  . ja v a2 s .  c  om*/
 * @param tableArg the table 
 * @param rowIndex the row index in question
 * @return the StatDataItem at the rowIndex
 */
protected static StatDataItem getStatDataItem(final JTable tableArg, final int rowIndex) {
    int rowInx = rowIndex;

    TableModel tblModel;
    if (tableArg instanceof SortableJTable) {
        SortableJTable sTable = (SortableJTable) tableArg;
        SortableTableModel sortableModel = sTable.getSortableTableModel();

        rowInx = sortableModel.getDelegatedRow(rowInx);
        tblModel = sTable.getModel();

    } else {
        tblModel = tableArg.getModel();
    }

    return ((StatGroupTableModel) tblModel).getDataItem(rowInx);

}

From source file:SimpleTableModel.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new SimpleTableModel());

    TableRowSorter sorter = new TableRowSorter(table.getModel());
    table.setRowSorter(sorter);// w ww  .j  a  va  2 s . co m

    RowFilter<SimpleTableModel, Integer> IDFilter = new RowFilter<SimpleTableModel, Integer>() {
        @Override
        public boolean include(Entry<? extends SimpleTableModel, ? extends Integer> entry) {
            SimpleTableModel model = entry.getModel();
            int rowIndex = entry.getIdentifier().intValue();
            Integer ID = (Integer) model.getValueAt(rowIndex, 0);
            if (ID.intValue() <= 100) {
                return false; // Do not show rows with an ID <= 100
            }
            return true;
        }
    };

    sorter.setRowFilter(IDFilter);

    Container contentPane = this.getContentPane();
    contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
}