Example usage for javax.swing.table DefaultTableModel setValueAt

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

Introduction

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

Prototype

public void setValueAt(Object aValue, int row, int column) 

Source Link

Document

Sets the object value for the cell at column and row.

Usage

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();/* w ww  .  j  av  a2 s. com*/
    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 void setValueAt(JTable table, Object element, int row, int col) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.setValueAt(element, row, col);
}

From source file:MainWindowLogic.java

static private void ponumerujWiersze(JTable where) {
    DefaultTableModel tmp = (DefaultTableModel) where.getModel();
    for (int i = 0; i < tmp.getRowCount(); i++) {
        String val = (i + 1) + ".";
        tmp.setValueAt(val, i, 0);

    }/*from  w w w  .  j  a  va2s  .  c  om*/
}

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 ww w  .j  a v a2 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);
}

From source file:cz.muni.fi.mushroomhunter.restclient.LocationUpdateSwingWorker.java

@Override
protected void done() {
    try {//from ww  w  . java  2 s.c o  m
        get();
    } catch (Exception e) {
        restClient.getlMessageLocations().setText("ERROR: server is unavailable.");
        return;
    }
    DefaultTableModel model = (DefaultTableModel) restClient.getTblLocation().getModel();
    try {
        model.setValueAt(restClient.getTfLocationName().getText(), get(), 0);
        model.setValueAt(restClient.getTfLocationDescription().getText(), get(), 1);
        model.setValueAt(restClient.getTfLocationNearCity().getText(), get(), 2);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

From source file:cz.muni.fi.mushroomhunter.restclient.MushroomUpdateSwingWorker.java

@Override
protected void done() {
    try {//  w  w w .  j a  v a2  s .co  m
        get();
    } catch (Exception e) {
        restClient.getlMessageMushrooms().setText("ERROR: server is unavailable.");
        return;
    }
    DefaultTableModel model = (DefaultTableModel) restClient.getTblMushroom().getModel();
    try {
        model.setValueAt(restClient.getTfMushroomName().getText(), get(), 0);
        model.setValueAt(restClient.getComboBoxMushroomType().getSelectedItem().toString(), get(), 1);
        model.setValueAt(restClient.getComboBoxMushroomStartOfOccurence().getSelectedItem().toString(), get(),
                2);
        model.setValueAt(restClient.getComboBoxMushroomEndOfOccurence().getSelectedItem().toString(), get(), 3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

From source file:pl.piotrsukiennik.jbrain.gui.ModelPanel.java

protected TableModel convert2Model(ConnectionsDTO connectionsDTO) {
    String[] layers = connectionsDTO.getLayers();
    double[][] connections = connectionsDTO.getConnections();
    DefaultTableModel tableModel = new DefaultTableModel(layers.length, layers.length + 1);
    for (int i = 0; i < layers.length; i++) {
        tableModel.setValueAt(layers[i], i, 0);
    }//ww w .ja  v  a 2  s. com
    for (int i = 0; i < connections.length; i++) {
        for (int j = 0; j < connections.length; j++) {
            double connection = connections[i][j];
            if (connection != 0) {
                tableModel.setValueAt(connections[i][j], i, j + 1);
            }
        }
    }
    List<String> labels = new ArrayList<String>();
    labels.add("");
    labels.addAll(Arrays.asList(connectionsDTO.getLayers()));
    tableModel.setColumnIdentifiers(labels.toArray());
    return tableModel;
}

From source file:gui.ResultadoDistFreq.java

public void preencherTabela(ArrayList<String> valores, int[] freqAbs) {
    DefaultTableModel modelo = (DefaultTableModel) tabelaItens.getModel();
    int totalValores = 0;

    for (int i = 0; i < freqAbs.length; i++) {
        totalValores += freqAbs[i];/*from www  .  jav  a2  s  .co m*/
    }

    modelo.setRowCount(valores.size());
    for (int i = 0; i < modelo.getRowCount(); i++) {
        modelo.setValueAt(("" + (i + 1)), i, 0);
        modelo.setValueAt(valores.get(i), i, 1);
        modelo.setValueAt(String.valueOf(freqAbs[i]), i, 2);
        modelo.setValueAt(String.valueOf((((freqAbs[i] * 1.0) / 25) * 100)), i, 3);
    }
}

From source file:dbseer.gui.panel.DBSeerLiveMonitorPanel.java

public void setTotalNumberOfTransactions(double total) {
    DefaultTableModel model = (DefaultTableModel) monitorTable.getModel();
    model.setValueAt(String.format("%.0f", total), 0, 1);
}

From source file:dbseer.gui.panel.DBSeerLiveMonitorPanel.java

public void setCurrentTPS(double tps) {
    DefaultTableModel model = (DefaultTableModel) monitorTable.getModel();
    model.setValueAt(String.format("%.1f", tps), 1, 1);
}