Pepsi Day
Pepsi Day

Reputation: 1

Getting single INT value from jTable

I have a table with two values: A and B Is it possible to get A as int using getSelectedRow()?

At this moment I got this, it cannot find symbol for variable A

 DefaultTableModel tm = (DefaultTableModel)jTabela.getModel();    
 int A = Integer.parseInt( tm.getValueAt(jTabela.A, getSelectedRow()).toString() );

Upvotes: 0

Views: 962

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You're putting the row number into the wrong parameter. Per the JTable API the getValueAt(...) method takes two int parameters, the first of which is the row index and the second is the column index. So you'll want something like:

DefaultTableModel tm = (DefaultTableModel)jTabela.getModel(); 
int row = tm.getSelectedRow();
if (row == -1) {
   return;
}
Object value = jTabela.getValueAt(row, whateverYourColumnIs);
int intValue = value != null ? Integer.parseInt(value.toString()) : 0;

For example:

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class TableExample extends JPanel {
   private static final String[] COLUMNS = {"Name", "Value"};
   private DefaultTableModel model = new DefaultTableModel(COLUMNS, 0) {
      public java.lang.Class<?> getColumnClass(int columnIndex) {
         Object value = getValueAt(0, columnIndex);
         if (value == null) {
            return super.getColumnClass(columnIndex);
         } else {
            return value.getClass();
         }
      };
   };
   private JTable table = new JTable(model);

   public TableExample() {
      for (int i = 0; i < 10; i++) {
         String name = "Row " + (i + 1);
         int value = (i + 1) * 10;
         Vector<Object> row = new Vector<>();
         row.add(name);
         row.add(value);
         model.addRow(row);
      }
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

         @Override
         public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
               return;
            }
            int row = table.getSelectedRow();
            if (row == -1) {
               return;
            }
            Object value = table.getValueAt(row, 1); // numbers are in row 1
            if (value != null) {
               System.out.println("Selected value: " + value);
               int intValue = Integer.parseInt(value.toString());
               // can use value here
            }
         }
      });

      setLayout(new BorderLayout());
      add(new JScrollPane(table));
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Table Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TableExample());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Upvotes: 1

Related Questions