Example usage for javax.swing.table DefaultTableModel getValueAt

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

Introduction

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

Prototype

public Object getValueAt(int row, int column) 

Source Link

Document

Returns an attribute value for the cell at row and column.

Usage

From source file:Main.java

public static void main(String s[]) {
    DefaultTableModel model = new DefaultTableModel();

    model.addColumn("Col1");
    model.addColumn("Col2");

    model.addRow(new Object[] { "1", "v2" });
    model.addRow(new Object[] { "2", "v2" });

    List<String> numdata = new ArrayList<String>();
    for (int count = 0; count < model.getRowCount(); count++) {
        numdata.add(model.getValueAt(count, 0).toString());
    }/*from  ww  w  .j a  va  2  s. c  om*/

    System.out.println(numdata);
}

From source file:Main.java

public static void main(String[] args) {
    int maximum = 100;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Integer[] oneRow = { 0, 0, 0, 0 };
    String[] headers = { "A", "B", "C", "D" };
    Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, };
    DefaultTableModel model = new DefaultTableModel(data, headers);
    JTable table = new JTable(model);
    table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    frame.add(new JScrollPane(table));
    frame.pack();/*from   w w w .  j  av  a2s.  c o m*/
    frame.setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Object waiter = new Object();
            synchronized (waiter) {
                int rows = model.getRowCount();
                int columns = model.getColumnCount();
                Random random = new Random(System.currentTimeMillis());
                boolean done = false;
                while (!done) {
                    int row = random.nextInt(rows);
                    int column = random.nextInt(columns);
                    Integer value = (Integer) model.getValueAt(row, column);
                    value++;
                    if (value <= maximum) {
                        model.setValueAt(value, row, column);
                        try {
                            waiter.wait(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    done = true;
                    for (row = 0; row < rows; row++) {
                        for (column = 0; column < columns; column++) {
                            if (!model.getValueAt(row, column).equals(maximum)) {
                                done = false;
                                break;
                            }
                        }
                        if (!done) {
                            break;
                        }
                    }
                }
            }
        }
    }).start();
}

From source file:Main.java

public static Object[] getValueBySelectedRow(JTable table, int rows[], int col) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    Object[] ret = new Object[rows.length];
    for (int i = 0; i < rows.length; i++)
        ret[i] = model.getValueAt(rows[i], col);
    return ret;/* w  w  w.j  a v  a 2  s.  co m*/
}

From source file:Main.java

public static Object[] getData(int index, final JTable table) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    Object[] rowData = new Object[model.getColumnCount()];
    for (int i = 0; i < rowData.length; i++) {
        rowData[i] = model.getValueAt(index, i);
    }/*ww w .  j  a va2 s.  c o  m*/
    return rowData;
}

From source file:com.qualixium.executor.command.CommandHelper.java

public static JsonNode getJsonNodeFromCommandsModel(DefaultTableModel model) {
    ArrayNode jsonArray = MAPPER.createArrayNode();

    for (int i = 0; i < model.getRowCount(); i++) {
        ObjectNode jsonNode = MAPPER.createObjectNode();

        String name = (String) model.getValueAt(i, 0);
        String command = ((String) model.getValueAt(i, 1)).replace("\\", "\\\\");

        jsonNode.put(model.getColumnName(0), name);
        jsonNode.put(model.getColumnName(1), command);

        jsonArray.add(jsonNode);/* ww w . j a  v  a2  s  .c  o m*/
    }

    return jsonArray;
}

From source file:MainWindowLogic.java

static private void addPointsToSeries(XYSeries seriesDestination, JTable jTableSource) {
    DefaultTableModel defaultModelOfJTable = (DefaultTableModel) jTableSource.getModel();
    for (int i = 0; i < defaultModelOfJTable.getRowCount(); i++) {
        seriesDestination.add((double) defaultModelOfJTable.getValueAt(i, 1),
                (double) defaultModelOfJTable.getValueAt(i, 2));
    }/*  w w  w .  j  a  v a2s  .c  o m*/
}

From source file:MainWindowLogic.java

static void deleteSelectedRow(JTable where) {
    DefaultTableModel tmp = (DefaultTableModel) where.getModel();
    System.out.println("Wielko tabeli przed usuniciem: " + tmp.getRowCount());
    for (int i = 0; i < tmp.getRowCount(); i++) {
        boolean result = (boolean) tmp.getValueAt(i, 3);
        if (result) {
            tmp.removeRow(i);//  ww  w  .  j  a v  a 2  s  .  c  o m
            i--;
        }

    }
    System.out.println("Wielko tabeli po usuniciu: " + tmp.getRowCount());
    //TODO naprawi numeracje tabeli
    ponumerujWiersze(where);
}

From source file:MainWindowLogic.java

private static void deleteDuplicatsFromTable(JTable jTable) {
    DefaultTableModel tmp = (DefaultTableModel) jTable.getModel();
    boolean somethingChange = false;
    for (int i = 0; i < tmp.getRowCount(); i++) {
        for (int j = i + 1; j < tmp.getRowCount(); j++) {

            if (((double) tmp.getValueAt(i, 1) == (double) tmp.getValueAt(j, 1))
                    && ((double) tmp.getValueAt(i, 2) == (double) tmp.getValueAt(j, 2))) {
                tmp.removeRow(j);//from w  ww.  java  2s.  c o  m
                j--;
                somethingChange = true;

            }
        }
    }
    if (somethingChange) {
        ponumerujWiersze(jTable);
    }
}

From source file:MainWindowLogic.java

static Point2D[] rewritePointsTableWitoutDuplicats(JTable jTable1) {
    deleteDuplicatsFromTable(jTable1);//from   w ww.j  av  a  2s.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:com.stam.batchmove.BatchMoveUtils.java

public static void showFilesFrame(Object[][] data, String[] columnNames, final JFrame callerFrame) {
    final FilesFrame filesFrame = new FilesFrame();

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {

        private static final long serialVersionUID = 1L;

        @Override/*from  w ww  . j a  v a  2  s .  c  om*/
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return column == 0;
        }
    };
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(JLabel.CENTER);
    final JTable table = new JTable(model);
    for (int i = 1; i < table.getColumnCount(); i++) {
        table.setDefaultRenderer(table.getColumnClass(i), renderer);
    }
    //            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setRowHeight(30);
    table.getTableHeader().setFont(new Font("Serif", Font.BOLD, 14));
    table.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
    table.setRowSelectionAllowed(false);
    table.getColumnModel().getColumn(0).setMaxWidth(35);
    table.getColumnModel().getColumn(1).setPreferredWidth(350);
    table.getColumnModel().getColumn(2).setPreferredWidth(90);
    table.getColumnModel().getColumn(2).setMaxWidth(140);
    table.getColumnModel().getColumn(3).setMaxWidth(90);

    JPanel tblPanel = new JPanel();
    JPanel btnPanel = new JPanel();

    tblPanel.setLayout(new BorderLayout());
    if (table.getRowCount() > 15) {
        JScrollPane scrollPane = new JScrollPane(table);
        tblPanel.add(scrollPane, BorderLayout.CENTER);
    } else {
        tblPanel.add(table.getTableHeader(), BorderLayout.NORTH);
        tblPanel.add(table, BorderLayout.CENTER);
    }

    btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    filesFrame.setMinimumSize(new Dimension(800, 600));
    filesFrame.setLayout(new BorderLayout());
    filesFrame.add(tblPanel, BorderLayout.NORTH);
    filesFrame.add(btnPanel, BorderLayout.SOUTH);

    final JLabel resultsLabel = new JLabel();

    JButton cancelBtn = new JButton("Cancel");
    cancelBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            filesFrame.setVisible(false);
            callerFrame.setVisible(true);
        }
    });

    JButton moveBtn = new JButton("Copy");
    moveBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setDialogTitle("Choose target directory");
            int selVal = fileChooser.showOpenDialog(null);
            if (selVal == JFileChooser.APPROVE_OPTION) {
                File selection = fileChooser.getSelectedFile();
                String targetPath = selection.getAbsolutePath();

                DefaultTableModel dtm = (DefaultTableModel) table.getModel();
                int nRow = dtm.getRowCount();
                int copied = 0;
                for (int i = 0; i < nRow; i++) {
                    Boolean selected = (Boolean) dtm.getValueAt(i, 0);
                    String filePath = dtm.getValueAt(i, 1).toString();

                    if (selected) {
                        try {
                            FileUtils.copyFileToDirectory(new File(filePath), new File(targetPath));
                            dtm.setValueAt("Copied", i, 3);
                            copied++;
                        } catch (Exception ex) {
                            Logger.getLogger(SelectionFrame.class.getName()).log(Level.SEVERE, null, ex);
                            dtm.setValueAt("Failed", i, 3);
                        }
                    }
                }
                resultsLabel.setText(copied + " files copied. Finished!");
            }
        }
    });
    btnPanel.add(cancelBtn);
    btnPanel.add(moveBtn);
    btnPanel.add(resultsLabel);

    filesFrame.revalidate();
    filesFrame.setVisible(true);

    callerFrame.setVisible(false);
}