Guest User

Untitled

a guest
Feb 26th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. /**
  2. * Write a description of class UI here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10. import javax.swing.JScrollPane;
  11. import java.awt.CardLayout;
  12. import javax.swing.table.DefaultTableModel;
  13.  
  14. public class UI implements ItemListener
  15. {
  16. JPanel cards;
  17. final static String BUTTONPANEL = "View Inventory";
  18. final static String TEXTPANEL = "Add Inventory";
  19. final static String LOGIN = "Login";
  20. String passwordM = "test";
  21. String itemName = "";
  22. String priceNum = "";
  23. String quantityNum = "";
  24. DefaultTableModel tableSet;
  25. public void addComponentToPane(Container pane)
  26. {
  27. JPanel comboBoxPane = new JPanel();
  28. JTabbedPane tabbedPane = new JTabbedPane();
  29. JButton add = new JButton("ADD");
  30. JButton login = new JButton("LOGIN");
  31.  
  32. JTextField item = new JTextField("", 20);
  33. JTextField price = new JTextField("", 20);
  34. JTextField quantity = new JTextField("", 20);
  35.  
  36. JTextField user = new JTextField("", 20);
  37. JTextField pass = new JTextField("", 20);
  38.  
  39. JPanel card = new JPanel();
  40.  
  41. String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL, LOGIN};
  42. JComboBox cb = new JComboBox(comboBoxItems);
  43. cb.setEditable(false);
  44. cb.addItemListener(this);
  45. comboBoxPane.add(cb);
  46.  
  47. //creating the card
  48. card.add(new Label("Login: "));
  49. card.add(user);
  50. card.add(pass);
  51. card.add(login);
  52.  
  53. JPanel card1 = new JPanel();
  54.  
  55. JPanel card2 = new JPanel();
  56. card2.add(new JLabel("ITEM"));
  57. card2.add(item);
  58. card2.add(new JLabel("PRICE"));
  59. card2.add(price);
  60. card2.add(new JLabel("QUANTITY"));
  61. card2.add(quantity);
  62. card2.add(add);
  63.  
  64. JPanel card3 = new JPanel();
  65. card3.add(new JTextField("WRITE",20));
  66.  
  67. tabbedPane.addTab(LOGIN, card);
  68. tabbedPane.addTab(BUTTONPANEL, card1);
  69. tabbedPane.addTab(TEXTPANEL, card2);
  70.  
  71. //creating the Jtable
  72. String[] columns = new String[] {
  73. "Item", "Price", "Quantity"
  74. };
  75. //actual data for the table in a 2d array
  76. Object[][] data = new Object[][] {
  77. };
  78. //create table with dataNum
  79. tableSet = new DefaultTableModel(data, columns);
  80. JTable table = new JTable(tableSet);
  81. //add the table to the frame
  82. card1.add(new JScrollPane(table));
  83. card1.setVisible(true);
  84.  
  85. login.addActionListener(new ActionListener() {
  86. public void actionPerformed(ActionEvent e) {
  87. String username = user.getText();
  88.  
  89. String passName = pass.getText();
  90. if(passName == passwordM)
  91. {
  92. card.add(new JLabel("Welcome manager!"));
  93. }
  94. else
  95. {
  96. card.add(new JLabel("Try again buddy."));
  97. }
  98.  
  99.  
  100. }
  101. } );
  102. add.addActionListener(new ActionListener() {
  103. public void actionPerformed(ActionEvent e) {
  104. itemName = item.getText();
  105. priceNum = price.getText();
  106. quantityNum= quantity.getText();
  107. tableSet.addRow(new String[] {itemName, priceNum, quantityNum});
  108. item.setText("");
  109. price.setText("");
  110. quantity.setText("");
  111. }
  112. } );
  113. pane.add(tabbedPane, BorderLayout.CENTER);
  114. }
  115.  
  116. public void itemStateChanged(ItemEvent evt) {
  117. CardLayout cl = (CardLayout)(cards.getLayout());
  118. cl.show(cards, (String)evt.getItem());
  119. }
  120.  
  121. private static void createAndShowGUI() {
  122. //Create and set up the window.
  123. JFrame frame = new JFrame("CardLayoutDemo");
  124. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  125.  
  126. //Create and set up the content pane.
  127. UI demo = new UI();
  128.  
  129. //Display the window.
  130. frame.pack();
  131. frame.setSize(300,300);
  132. frame.setVisible(true);
  133.  
  134. demo.addComponentToPane(frame.getContentPane());
  135. //display
  136. frame.pack();
  137. frame.setVisible(true);
  138. }
  139.  
  140. public static void main(String[] args)
  141. {
  142.  
  143. try {
  144.  
  145. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  146. } catch (UnsupportedLookAndFeelException ex) {
  147. ex.printStackTrace();
  148. } catch (IllegalAccessException ex) {
  149. ex.printStackTrace();
  150. } catch (InstantiationException ex) {
  151. ex.printStackTrace();
  152. } catch (ClassNotFoundException ex) {
  153. ex.printStackTrace();
  154. }
  155. /* Turn off metal's use of bold fonts */
  156. UIManager.put("swing.boldMetal", Boolean.FALSE);
  157.  
  158. //Schedule a job for the event dispatch thread:
  159. //creating and showing this application's GUI.
  160. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  161. public void run() {
  162. createAndShowGUI();
  163. }
  164. });
  165. }
  166.  
  167. }
Add Comment
Please, Sign In to add comment