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:com.cch.aj.entryrecorder.frame.SearchJFrame.java

@Override
public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting() && tblSearch.getSelectedRow() != -1) {
        TableModel model = tblSearch.getModel();
        int checkId = (int) model.getValueAt(tblSearch.getSelectedRow(), 0);
        MainJFrame mf = AppContext.getApplicationContext().getBean("MainJFrame", MainJFrame.class);
        mf.init(checkId);/*from   ww  w  . j  a  v a2  s.  com*/
        mf.setVisible(true);
    }
}

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

/**
 * AWProcess when a user change the text contained for the textfield
 *
 * @param e/*from  www.  j av a2s. com*/
 */
private void textFieldChanged(DocumentEvent e) {
    if (logger.isDebugEnabled()) {
        logger.debug(
                "Processing text field change with column index: " + columnIndex + " changed event " + e + " ");
    }

    String prefix = textField.getText();
    if (prefix.length() == 0) {
        table.clearSelection();
        return;
    }

    boolean found = false;
    TableModel m = table.getModel();

    for (int i = 0; found == false && i < m.getRowCount(); i++) {
        Object o = m.getValueAt(i, columnIndex);
        if (o == null) {
            continue;
        }

        String s = o.toString();
        if (s.toUpperCase().startsWith(prefix.toUpperCase())) {
            table.setRowSelectionInterval(i, i);
            Rectangle rect = table.getCellRect(i, columnIndex, true);
            table.scrollRectToVisible(rect);

            found = true;
        }
    }

    if (!found) {
        table.clearSelection();
    }
}

From source file:SimpleTableSelectionDemo.java

private void printDebugData(JTable table) {
    int numRows = table.getRowCount();
    int numCols = table.getColumnCount();
    javax.swing.table.TableModel model = table.getModel();

    System.out.println("Value of data: ");
    for (int i = 0; i < numRows; i++) {
        System.out.print("    row " + i + ":");
        for (int j = 0; j < numCols; j++) {
            System.out.print("  " + model.getValueAt(i, j));
        }//  w  ww . jav a  2s.c o  m
        System.out.println();
    }
    System.out.println("--------------------------");
}

From source file:ListProperties.java

public int compareRowsByColumn(int row1, int row2, int column) {
    Class type = model.getColumnClass(column);
    TableModel data = model;

    // Check for nulls

    Object o1 = data.getValueAt(row1, column);
    Object o2 = data.getValueAt(row2, column);

    // If both values are null return 0
    if (o1 == null && o2 == null) {
        return 0;
    } else if (o1 == null) { // Define null less than everything.
        return -1;
    } else if (o2 == null) {
        return 1;
    }/*from   www .ja v a2  s. c  o m*/

    if (type.getSuperclass() == Number.class) {
        Number n1 = (Number) data.getValueAt(row1, column);
        double d1 = n1.doubleValue();
        Number n2 = (Number) data.getValueAt(row2, column);
        double d2 = n2.doubleValue();

        if (d1 < d2)
            return -1;
        else if (d1 > d2)
            return 1;
        else
            return 0;
    } else if (type == String.class) {
        String s1 = (String) data.getValueAt(row1, column);
        String s2 = (String) data.getValueAt(row2, column);
        int result = s1.compareTo(s2);

        if (result < 0)
            return -1;
        else if (result > 0)
            return 1;
        else
            return 0;
    } else if (type == java.util.Date.class) {
        Date d1 = (Date) data.getValueAt(row1, column);
        long n1 = d1.getTime();
        Date d2 = (Date) data.getValueAt(row2, column);
        long n2 = d2.getTime();

        if (n1 < n2)
            return -1;
        else if (n1 > n2)
            return 1;
        else
            return 0;
    } else if (type == Boolean.class) {
        Boolean bool1 = (Boolean) data.getValueAt(row1, column);
        boolean b1 = bool1.booleanValue();
        Boolean bool2 = (Boolean) data.getValueAt(row2, column);
        boolean b2 = bool2.booleanValue();

        if (b1 == b2)
            return 0;
        else if (b1) // Define false < true
            return 1;
        else
            return -1;
    } else {
        Object v1 = data.getValueAt(row1, column);
        String s1 = v1.toString();
        Object v2 = data.getValueAt(row2, column);
        String s2 = v2.toString();
        int result = s1.compareTo(s2);

        if (result < 0)
            return -1;
        else if (result > 0)
            return 1;
        else
            return 0;
    }
}

From source file:MainFrame.MainFrame.java

public MainFrame() {
    //Create menu
    this.menuBar = new JMenuBar();
    this.menuProxy = new JMenu("Proxy");
    this.menuBar.add(menuProxy);
    this.cbMenuItem = new JCheckBoxMenuItem("? proxy");
    this.cbMenuItem.setMnemonic(KeyEvent.VK_C);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            if (selected) {
                SingleDataHolder.getInstance().isProxyActivated = true;
                SingleDataHolder.getInstance().proxyIpAdress = MainFrame.this.proxyIpAdress.getText();
                SingleDataHolder.getInstance().proxyPort = Integer.parseInt(MainFrame.this.proxyPort.getText());
                SingleDataHolder.getInstance().proxyLogin = MainFrame.this.proxyLogin.getText();
                SingleDataHolder.getInstance().proxyPassword = String
                        .valueOf(MainFrame.this.proxyPassword.getPassword());
            } else {
                SingleDataHolder.getInstance().isProxyActivated = false;
            }/*from  w  w  w .  j a  v a  2 s  . com*/
        }
    };
    this.cbMenuItem.addActionListener(aListener);

    this.proxyIpAdress = new JTextField();
    this.proxyPort = new JTextField();
    this.proxyLogin = new JTextField();
    ;
    this.proxyPassword = new JPasswordField();
    this.menuProxy.add(this.cbMenuItem);
    this.menuProxy.add(new JLabel("IP ?"));
    this.menuProxy.add(this.proxyIpAdress);
    this.menuProxy.add(new JLabel("? "));
    this.menuProxy.add(this.proxyPort);
    this.menuProxy.add(new JLabel(""));
    this.menuProxy.add(this.proxyLogin);
    this.menuProxy.add(new JLabel(""));
    this.menuProxy.add(this.proxyPassword);
    this.setJMenuBar(this.menuBar);

    communicator = new HttpCommunicator();
    myActionListener = new mainFrameActionListener();
    mainPanel = new JPanel(new BorderLayout());
    this.setContentPane(mainPanel);

    JPanel P = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 5));
    mainPanel.add(P, BorderLayout.NORTH);

    //Create Group label
    labelGroup = new JLabel("");
    P.add(labelGroup);
    //Create Group combo
    comboGroup = new JComboBox();
    comboGroup.addItem("?");
    comboGroup.addActionListener(myActionListener);
    P.add(comboGroup);

    //Create Date label
    labelDate = new JLabel("");
    P.add(labelDate);
    //Create Date combo
    comboDate = new JComboBox();
    comboDate.addItem("?");
    comboDate.addActionListener(myActionListener);
    P.add(comboDate);

    //Create Delete button
    buttonDelete = new JButton("");
    buttonDelete.setEnabled(false);
    buttonDelete.addActionListener(myActionListener);
    P.add(buttonDelete);

    //this.checkConnection = new CheckConnection(buttonDelete);

    buttonPasswordsManager = new JButton(" ");
    buttonPasswordsManager.addActionListener(myActionListener);
    P.add(buttonPasswordsManager);

    try {
        Image img = ImageIO.read(getClass().getResource("resources/table_refresh.png"));
        this.buttonRefresh = new JButton(new ImageIcon(img));
        this.buttonRefresh.addActionListener(myActionListener);
        this.buttonRefresh.setToolTipText(" ");
        P.add(buttonRefresh);
    } catch (IOException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    //CreateLessonTableModel
    lessonTableModel = new LessonTableModel();

    try {
        communicator.setCombos(comboGroup, comboDate, lessonTableModel);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, " ? ? .");
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    //CreateTable
    table = new JTable(lessonTableModel);
    table.setFillsViewportHeight(true);
    table.setAutoCreateRowSorter(true);

    JScrollPane scrollP = new JScrollPane(table);
    mainPanel.add(scrollP, BorderLayout.CENTER);
    //Create Table sorter
    sorter = new TableRowSorter<LessonTableModel>(lessonTableModel);
    table.setRowSorter(sorter);
    table.getModel().addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            TableModel model = (TableModel) e.getSource();
            for (int i = 0; i < model.getRowCount(); i++) {
                if ((boolean) model.getValueAt(i, 2) == true) {
                    buttonDelete.setEnabled(true);
                    return;
                }
            }
            buttonDelete.setEnabled(false);
        }
    });

    //Standart block
    this.setSize(700, 400);
    this.setTitle(" ");
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent we) {
            System.exit(0);
        }
    });
    try {
        Image img = ImageIO.read(getClass().getResource("resources/appIcon.png"));
        this.setIconImage(img);
    } catch (IOException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    this.setVisible(true);
}

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 ww w  .j  a 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:com.mirth.connect.client.ui.components.MirthTableTransferHandler.java

@Override
protected Transferable createTransferable(JComponent c) {
    TableModel model = ((JTable) c).getModel();
    int[] rows;/* w ww .j  av a 2s .c o  m*/
    if (c instanceof MirthTreeTable) {
        rows = ((MirthTreeTable) c).getSelectedModelRows();
    } else {
        rows = ((MirthTable) c).getSelectedModelRows();
    }

    // Don't put anything on the clipboard if no rows are selected
    if (rows.length == 0) {
        return null;
    }

    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < rows.length; i++) {
        builder.append(model.getValueAt(rows[i], primaryColumnIndex));

        String secondaryValue = String.valueOf(model.getValueAt(rows[i], secondaryColumnIndex));
        if (StringUtils.isNotBlank(secondaryValue)) {
            builder.append(" (");
            builder.append(model.getValueAt(rows[i], secondaryColumnIndex));
            builder.append(")");
        }

        if (i != rows.length - 1) {
            builder.append("\n");
        }
    }

    return new StringSelection(builder.toString());
}

From source file:userinterface.CyberSecurity.CyberSecurityJPanel.java

private String getEmployeeIDFromTable(int rowIndex) {
    final TableModel modelView = employeeTable.getModel();
    return (String) modelView.getValueAt(rowIndex, ID_COLUMN_INDEX);
}

From source file:medsavant.enrichment.app.RegionListAggregatePanel.java

private JPopupMenu createPopup() {
    JPopupMenu menu = new JPopupMenu();

    TableModel model = tablePanel.getTable().getModel();
    final int[] selRows = tablePanel.getTable().getSelectedRows();

    if (selRows.length == 1) {
        JMenuItem browseItem = new JMenuItem(String.format("<html>Look at %s in genome browser</html>",
                "<i>" + model.getValueAt(selRows[0], 0) + "</i>"));
        browseItem.addActionListener(new ActionListener() {
            @Override/*from w ww.  j  a  v a 2s  .  c  o m*/
            public void actionPerformed(ActionEvent ae) {
                TableModel model = tablePanel.getTable().getModel();
                int r = selRows[0];
                LocationController.getInstance().setLocation((String) model.getValueAt(r, 1),
                        new Range((Integer) model.getValueAt(r, 2), (Integer) model.getValueAt(r, 3)));
                ViewController.getInstance().getMenu().switchToSubSection(BrowserPage.getInstance());
            }
        });
        menu.add(browseItem);
    }

    JMenuItem posItem = new JMenuItem(String.format("<html>Filter by %s</html>",
            selRows.length == 1 ? "Region <i>" + model.getValueAt(selRows[0], 0) + "</i>"
                    : "Selected Regions"));
    posItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            ThreadController.getInstance().cancelWorkers(pageName);

            List<GenomicRegion> regions = new ArrayList<GenomicRegion>();
            TableModel model = tablePanel.getTable().getModel();

            for (int r : selRows) {
                String geneName = (String) model.getValueAt(r, 0);
                String chrom = (String) model.getValueAt(r, 1);
                Integer start = (Integer) model.getValueAt(r, 2);
                Integer end = (Integer) model.getValueAt(r, 3);

                regions.add(new GenomicRegion(geneName, chrom, start, end));
            }

            QueryUtils.addQueryOnRegions(regions,
                    Arrays.asList(new RegionSet[] { (RegionSet) regionSetCombo.getSelectedItem() }));

        }
    });
    menu.add(posItem);

    return menu;
}

From source file:edu.ku.brc.specify.utilapps.SetUpBuildDlg.java

/**
 * @param table//from  w  w w  .  j  a va 2s.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
        } else {
            // ???
        }

        totalWidth += column.getPreferredWidth();
    }
}