how to store jtable value in multidimensional array?

how to store jtable value in multidimensional array?

i want to store the value of jtable in multidimensional array,with type double. how to store jtable value in multidimensional array?

View Answers

March 16, 2011 at 4:36 PM

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;

class GetJTableData{
    public static Object[][] getTableData (JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
        for (int j = 0 ; j < nCol ; j++)
            tableData[i][j] = dtm.getValueAt(i,j);
    return tableData;
}
    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("Getting Cell Values in JTable");
    frame.setLayout(null);
    String data[][] = {{"A","Delhi"},
                      {"B","Mumbai"},
                      {"C","Chennai"},
                      {"D","Kolkata"}};
    String col[] = {"Name","Address"};    
    DefaultTableModel model = new DefaultTableModel(data, col);
    final JTable table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    pane.setBounds(10,10,300,200);
    JButton b=new JButton("Get");
    b.setBounds(10,250,80,20);
    frame.add(pane);
    frame.add(b);
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
Object[][] A=getTableData(table);
for (int i=0 ; i < A.length ; i++)
        {     System.out.println();
            for  (int j=0 ; j < A[i].length ; j++){
                System.out.print(A[i][j].toString()+" ");
                  }
        }
     }
    });
    frame.setSize(350,350);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setVisible(true);
    }
}

Ads









Related Tutorials/Questions & Answers:
how to store jtable value in multidimensional array?
how to store jtable value in multidimensional array?
Advertisements
How to collect Java input field value display into Jtable?
How to store two integer value in a separate file and retrieve those values
How to add JTable in JPanel
JavaScript array multidimensional
To store value in session & display it
store dynamic generated textbox value into database
JavaScript array multidimensional
How to concatenate two arrays in Java?
How to insert rows in jTable?
swap two integer arrays
swap two integer arrays
swap two integer arrays
store value in checkbox from gridview
JTable
Arrays
arrays
how to compare 2 arrays using java?
JTable
Arrays
how to create a header in jtable using java swing
How to use JTable with MS-Access
How to use JTable with MS-Access
store html value in the sqllite 3 php
jtable
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
How to store image into database
How to store an image in database
Arrays
JTable

Ads