Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Applicationrn");
- // 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);
- 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 supermarketx.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]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement