I have 2 classes called ShopFrame(JFrame) and Importer(Has some fields)
The JFrame has a JList, and for that to be populated it summons a DefaultListModel from the Importer class, but it ALWAYS appears to be empty when I run the JFrame class.
In the Importer class, I declare an empty DefaultListModel, and below that in the main method, I use methods to populate it with database information. If I write a println loop within the method, it shows me that all the objects were successfully added to the declared DefaultListModel. The problem is that the JFrame seems to summon the declared object before it gets populated.
This might be a really silly inheritence question, or something! But please do help me if you can.
I didn't think this needed an example but please ask if required, this seems like more of a simple logic question.
Thanks!
ShopFrame Class
public class ShopFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShopFrame frame = new ShoppingFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShoppingFrame() {
setTitle("Shopping Application\r\n");
// Adaptive window size
setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 900, 556);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{45, 0, 20, 0, 45, 1, 45, 0, 20, 0, 45, 0};
gridBagLayout.rowHeights = new int[]{45, 0, 0, 0, 0, 0, 20, 0, 0, 20, 0, 0, 20, 0, 30, 0, 45, 0};
gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
getContentPane().setLayout(gridBagLayout);
JPanel panel = new JPanel();
panel.setMinimumSize(new Dimension(5, 20));
panel.setBackground(SystemColor.scrollbar);
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 17;
gbc_panel.insets = new Insets(0, 0, 0, 5);
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 5;
gbc_panel.gridy = 0;
getContentPane().add(panel, gbc_panel);
//Importer.productsModel.addElement("This gets added to the empty DefaultListModel, but not the Importer Class's info.");
JList<String> productBox = new JList<String>(Importer.productsModel);
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.gridheight = 4;
gbc_list.gridwidth = 3;
gbc_list.insets = new Insets(0, 0, 5, 5);
gbc_list.fill = GridBagConstraints.BOTH;
gbc_list.gridx = 1;
gbc_list.gridy = 2;
getContentPane().add(productBox, gbc_list);
}
Importer Class
class Importer {
// Declarations
public static ArrayList<Product> productList = new ArrayList<Product>();
public static DefaultListModel<String> productsModel = new DefaultListModel<String>();
// Connect to database
private static Connection connect = null;
// Running parts of SQL
private static Statement statement = null;
// Running parts of SQL that take parameters
private static PreparedStatement preparedStatement = null;
// Result from a SELECT CELL statement
private static ResultSet resultSetProducts = null;
}
public static void main(String[] args) {
try {
connect = DriverManager.getConnection(
"jdbc:mysql://localhost/shop?useSSL=true"
+ "&user=root&password=Pa$$");
statement = connect.createStatement();
// This imports products into a collection
resultSetProducts = statement.executeQuery(
"select * from shop.products");
exportProductsAsCollection(resultSetProducts, productList, productsModel);
connect.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void exportProductsAsCollection(ResultSet resultSetProducts, ArrayList<Product> productList, DefaultListModel<String> productModel) throws SQLException {
while (resultSetProducts.next()) {
// Adds each product object (Product) into ArrayList
productList.add(new Product(resultSetProducts.getString("ProductName"), resultSetProducts.getDouble("Price")));
}
// Exports names of products for use in JList
String[] arr = new String[productList.size()];
for (int i = 0; i < arr.length; i++)
{
arr[i] = productList.get(i).getProductName();
productModel.addElement(arr[i]);
// To test
System.out.println(arr[i]);
}
}
You start your application. The main method of ShopFrame is called. This calls the constructor of ShopFrame. This constructor creates a JList with Importer.productsModel
as its model.
Note that in the sequence described above, the main method of Importer is never called anywhere: an application has only one entry point, not two. Since it's that method that populates the productsModel, it's never populated, and is thus empty.
Remove that main method, and transform it into a method that creates, populates, and returns the data you need in your frame. Remove all those public static variables.
Call that new method from your frame, and populate the JList with what is returned.
Swing is quite a complex API. I would familirize with the concepts of classes, objects, methods before going further with Swing. Also note that your question has nothing to do with inheritance, despite what the title says. So that's probably another concept you should familiarize with.