[jTable - Swing] zmiana wartości w tabeli

Odpowiedz Nowy wątek
Farmazon
2009-11-01 18:20
Farmazon
0

Cześć,
Mam problem. Mam tabelę 3x5 i chcę stworzyć taką funkcję, która w każdej komórce podniesie ich wartość do kwadratu.
Muszę pobrać wartość każdej komórki tylko nie wiem jak [glowa]
Ta funkcja zamienia mi wartości komórek na 0. Jak ją przerobić, żeby żeby liczyła kwadraty liczb?
Kod ten znajduję się pod switch(x) case 0::

for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 3; j++) {
                        b = 0;
                        jTable1.setValueAt(b, i, j);
                    }
                }

Podaję też cały kod:

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;

public class Ramka extends javax.swing.JFrame {

    int x;

    /** Creates new form Ramka */
    public Ramka() {
        initComponents();

    }

        @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jSpinner1 = new javax.swing.JSpinner();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Przedział losowania");

        jLabel2.setText("OD");

        jLabel3.setText("DO");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "1", "2", "3"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Losuj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                losuj(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kwadrat", "wielokrotniść", "zerowanie" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                funkcja(evt);
            }
        });

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(2, 2, 5, 1));

        jButton2.setText("Przelicz");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                przelicz(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(106, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void losuj(java.awt.event.ActionEvent evt) {
        int a;
        try {
            int k = Integer.parseInt(jTextField1.getText());
            int l = Integer.parseInt(jTextField2.getText());
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 3; j++) {
                    a = (int) (k + (l - k + 1) * Math.random());
                    jTable1.setValueAt(a, i, j);
                }
            }
            JOptionPane.showMessageDialog(null, "Element minimalny= " + min());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Bład danych");
        }
        try {
            BufferedReader we = new BufferedReader(new InputStreamReader(System.in));
            String np = JOptionPane.showInputDialog(this, "podaj nazwe pliku wyników: ");
            FileWriter wy = new FileWriter(np + ".txt");
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 3; j++) {
                    wy.write(" " + jTable1.getValueAt(i, j));
                }
                wy.write("\n");
            }
            wy.close();
        } catch (Exception e) {
            e.getMessage();
        }
    }

    private void funkcja(java.awt.event.ItemEvent evt) {
        x = jComboBox1.getSelectedIndex();

    // TODO add your handling code here:
    }

    private void przelicz(java.awt.event.ActionEvent evt) {
        int b;
        int c;
        switch (x) {
            case 0:
                    for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 3; j++) {
                        jTable1.getValueAt(i, j);

                    }
                }
                break;
            case 1:

                break;
            case 2:
                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 3; j++) {
                        b = 0;
                        jTable1.setValueAt(b, i, j);
                    }
                }
                break;
            default:
                break;
        }        // TODO add your handling code here:
    }

    int min() {
        int m = (Integer) jTable1.getValueAt(0, 0);
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            for (int j = 0; j < jTable1.getColumnCount(); j++) {
                int a = (Integer) jTable1.getValueAt(i, j);
                if (a < m) {
                    m = a;
                }
            }
        }
        return m;
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Ramka().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
}

Pozostało 580 znaków

thebill
2009-11-01 21:58
thebill
0

Analogicznie do metody setValueAt istnieje getValueAt

if (jTable.getValueAt(i, j) != null && 
                jTable.getValueAt(i, j) instanceof Integer) {
            // obliczam kwadrat
            final int sqr = (Integer) jTable.getValueAt(i, j) * 
                (Integer) jTable.getValueAt(i, j);
            // ustawiam wartość
            jTable.setValueAt(sqr, i, j);
}

Pozostało 580 znaków

Farmazon
2009-11-01 23:25
Farmazon
0

Dzięki wielkie [browar]
A mam pytanie jak zrobić żeby wczytać argumenty do tablicy z pliku txt ?
Tutaj kod odpowiedzialny za zapis do pliku txt. Jak to przerobić, żeby odczytywało z pliku txt?

try {
            BufferedReader we = new BufferedReader(new InputStreamReader(System.in));
            String np = JOptionPane.showInputDialog(this, "podaj nazwe pliku wyników: ");
            FileWriter wy = new FileWriter(np + ".txt");
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 3; j++) {
                    wy.write(" " + jTable1.getValueAt(i, j));
                }
                wy.write("\n");
            }
            wy.close();
        } catch (Exception e) {
            e.getMessage();
        }

Pozostało 580 znaków

Odpowiedz

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0