Reputation:
so I have this code:
public class Welcome extends JFrame {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Welcome frame = new Welcome();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Welcome() {
setAlwaysOnTop(true);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 1024, 768);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setScrollPosition(new Point(0, 0));
scrollPane.setBounds(10, 10, 998, 719);
contentPane.add(scrollPane);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"First Name", "Last Name", "Cellphone"
}
));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
table.setBounds(10, 10, 998, 718);
contentPane.add(table);
}
}
What I am trying to do is fill the jtable with data from the database, but I don't know how. I am thinking of having a method outside of the initialize method, and create a multidimensional array in there for the jTable to reference. I am targeting this line of code:
new Object[][] {
}
How could I make a multidimensional Object array and populate it with data from the database?? The gui was done using eclipse window builder.
Thanks, Josh
Upvotes: 1
Views: 1679
Reputation: 109823
How could I make a multidimensional Object array and populate it with data from the database?? The gui was done using eclipse window builder.
not I'm disagree everything is simpler, quite simple
Database Objects
and JDBC methods
returns by default (ResultSet
) only columns
and rows
JTable
(its underlaying XxxTableModel
) is about columns
and rows
only
Swing JComponents
and its XxxModel
are designated to be reusable, both model
and view
too
back to JDBC
and XxxTableModel
, my following sentences, all points will be about XxxTableModel
there no issue to add, remove, modify row(s), reset to null , modify cell value etc
ResultSet
returns (if isn'm null) collections
of rows and you would need to loop inside, row by row
each of row representing a new row for XxxTableModel
if isnt there real and important reason to start with DefaultTableModel
shortcut, but I'd be suggest to use Vector<Vector<Object>>
for DefaultTableModel
, and util.List
or Vector<Vector<Object>>
for AbstractTableModel
as underlaying array
everything important that I don't want to wrote here is very godd described in Oracle tutorial How to use Tables
Initial Thread
and Event Dispatch Thread
Upvotes: 1
Reputation: 12985
There are two common ways to do this, three if you stretch a bit:
ONE
Change this line a bit:
new DefaultTableModel(
loadTableData(),
new String[] {
"First Name", "Last Name", "Cellphone"
}
)
Then you create a method there in the same class that calls the right classes and loads the database info and creates the 2D Object array containing it.
TWO
Add a place for the data and a way to set it:
private Object[] theData;
public setData(Object[] data) {
theData = data;
}
Then that line we changed in ONE looks like this:
new DefaultTableModel(
theData,
new String[] {
"First Name", "Last Name", "Cellphone"
}
)
Finally, you have to have a different class with the main method in it that will load the data from the database, create the instance of the Welcome class and inject the database data by calling setData(loadedData)
. (This could be done by Spring or Guice as well. Or just call the setter.)
THREE
The third way is to create a whole new class that is a TableModel
that knows how to read data from the database. You pass an instance of that class to the JTable
and when the data is needed, it can be read.
This is documented lots of places on the web. I'd Google for "Java Swing TableModel database" to find some examples.
Upvotes: 1