Reputation: 125
I've started a little side project to learn how to extract web data and learn how to use tables with Java. First time working with tables and 2D arrays so I may be going about this the wrong way.. I am trying to extract data from a site and input it into a table and so far I have been able to get the data. I input the data into a single string separated by commas and new lines(csv) called scheduleCsv
Sun 02-16-14 09:45 PM,1,REAL TIRED,JUST WACK AT IT,2 - 4,
Sun 02-23-14 08:10 PM,1,BALLERS,REAL TIRED,4 - 11,
Sun 03-02-14 09:00 PM,1,REAL TIRED,EL TRI,1 - 7,
Sun 03-09-14 05:50 PM,1,GO GO POWER RANGERS,REAL TIRED,4 - 9,
Sun 03-16-14 06:40 PM,1,REAL TIRED,GAME OF GROANS,
Sun 03-23-14 09:00 PM,1,HUNGOVER HAT TRICKS,REAL TIRED,
Sun 03-30-14 07:25 PM,1,REAL TIRED,PRESTIGE WORLD WIDE,
Sun 04-06-14 04:20 PM,1,REAL TIRED,BALLERS,
Is it possible to input this data into a JTable? I have tried a few different methods from searching around and so far have been unsuccessful. I have another array the specifies column names
String[] columnNames = {"Date","Field", "Home Team","Visitor Team", "Score"};
and my table is set up as so
final JTable scheduleTable = new JTable(data,columnNames);
I believe the "data" should be a 2D array..?
Upvotes: 0
Views: 1429
Reputation: 1374
As mentioned by Max google it, there are many good tutorials. For example: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
To read the cvs file I would recommend you to load into List instead of array.
Upvotes: 1
Reputation: 54639
Depending on how the data is obtained, this should not be so difficult:
"\n"
to obtain individual lines,
to obtain individual columnsAn example:
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class TableContentsTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"Date","Field", "Home Team","Visitor Team", "Score"};
String[][] contents = createTableContents();
JTable table = new JTable(contents, columnNames);
f.getContentPane().add(table);
f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static String[][] createTableContents()
{
String input =
"Sun 02-16-14 09:45 PM,1,REAL TIRED,JUST WACK AT IT,2 - 4,"+"\n"+
"Sun 02-23-14 08:10 PM,1,BALLERS,REAL TIRED,4 - 11,"+"\n"+
"Sun 03-02-14 09:00 PM,1,REAL TIRED,EL TRI,1 - 7,"+"\n"+
"Sun 03-09-14 05:50 PM,1,GO GO POWER RANGERS,REAL TIRED,4 - 9,"+"\n"+
"Sun 03-16-14 06:40 PM,1,REAL TIRED,GAME OF GROANS,"+"\n"+
"Sun 03-23-14 09:00 PM,1,HUNGOVER HAT TRICKS,REAL TIRED,"+"\n"+
"Sun 03-30-14 07:25 PM,1,REAL TIRED,PRESTIGE WORLD WIDE,"+"\n"+
"Sun 04-06-14 04:20 PM,1,REAL TIRED,BALLERS,"+"\n";
String[] lines = input.split("\n");
String result[][] = new String[lines.length][];
for (int i=0; i<lines.length; i++)
{
result[i] = Arrays.copyOf(lines[i].split(","), 5);
}
return result;
}
}
Upvotes: 1