Example usage for javax.swing.event TableModelEvent INSERT

List of usage examples for javax.swing.event TableModelEvent INSERT

Introduction

In this page you can find the example usage for javax.swing.event TableModelEvent INSERT.

Prototype

int INSERT

To view the source code for javax.swing.event TableModelEvent INSERT.

Click Source Link

Document

Identifies the addition of new rows or columns.

Usage

From source file:Main.java

public void tableChanged(TableModelEvent e) {
    int firstRow = e.getFirstRow();
    int lastRow = e.getLastRow();
    int index = e.getColumn();

    switch (e.getType()) {
    case TableModelEvent.INSERT:
        for (int i = firstRow; i <= lastRow; i++) {
            System.out.println(i);
        }/*from   w w  w  . j ava 2s  .  c  o  m*/
        break;
    case TableModelEvent.UPDATE:
        if (firstRow == TableModelEvent.HEADER_ROW) {
            if (index == TableModelEvent.ALL_COLUMNS) {
                System.out.println("A column was added");
            } else {
                System.out.println(index + "in header changed");
            }
        } else {
            for (int i = firstRow; i <= lastRow; i++) {
                if (index == TableModelEvent.ALL_COLUMNS) {
                    System.out.println("All columns have changed");
                } else {
                    System.out.println(index);
                }
            }
        }
        break;
    case TableModelEvent.DELETE:
        for (int i = firstRow; i <= lastRow; i++) {
            System.out.println(i);
        }
        break;
    }
}

From source file:ExpenseReport.java

public ExpenseReport() {
    super("Expense Report");
    setSize(570, 200);/*from  w ww  .j  a  va 2 s .c o m*/

    m_data = new ExpenseReportData(this);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);
    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {
        TableCellRenderer renderer;
        if (k == ExpenseReportData.COL_APPROVED)
            renderer = new CheckCellRenderer();
        else {
            DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
            textRenderer.setHorizontalAlignment(ExpenseReportData.m_columns[k].m_alignment);
            renderer = textRenderer;
        }

        TableCellEditor editor;

        if (k == ExpenseReportData.COL_CATEGORY)
            editor = new DefaultCellEditor(new JComboBox(ExpenseReportData.CATEGORIES));
        else if (k == ExpenseReportData.COL_APPROVED)
            editor = new DefaultCellEditor(new JCheckBox());
        else
            editor = new DefaultCellEditor(new JTextField());

        TableColumn column = new TableColumn(k, ExpenseReportData.m_columns[k].m_width, renderer, editor);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.setSize(550, 150);
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");
    m_title = new JLabel("Total: $", penny, JButton.LEFT);
    m_title.setForeground(Color.black);
    m_title.setAlignmentY(0.5f);
    p.add(m_title);
    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");
    bt.setMnemonic('b');
    bt.setAlignmentY(0.5f);
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row);
            m_table.tableChanged(
                    new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Insert after");
    bt.setMnemonic('a');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row + 1);
            m_table.tableChanged(new TableModelEvent(m_data, row + 1, row + 1, TableModelEvent.ALL_COLUMNS,
                    TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Delete row");
    bt.setMnemonic('d');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            if (m_data.delete(row)) {
                m_table.tableChanged(new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS,
                        TableModelEvent.INSERT));
                m_table.repaint();
                calcTotal();
            }
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:net.sourceforge.happybank.main.BankMain.java

/**
 * loads the customer account data/*from   w  ww .  j  a v a 2s  .  c  o m*/
 * 
 * @return success (positive) or failure (negative)
 */
protected int loadAccounts() {
    try {
        List<Customer> customers = bank.getCustomers();
        Iterator<Customer> custIter = customers.iterator();
        Customer cust = null;
        while (custIter.hasNext()) {
            cust = custIter.next();
            String cid = cust.getId();
            Customer customer = bank.getCustomer(cid);
            List<Account> accounts = bank.getAccounts(cid);
            Iterator<Account> accIter = accounts.iterator();
            Account acc = null;
            while (accIter.hasNext()) {
                acc = accIter.next();
                String aid = acc.getId();
                acc = bank.getAccount(aid);
                int row = accountEntries.getSelectedRow();
                if (acc == null)
                    break;
                accountModel.insert(new AccountData(aid, acc.getType(), acc.getBalance(), cust.getFirstName(),
                        cust.getLastName(), cid), row + 1);
            }
        }

        // refresh table
        accountEntries.tableChanged(new TableModelEvent(accountModel, 0, accountModel.getRowCount(),
                TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
        accountEntries.repaint();
    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error reading account data", "Read Error",
                JOptionPane.ERROR_MESSAGE);
        return -1;
    }
    return 0;
}

From source file:com.stanley.captioner.MainFrame.java

private void initListeners() {
    videoTable.getModel().addTableModelListener(new TableModelListener() {
        @Override//from   w  ww.  j  a  v  a2 s  .  co m
        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.INSERT || e.getType() == TableModelEvent.DELETE) {
                if (videoTable.getRowCount() > 0) {
                    clearButton.setEnabled(true);
                    if (outputDirectoryField.getText() != null && outputDirectoryField.getText().length() > 0) {
                        convertButton.setEnabled(true);
                    } else {
                        convertButton.setEnabled(false);
                    }
                } else {
                    clearButton.setEnabled(false);
                    convertButton.setEnabled(false);
                }
            }
        }
    });

    videoTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (videoTable.getSelectedRowCount() > 0) {
                removeButton.setEnabled(true);
            } else {
                removeButton.setEnabled(false);
            }
        }
    });
}

From source file:blue.components.lines.Line.java

public void fireTableRowsInserted(int firstRow, int lastRow) {
    fireTableChanged(/*from  w  w w .jav  a 2  s.  co m*/
            new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}

From source file:org.jdal.swing.ListTableModel.java

/**
 * Add a Object to underlaying list/*  ww w  .j  a va2 s  .  co  m*/
 * @param o the object to add
 * @return true if added
 */
public boolean add(Object o) {
    boolean result = list.add(o);

    if (usingChecks)
        checks.add(Boolean.FALSE);

    if (list.size() == 1) {
        // adding on empty list, need to init
        init();
    }

    fireTableChanged(new TableModelEvent(this, list.size() - 1, list.size() - 1, TableModelEvent.ALL_COLUMNS,
            TableModelEvent.INSERT));

    return result;
}

From source file:it.cnr.icar.eric.client.ui.swing.RegistryObjectsTable.java

/**
 * This fine grain notification tells listeners the exact range
 * of cells, rows, or columns that changed.
 *
 * Overrides base class behaviour by setting selection when first
 * row (destination) is added to model.//from w  w  w.  j  a  va  2 s.c  o m
 */
public void tableChanged(TableModelEvent e) {
    if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
        // The whole thing changed
        // This will effect invalidation of the JTable and JTableHeader.
        createDefaultColumnsFromModel();
        // Reset cachedTableCellRenderer
        cachedTableCellRenderers = new TableCellRenderer[getColumnCount()];
        return;
    } else {
        super.tableChanged(e);
    }
    // If no selectedRow, set selectedRow to firstRow
    if ((selectedRow == -1) && (e.getType() == TableModelEvent.INSERT)) {
        // Following will result in a software initiated selection
        // of the first row in table
        ListSelectionModel rowSM = getSelectionModel();
        rowSM.setSelectionInterval(0, 0);
    }
}

From source file:org.nuclos.client.ui.collect.SubForm.java

public void setupTableModelListener() {
    this.tblmdllistener = new TableModelListener() {
        @Override//from   ww  w.j a va 2 s.  c  om
        public void tableChanged(TableModelEvent ev) {
            // recalculate the optimal column widths when custom column widths are not used yet and
            // a row is inserted (the first time):
            //@todo: this here is entered not only the first time a row is entered but also on startup (sort fires a tableDataChanged)
            if (!useCustomColumnWidths) {
                // Now this is an example for an API that sucks :(
                final boolean bRowsInserted = (ev.getType() == TableModelEvent.INSERT) ||
                // @todo: The first condition (INSERT) is clear, but what for the second (complete UPDATE)?
                (ev.getType() == TableModelEvent.UPDATE && ev.getColumn() == TableModelEvent.ALL_COLUMNS
                        && ev.getLastRow() == Integer.MAX_VALUE);
                if (bRowsInserted) {
                    resetDefaultColumnWidths();
                    LOG.debug("Custom column widths should be used here."); // Setting them manually.");
                }
            }
            // TableModelEvents caused by sorting don't change the subform state:
            if (!(ev instanceof SortableTableModelEvent)) {
                fireStateChanged();
            }
        }
    };
    subformtbl.getModel().addTableModelListener(this.tblmdllistener);
}

From source file:org.wings.STable.java

public void tableChanged(TableModelEvent e) {
    // kill active editors
    //editingCanceled(null);

    if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
        // The whole thing changed
        clearSelectionAndLeadAnchor();//from   w  ww  . java  2  s . c  o m
        if (getAutoCreateColumnsFromModel())
            createDefaultColumnsFromModel();
    } else {
        switch (e.getType()) {
        case TableModelEvent.INSERT:
            if (e.getFirstRow() >= 0)
                getSelectionModel().insertIndexInterval(e.getFirstRow(), e.getLastRow(), true);
            break;

        case TableModelEvent.DELETE:
            if (e.getFirstRow() >= 0)
                getSelectionModel().removeIndexInterval(e.getFirstRow(), e.getLastRow());
            break;
        case TableModelEvent.UPDATE:
            /* event fire on javax.swing.table.AbstractTableModel.fireTableDataChanged() */
            if (e.getLastRow() == Integer.MAX_VALUE)
                clearSelectionAndLeadAnchor();
            break;
        }
    }

    if (model.getRowCount() != rowCountBackUp) {
        rowCountBackUp = model.getRowCount();
        fireViewportChanged(false);
    }
    if (columnModel.getColumnCount() != columnCountBackUp) {
        columnCountBackUp = columnModel.getColumnCount();
        fireViewportChanged(true);
    }

    if (e != null && e.getFirstRow() == e.getLastRow() && e.getFirstRow() != TableModelEvent.HEADER_ROW
            && e.getColumn() != TableModelEvent.ALL_COLUMNS && e.getType() == TableModelEvent.UPDATE) {
        if (isUpdatePossible() && STable.class.isAssignableFrom(getClass()))
            update(((TableCG) getCG()).getRenderCellUpdate(this, e.getFirstRow(), e.getColumn()));
        else
            reload();
    }
    /*        else if (e != null &&
    e.getFirstRow() == 0 &&
    e.getLastRow() == Integer.MAX_VALUE &&
    e.getColumn() == TableModelEvent.ALL_COLUMNS &&
    e.getType() == TableModelEvent.UPDATE) {
    update(((TableCG)getCG()).getDataUpdate(this));
            }*/
    else {
        reload();
    }
}

From source file:phex.gui.models.SearchListTableModel.java

/**
 * Called if a search was added./* w ww.j ava  2s .  c  o m*/
 */
public void searchAdded(int position) {
    fireTableChanged(
            new TableModelEvent(this, position, position, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}