Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. public class ShopFrame extends JFrame {
  2.  
  3. public static void main(String[] args) {
  4. EventQueue.invokeLater(new Runnable() {
  5. public void run() {
  6. try {
  7. ShopFrame frame = new ShoppingFrame();
  8. frame.setVisible(true);
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. });
  14. }
  15.  
  16. public ShoppingFrame() {
  17. setTitle("Shopping Applicationrn");
  18. // Adaptive window size
  19. setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
  20. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. setBounds(100, 100, 900, 556);
  22. GridBagLayout gridBagLayout = new GridBagLayout();
  23. gridBagLayout.columnWidths = new int[]{45, 0, 20, 0, 45, 1, 45, 0, 20, 0, 45, 0};
  24. gridBagLayout.rowHeights = new int[]{45, 0, 0, 0, 0, 0, 20, 0, 0, 20, 0, 0, 20, 0, 30, 0, 45, 0};
  25. 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};
  26. 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};
  27. getContentPane().setLayout(gridBagLayout);
  28.  
  29. JPanel panel = new JPanel();
  30. panel.setMinimumSize(new Dimension(5, 20));
  31. panel.setBackground(SystemColor.scrollbar);
  32. GridBagConstraints gbc_panel = new GridBagConstraints();
  33. gbc_panel.gridheight = 17;
  34. gbc_panel.insets = new Insets(0, 0, 0, 5);
  35. gbc_panel.fill = GridBagConstraints.BOTH;
  36. gbc_panel.gridx = 5;
  37. gbc_panel.gridy = 0;
  38. getContentPane().add(panel, gbc_panel);
  39.  
  40. //Importer.productsModel.addElement("This gets added to the empty DefaultListModel, but not the Importer Class's info.");
  41. JList<String> productBox = new JList<String>(Importer.productsModel);
  42. GridBagConstraints gbc_list = new GridBagConstraints();
  43. gbc_list.gridheight = 4;
  44. gbc_list.gridwidth = 3;
  45. gbc_list.insets = new Insets(0, 0, 5, 5);
  46. gbc_list.fill = GridBagConstraints.BOTH;
  47. gbc_list.gridx = 1;
  48. gbc_list.gridy = 2;
  49. getContentPane().add(productBox, gbc_list);
  50.  
  51. class Importer {
  52.  
  53. // Declarations
  54. public static ArrayList<Product> productList = new ArrayList<Product>();
  55. public static DefaultListModel<String> productsModel = new DefaultListModel<String>();
  56.  
  57. // Connect to database
  58. private static Connection connect = null;
  59.  
  60. // Running parts of SQL
  61. private static Statement statement = null;
  62.  
  63. // Running parts of SQL that take parameters
  64. private static PreparedStatement preparedStatement = null;
  65.  
  66. // Result from a SELECT CELL statement
  67. private static ResultSet resultSetProducts = null;
  68. }
  69.  
  70. public static void main(String[] args) {
  71. try {
  72. connect = DriverManager.getConnection(
  73. "jdbc:mysql://localhost/shop?useSSL=true"
  74. + "&user=root&password=Pa$$");
  75.  
  76. statement = connect.createStatement();
  77.  
  78. // This imports products into a collection
  79. resultSetProducts = statement.executeQuery(
  80. "select * from supermarketx.products");
  81. exportProductsAsCollection(resultSetProducts, productList, productsModel);
  82.  
  83. connect.close();
  84.  
  85. } catch (Exception e) {
  86. System.out.println(e);
  87. }
  88. }
  89.  
  90. public static void exportProductsAsCollection(ResultSet resultSetProducts, ArrayList<Product> productList, DefaultListModel<String> productModel) throws SQLException {
  91.  
  92. while (resultSetProducts.next()) {
  93. // Adds each product object (Product) into ArrayList
  94. productList.add(new Product(resultSetProducts.getString("ProductName"), resultSetProducts.getDouble("Price")));
  95. }
  96. // Exports names of products for use in JList
  97. String[] arr = new String[productList.size()];
  98. for (int i = 0; i < arr.length; i++)
  99. {
  100. arr[i] = productList.get(i).getProductName();
  101. productModel.addElement(arr[i]);
  102. // To test
  103. System.out.println(arr[i]);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement