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:org.tellervo.desktop.tridasv2.ui.ComponentViewer.java

private void setupTable() {
    tableModel = new ElementListTableModel();
    table = new JXTable(tableModel);

    tableSorter = new ElementListTableSorter(tableModel, table);
    table.getTableHeader().addMouseListener(tableSorter); // add sorter & header renderer
    table.setColumnSelectionAllowed(false);
    table.setRowSelectionAllowed(true);//w  ww  . j  a  v a 2  s .c  o  m

    // set our column widths
    ElementListTableModel.setupColumnWidths(table);

    table.setDefaultRenderer(Object.class, new ElementListCellRenderer(this, false));
    table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer(this, false));

    // hide irrelevent columns
    TableColumnModelExt colmodel = (TableColumnModelExt) table.getColumnModel();
    table.setColumnControlVisible(true);
    colmodel.getColumnExt(I18n.getText("hidden.MostRecentVersion")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.n")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.rec")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.hash")).setVisible(false);

    // popup menu
    table.addMouseListener(new PopupListener() {
        @Override
        public void showPopup(MouseEvent e) {
            // only clicks on tables
            if (!(e.getSource() instanceof JTable))
                return;

            JTable table = (JTable) e.getSource();
            ElementListTableModel model = (ElementListTableModel) table.getModel();

            // get the row and sanity check
            int row = table.rowAtPoint(e.getPoint());
            if (row < 0 || row >= model.getRowCount())
                return;

            // select it?
            table.setRowSelectionInterval(row, row);

            // get the element
            Element element = model.getElementAt(row);

            // create and show the menu
            JPopupMenu popup = new ElementListPopupMenu(element, ComponentViewer.this);
            popup.show(table, e.getX(), e.getY());
        }
    });
}

From source file:org.tellervo.desktop.tridasv2.ui.ComponentViewerOld.java

private void setupTable() {
    tableModel = new ElementListTableModel();
    table = new JXTable(tableModel);

    tableSorter = new ElementListTableSorter(tableModel, table);
    table.getTableHeader().addMouseListener(tableSorter); // add sorter & header renderer
    table.setColumnSelectionAllowed(false);
    table.setRowSelectionAllowed(true);//  w w w  .ja v a 2s  . c o  m

    // set our column widths
    ElementListTableModel.setupColumnWidths(table);

    table.setDefaultRenderer(Object.class, new ElementListCellRenderer(this, false));
    table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer(this, false));

    // hide irrelevent columns
    TableColumnModelExt colmodel = (TableColumnModelExt) table.getColumnModel();
    table.setColumnControlVisible(true);
    colmodel.getColumnExt(I18n.getText("hidden.MostRecentVersion")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.n")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.rec")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.hash")).setVisible(false);

    // popup menu
    table.addMouseListener(new PopupListener() {
        @Override
        public void showPopup(MouseEvent e) {
            // only clicks on tables
            if (!(e.getSource() instanceof JTable))
                return;

            JTable table = (JTable) e.getSource();
            ElementListTableModel model = (ElementListTableModel) table.getModel();

            // get the row and sanity check
            int row = table.rowAtPoint(e.getPoint());
            if (row < 0 || row >= model.getRowCount())
                return;

            // select it?
            table.setRowSelectionInterval(row, row);

            // get the element
            Element element = model.getElementAt(row);

            // create and show the menu
            JPopupMenu popup = new ElementListPopupMenu(element, ComponentViewerOld.this);
            popup.show(table, e.getX(), e.getY());
        }
    });
}

From source file:org.yccheok.jstock.gui.JStock.java

public void addStockToTable(final Stock stock, final StockAlert alert) {
    final JTable _jTable1 = this.jTable1;
    if (java.awt.EventQueue.isDispatchThread()) {
        final StockTableModel tableModel = (StockTableModel) _jTable1.getModel();
        tableModel.addStock(stock, alert);
    } else {//from   ww w  .ja  va 2 s.  c  o m
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final StockTableModel tableModel = (StockTableModel) _jTable1.getModel();
                tableModel.addStock(stock, alert);
            }
        });
    }
}

From source file:org.yccheok.jstock.gui.JStock.java

public void addStockToTable(final Stock stock) {
    final JTable _jTable1 = this.jTable1;
    if (java.awt.EventQueue.isDispatchThread()) {
        final StockTableModel tableModel = (StockTableModel) _jTable1.getModel();
        tableModel.addStock(stock);//  ww  w. j ava  2 s . c om
    } else {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final StockTableModel tableModel = (StockTableModel) _jTable1.getModel();
                tableModel.addStock(stock);
            }
        });
    }
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void insertTableColumnFromModel(JTable jTable, Object value, int clickedColumnIndex) {
    boolean isVisible = true;

    try {//from w  ww  .  ja  v  a2 s.com

        TableColumn tableColumn = jTable.getColumn(value);

    }

    catch (java.lang.IllegalArgumentException exp) {

        isVisible = false;

    }

    if (isVisible)
        return;

    TableModel tableModel = jTable.getModel();

    final int modelIndex = getModelColumnIndex(jTable, value);

    Class c = tableModel.getColumnClass(modelIndex);

    TableColumn tableColumn = new javax.swing.table.TableColumn(modelIndex, 0, jTable.getDefaultRenderer(c),
            jTable.getDefaultEditor(c));

    jTable.addColumn(tableColumn);

    makeTableColumnWidthFit(jTable, jTable.getColumnCount() - 1, 5);

    // If we right clicked on the 3rd column, and select a new column, we
    // would like the new column to be inserted into 4th column. Note that,
    // clickedColumnIndex will be < 0, if we right clicked on empty area.
    if (clickedColumnIndex < 0) {
        // Have it in the last column when we right clicked on empty area.
        jTable.moveColumn(jTable.getColumnCount() - 1, jTable.getColumnCount() - 1);
    } else {
        // +1, as we want our newly inserted column to be at the right of
        // clicked column.
        jTable.moveColumn(jTable.getColumnCount() - 1,
                Math.min(jTable.getColumnCount() - 1, clickedColumnIndex + 1));
    }
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static int getModelColumnIndex(JTable jTable, Object value) {

    TableModel tableModel = jTable.getModel();

    if (tableModel instanceof StockTableModel) {

        return ((StockTableModel) tableModel).findColumn(value.toString());

    }/*from ww  w .  jav  a 2 s .c o m*/

    try {

        TableColumn tableColumn = jTable.getColumn(value);

        return tableColumn.getModelIndex();

    } // Anti-pattern. We are depending on the exception throwing. Bad!

    catch (java.lang.IllegalArgumentException exp) {

        final int columnCount = tableModel.getColumnCount();

        for (int col = 0; col < columnCount; col++) {

            String s = tableModel.getColumnName(col);

            if (s.equals(value))

                return col;

        }

    }

    return -1;

}

From source file:savant.view.swing.BookmarkSheet.java

private void loadBookmarks(JTable table) {
    final BookmarksTableModel btm = (BookmarksTableModel) table.getModel();
    List<Bookmark> bookmarks = btm.getData();

    if (bookmarks.size() > 0) {
        String message = "Clear existing bookmarks?";
        String title = "Clear Bookmarks";
        // display the JOptionPane showConfirmDialog
        int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            btm.clearData();/*  w w w.j av a  2  s  .co m*/
            BookmarkController.getInstance().clearBookmarks();
        }
    }

    final File selectedFile = DialogUtils.chooseFileForOpen("Load Bookmarks", null, null);

    // set the genome
    if (selectedFile != null) {

        int result = JOptionPane.showOptionDialog(null, "Would you like to add padding to each bookmark range?",
                "Add a margin?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
        final boolean addMargin = (result == JOptionPane.YES_OPTION);

        Window w = SwingUtilities.getWindowAncestor(BookmarkSheet.this);
        JOptionPane optionPane = new JOptionPane(
                "<html>Loading bookmarks from file.<br>This may take a moment.</html>",
                JOptionPane.INFORMATION_MESSAGE, JOptionPane.CANCEL_OPTION);
        final JDialog dialog = new JDialog(w, "Loading Bookmarks", Dialog.ModalityType.MODELESS);
        dialog.setContentPane(optionPane);
        dialog.pack();
        dialog.setLocationRelativeTo(w);
        dialog.setVisible(true);
        new Thread("BookmarkSheet.loadBookmarks") {
            @Override
            public void run() {
                try {
                    BookmarkController.getInstance().addBookmarksFromFile(selectedFile, addMargin);
                    btm.fireTableDataChanged();
                } catch (Exception ex) {
                    DialogUtils.displayError("Error", "Unable to load bookmarks: " + ex.getMessage());
                } finally {
                    dialog.setVisible(false);
                    dialog.dispose();
                }
            }
        }.start();
    }
}

From source file:savant.view.swing.BookmarkSheet.java

private static void saveBookmarks(JTable table) {
    BookmarksTableModel btm = (BookmarksTableModel) table.getModel();
    List<Bookmark> bookmarks = btm.getData();

    // get the path (null if none selected)
    File selectedFile = DialogUtils.chooseFileForSave("Save Bookmarks", "Bookmarks.txt");

    // set the genome
    if (selectedFile != null) {
        try {//  w w w .j  a  v a 2  s . c  o  m
            saveBookmarks(selectedFile.getAbsolutePath(), bookmarks);
        } catch (IOException ex) {
            DialogUtils.displayError("Error", "Unable to save bookmarks: " + ex.getMessage());
        }
    }
}

From source file:StockForecast.Main.java

private void displayUser() throws Exception {
    String selectItem = jComboBox2.getSelectedItem().toString();
    Connect connect = new Connect();
    DefaultTableModel displayTable = new DefaultTableModel();
    String text = jComboBox4.getSelectedItem().toString();
    System.out.println(text.toUpperCase());

    displayTable.addColumn("User ID");
    displayTable.addColumn("Username");
    displayTable.addColumn("Password");
    displayTable.addColumn("User Type");
    displayTable.addColumn("Name");
    displayTable.addColumn("");
    displayTable.addColumn("");
    ResultSet rs = connect.connectSelect("1",
            "SELECT * FROM ROOT.LOGIN NATURAL JOIN PROFILE WHERE LOGINID='" + text + "'");
    displayTable.addRow(new Object[] { rs.getString("loginid"), rs.getString("username"),
            rs.getString("password"), rs.getString("usertype"), rs.getString("name"), "Delete", "Update" });
    jTable1.setModel(displayTable);/* w w  w. j  av  a 2 s .c o  m*/
    Action delete = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            JTable table = (JTable) e.getSource();
            int modelRow = Integer.valueOf(e.getActionCommand());
            ((DefaultTableModel) table.getModel()).removeRow(modelRow);
        }
    };

    ButtonColumn buttonColumn = new ButtonColumn(jTable1, delete, 5);
    ButtonColumn btnColumn = new ButtonColumn(jTable1, delete, 6);
    buttonColumn.setMnemonic(KeyEvent.VK_D);
    btnColumn.setMnemonic(KeyEvent.VK_U);

}

From source file:utils.ZTransform.java

@Override
public void actionPerformed(ActionEvent e) {
    List<CMatrix> loadedCMatrices = CoolMapMaster.getLoadedCMatrices();
    if (loadedCMatrices == null || loadedCMatrices.isEmpty()) {
        Messenger.showWarningMessage("No datasets were imported.", "No data");
        return;// w  ww.j  av  a 2 s .  c  o m
    }

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            JTable table = new JTable();
            DefaultTableModel defaultTableModel = Utils.getDefaultTableModel();
            table.setModel(defaultTableModel);
            table.getColumnModel().removeColumn(table.getColumnModel().getColumn(0));
            table.getTableHeader().setReorderingAllowed(false);

            int returnVal = JOptionPane.showConfirmDialog(CoolMapMaster.getCMainFrame(), new JScrollPane(table),
                    "Select data", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (returnVal == JOptionPane.OK_OPTION) {
                int[] selectedRows = table.getSelectedRows();
                ArrayList<CMatrix> selectedMatrices = new ArrayList<CMatrix>();
                for (int row : selectedRows) {

                    int index = table.convertRowIndexToModel(row);
                    try {
                        String ID = table.getModel().getValueAt(index, 0).toString();
                        CMatrix mx = CoolMapMaster.getCMatrixByID(ID);
                        if (mx != null) {
                            selectedMatrices.add(mx);
                        }
                    } catch (Exception e) {

                    }
                }
                //do
                createZTransform(selectedMatrices);
            }
        }
    });

}