how to store jtable value in multidimensional array?

Ads
 

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:35 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
How to concatenate two arrays in Java?
How to insert rows in jTable?
To store value in session & display it
JavaScript array multidimensional
store dynamic generated textbox value into database
swap two integer arrays
swap two integer arrays
swap two integer arrays
how to compare 2 arrays using java?
How to use JTable with MS-Access
How to use JTable with MS-Access
how to create a header in jtable using java swing
store value in checkbox from gridview
How to save value in JSP
how to archive cllocation value
how to archive cllocation value
JTable
How to store image into database
How to store an image in database
Arrays
arrays
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
JTable
store html value in the sqllite 3 php
How to create a JTable cell containing Image hyperlink?

Ads