I have a pretty basic understanding of Java, so forgive me if I'm asking an incredibly simple question.
I have two user-created arrays to populate a JTable with. I was able to figure out how to populate the column names with the first array, but I can't figure out how to fill all the rows with data from the second array.
I've searched the forums for topics related to my problem, but couldn't find anything. I've read the How To Use Tables java.sun.com tutorial over and over, but it doesn't seem to pertain to my situation.
Here's my code. rowdata[][] (which is sent from the main program not shown) is the array that I can't figure out how to populate the JTable with. What should the line "static Object[][] data = ;" read so that 'data' will fill into the JTable? If it helps, rowdata[][] is nothing but strings (String[x][4]).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class test2class extends JPanel {
static String[] columnNames = {""};
static Object[][] data = ;
public test2class() {
super(new GridLayout(1,0));
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
test2class newContentPane = new test2class();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void newTable(String columns[], String rowdata[][]) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
columnNames = columns;
data = rowdata;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Thanks in advanced for any help.