Guest User

Untitled

a guest
Jan 13th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.44 KB | None | 0 0
  1. JLabel change text if JDialog is visible
  2. import javax.swing.*;
  3. import javax.swing.border.CompoundBorder;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. public class HotelRoomsGUI extends JPanel{
  7. private JTabbedPane mainJTP;
  8. private JPanel classicTab,deluxeTab,presidentialTab,classicSubPanel,deluxeSubPanel,presidentialSubPanel;
  9. private String classicRoomNo[] = {"101","102","103","104","105","106","107","108","109","101","111","112"};
  10. private String deluxeRoomNo[] = {"201","202","203","204","205","206","207","208","209","201","211","212"};
  11. private String presidentialRoomNo[] = {"301","302","303","304","305","306","307","308","309","301","311","312"};
  12. private JButton[] classicRoom, deluxeRoom, presidentialRoom;
  13. private JLabel[] inputLabels;
  14. ButtonHandler bh;
  15. public HotelRoomsGUI(){
  16.  
  17. setLayout(null);
  18. setBackground(new Color(90,90,90));
  19. add(tabbedPane());
  20.  
  21. }
  22. public JPanel classic()
  23. {
  24. classicTab = new JPanel();
  25. classicTab.setBackground(new Color(70,70,70));
  26. classicTab.setLayout(null);
  27. classicSubPanel();
  28. return classicTab;
  29. }
  30. public JPanel classicSubPanel()
  31. {
  32. classicSubPanel = new JPanel();
  33. classicSubPanel.setBounds(10,10,605,455);
  34. classicSubPanel.setLayout(new GridLayout(4,3,10,10));
  35. classicSubPanel.setBackground(new Color(70,70,70));
  36. classicTab.add(classicSubPanel);
  37. return classicTab;
  38. }
  39. public JPanel deluxe()
  40. {
  41. deluxeTab = new JPanel();
  42. deluxeTab.setBackground(new Color(70,70,70));
  43. deluxeTab.setLayout(null);
  44. deluxeSubPanel();
  45. return deluxeTab;
  46. }
  47. public JPanel deluxeSubPanel()
  48. {
  49. deluxeSubPanel = new JPanel();
  50. deluxeSubPanel.setBounds(10,10,605,455);
  51. deluxeSubPanel.setLayout(new GridLayout(4,3,10,10));
  52. deluxeSubPanel.setBackground(new Color(70,70,70));
  53. deluxeTab.add(deluxeSubPanel);
  54. return deluxeSubPanel;
  55. }
  56. public JPanel presidential()
  57. {
  58. presidentialTab = new JPanel();
  59. presidentialTab.setBackground(new Color(70,70,70));
  60. presidentialTab.setLayout(null);
  61. presidentialSubPanel();
  62. return presidentialTab;
  63. }
  64. public JPanel presidentialSubPanel()
  65. {
  66. presidentialSubPanel = new JPanel();
  67. presidentialSubPanel.setBounds(10,10,605,455);
  68. presidentialSubPanel.setLayout(new GridLayout(4,3,10,10));
  69. presidentialSubPanel.setBackground(new Color(70,70,70));
  70. presidentialTab.add(presidentialSubPanel);
  71. return presidentialSubPanel;
  72. }
  73. //Holder of buttons
  74. public JTabbedPane tabbedPane()
  75. {
  76. UIManager.put("TabbedPane.selected", Color.ORANGE);
  77. mainJTP = new JTabbedPane();
  78. mainJTP.setBackground(Color.WHITE);
  79. mainJTP.setBounds(3,1,630,500);
  80. mainJTP.addTab("Classic",classic());
  81. mainJTP.addTab("Deluxe",deluxe());
  82. mainJTP.addTab("Presidential",presidential());
  83. rooms();
  84. return mainJTP;
  85. }
  86. //Labels that will be display on the RoomProfile class
  87. public JLabel[] inputLabels()
  88. {
  89. inputLabels = new JLabel[10];
  90. for(int x = 0; x<inputLabels.length;x++)
  91. {
  92. inputLabels[x] = new JLabel();
  93. inputLabels[x].setForeground(Color.WHITE);
  94. }
  95. return inputLabels;
  96. }
  97. public void rooms()
  98. {
  99. bh = new ButtonHandler();
  100. presidentialRoom = new JButton[presidentialRoomNo.length];
  101. deluxeRoom = new JButton[deluxeRoomNo.length];
  102. classicRoom = new JButton[classicRoomNo.length];
  103. for(int x = 0;x<classicRoomNo.length;x++){
  104. //classic rooms
  105. ImageIcon imageC = new ImageIcon("C:\Users\John\workspace" +
  106. "\SystemTest\src\Images\classicRooms.JPG"); // image
  107. classicRoom[x] = new JButton(classicRoomNo[x],imageC);
  108. classicRoom[x].setBackground(Color.WHITE);
  109. classicRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
  110. BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
  111. classicRoom[x].addActionListener(bh);
  112. classicSubPanel.add(classicRoom[x]);
  113. //deluxe rooms
  114. ImageIcon imageD = new ImageIcon("C:\Users\John\workspace" +
  115. "\SystemTest\src\Images\deluxeRooms.JPG"); // image
  116. deluxeRoom[x] = new JButton(deluxeRoomNo[x],imageD);
  117. deluxeRoom[x].setBackground(Color.WHITE);
  118. deluxeRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
  119. BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
  120. deluxeRoom[x].addActionListener(bh);
  121. deluxeSubPanel.add(deluxeRoom[x]);
  122. //presidential rooms
  123. ImageIcon imageP = new ImageIcon("C:\Users\John\workspace" +
  124. "\SystemTest\src\Images\presidentialRooms.JPG"); // image
  125. presidentialRoom[x] = new JButton(presidentialRoomNo[x],imageP);
  126. presidentialRoom[x].setBackground(Color.WHITE);
  127. presidentialRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
  128. BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
  129. presidentialRoom[x].addActionListener(bh);
  130. presidentialSubPanel.add(presidentialRoom[x]);
  131.  
  132. }
  133. }
  134. public class ButtonHandler implements ActionListener
  135. {
  136. public void actionPerformed(ActionEvent e)
  137. {
  138. //to get what button is accessed
  139. Room room = new Room(e.getActionCommand());
  140. //get all info using the room no. got from getActionCommand();
  141. GuestsInfo info = new GuestsInfo(room.getGuestID());
  142. String data[] = {info.getFirstName()+" "+info.getLastName(),info.getAge(),info.getGender(),
  143. info.getContactNo(),"Today",info.getTime(),"Tomorrow",room.getRoomNo(),room.getRoomType()};
  144. RoomProfile prof = new RoomProfile();
  145. if(prof.isVisible())
  146. {
  147. System.out.print("test");
  148. }
  149. else
  150. {
  151.  
  152. //setting text on label
  153. prof.setVisible(true);
  154. inputLabels();
  155. for(int i = 0; i<data.length; i++){
  156. inputLabels[i].setText(" "+data[i]);
  157. System.out.println(""+data[i]);
  158. }
  159. }
  160. }
  161. }
  162.  
  163. }
  164.  
  165. import java.awt.*;
  166. import java.awt.event.*;
  167. import javax.swing.*;
  168.  
  169. public class RoomProfile extends JDialog{
  170.  
  171. private JLabel[] labels;
  172. JLabel inputLabels[];
  173. private String topTextLabels[] = {"Fullname","Age","Gender","Address","Contact No","Arrival", "Time in", "Departure","Room No","Room Type"};
  174. private JButton okB;
  175. private JPanel subFrame,topPanel, bottomLeftPanel,bottomRightPanel;
  176. private JLabel designLabel;
  177. private ButtonHandler bh;
  178. private HotelRoomsGUI label = new HotelRoomsGUI();
  179. public RoomProfile() {
  180. setLayout(null);
  181. setModal(true);
  182. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  183. setBackground(new Color(80,80,80));
  184. setContentPane(setSubFrame());
  185. setSize(600,500);
  186. setLocationRelativeTo(null);
  187. }
  188. public JPanel setSubFrame()
  189. {
  190. createLabels();
  191. buttons();
  192. designLabel();
  193. subFrame = new JPanel();
  194. subFrame.setBounds(0,0,600,500);
  195. subFrame.setBackground(new Color(90,90,90));
  196. subFrame.setLayout(null);
  197. subFrame.add(setTopPanel());
  198. subFrame.add(setBottomRightPanel());
  199. subFrame.add(setBottomLeftPanel());
  200. subFrame.add(okB);
  201. subFrame.add(designLabel);
  202. return subFrame;
  203. }
  204. public void designLabel()
  205. {
  206. ImageIcon img = new ImageIcon("C:\Documents and Settings\Janpol\workspace\HotelGUI\src\Images\Account.JPG");
  207. designLabel = new JLabel(img);
  208. designLabel.setBounds(380,27,190,190);
  209. designLabel.setBorder(BorderFactory.createEtchedBorder(Color.GRAY,Color.BLACK));
  210.  
  211. }
  212. // this is where i added some of the labels
  213. public JPanel setTopPanel()
  214. {
  215. topPanel = new JPanel();
  216. topPanel.setLayout(new GridLayout(5,1));
  217. topPanel.setBounds(20,20,350,200);
  218. topPanel.setBackground(new Color(90,90,90));
  219. topPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GRAY,Color.BLACK)
  220. ,"Personal Information",1,2,new Font("Arial",Font.BOLD,13),Color.ORANGE));
  221. JLabel[] top = label.inputLabels();
  222. for(int x = 0; x<5; x++)
  223. {
  224. topPanel.add(labels[x]);
  225. topPanel.add(top[x]);
  226. }
  227.  
  228. return topPanel;
  229. }
  230. // also here
  231. public JPanel setBottomLeftPanel()
  232. {
  233. bottomLeftPanel = new JPanel();
  234. bottomLeftPanel.setLayout(new GridLayout(3,2));
  235. bottomLeftPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GRAY,Color.BLACK)
  236. ,"Stay Information",1,2,new Font("Arial",Font.BOLD,13),Color.ORANGE));
  237. bottomLeftPanel.setBackground(new Color(90,90,90));
  238. bottomLeftPanel.setBounds(20,250,300,140);
  239. JLabel[] bot = label.inputLabels();
  240. for(int x = 5; x<8; x++)
  241. {
  242. bottomLeftPanel.add(labels[x]);
  243. bottomLeftPanel.add(bot[x]);
  244. }
  245. return bottomLeftPanel;
  246. }
  247. // lastly here
  248. public JPanel setBottomRightPanel()
  249. {
  250. bottomRightPanel = new JPanel();
  251. bottomRightPanel.setLayout(new GridLayout(2,2));
  252. bottomRightPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GRAY,Color.BLACK)
  253. ,"Personal Information",1,2,new Font("Arial",Font.BOLD,13),Color.ORANGE));
  254. bottomRightPanel.setBackground(new Color(90,90,90));
  255. bottomRightPanel.setBounds(350,250,200,140);
  256. JLabel[] right = label.inputLabels();
  257. for(int x = 8; x<10; x++)
  258. {
  259. bottomRightPanel.add(labels[x]);
  260. bottomRightPanel.add(right[x]);
  261. }
  262. return bottomRightPanel;
  263. }
  264.  
  265. public JLabel[] createLabels()
  266. {
  267. labels = new JLabel[topTextLabels.length];
  268.  
  269. for(int x = 0; x<topTextLabels.length;x++)
  270. {
  271. labels[x] = new JLabel(topTextLabels[x]);
  272. labels[x].setForeground(Color.WHITE);
  273. }
  274. return labels;
  275. }
  276. public void buttons()
  277. {
  278. bh = new ButtonHandler();
  279. okB = new JButton("Ok");
  280. okB.setBounds(400,400,100,50);
  281. okB.addActionListener(bh);
  282. }
  283.  
  284. public class ButtonHandler implements ActionListener
  285. {
  286. public void actionPerformed(ActionEvent e)
  287. {
  288. dispose();
  289. }
  290. }
  291.  
  292. }
  293.  
  294. import java.sql.*;
  295.  
  296. public class Room
  297. {
  298.  
  299. private String roomType, availability, roomNo, Rate;
  300. private int guestID;
  301.  
  302. private Connection con;
  303. private PreparedStatement statement = null;
  304.  
  305. public Room(){
  306. try {
  307. Class.forName("com.mysql.jdbc.Driver");
  308. con = DriverManager.getConnection(
  309. "jdbc:mysql://localhost:3306/3moronsdb","root","");
  310.  
  311. }
  312. catch (Exception e) {
  313. e.printStackTrace();
  314. }
  315. }
  316. public Room(int id)
  317. {
  318. this();
  319. try{
  320. statement = con.prepareStatement("SELECT * FROM room WHERE guestID=?");
  321. statement.setInt(1, id);
  322. ResultSet rs = statement.executeQuery();
  323. while(rs.next()){
  324. this.guestID = rs.getInt(1);
  325. this.roomType = rs.getString(2);
  326. this.roomNo = rs.getString(3);
  327. this.Rate = rs.getString(4);
  328. this.availability= rs.getString(5);
  329. }
  330. }catch(Exception e){
  331. System.out.print(e);
  332. }
  333. }
  334. //Constructor for setting rate
  335. public Room(String roomTypeL, String roomNoL , String RateL, String availabilityL)
  336. {
  337. this();
  338. try
  339. {
  340. statement = con.prepareStatement("Insert into room(roomType, roomNo, rate,availability) values(?,?,?,?)");
  341. statement.setString(1, roomTypeL);
  342. statement.setString(2, roomNoL);
  343. statement.setString(3, RateL);
  344. statement.setString(4, availabilityL);
  345. statement.executeUpdate();
  346. }
  347. catch(Exception e)
  348. {
  349. e.printStackTrace();
  350. return;
  351. }
  352. }
  353. public Room(String roomNo){
  354. this();
  355. try{
  356. statement = con.prepareStatement("SELECT * FROM room WHERE roomNo=?");
  357. statement.setString(1, roomNo);
  358. ResultSet rs = statement.executeQuery();
  359. while(rs.next()){
  360. this.guestID = rs.getInt(1);
  361. this.roomType = rs.getString(2);
  362. this.roomNo = rs.getString(3);
  363. this.Rate = rs.getString(4);
  364. this.availability= rs.getString(5);
  365. }
  366. }
  367. catch(Exception e)
  368. {
  369. e.printStackTrace();
  370. return;
  371. }
  372. }
  373. //get guestID from the database
  374. public int getGuestID(){
  375. return this.guestID;
  376. }
  377. //getting roomType
  378. public String getRoomType(){
  379. return this.roomType;
  380. }
  381. //getting roomNo
  382. public String getRoomNo(){
  383. return this.roomNo;
  384. }
  385. //getting rate
  386. public String getRate(){
  387. return this.Rate;
  388. }
  389. //getting availability
  390. public String getAvailability(){
  391. return this.availability;
  392. }
  393. }
  394.  
  395. import java.sql.Connection;
  396. import java.sql.DriverManager;
  397. import java.sql.PreparedStatement;
  398. import java.sql.ResultSet;
  399.  
  400. public class GuestsInfo
  401. {
  402. private String firstName, lastName, gender, address, time, deposit, age, contactNo, stay;
  403. private int guestID;
  404. private Connection con;
  405. private PreparedStatement statement;
  406. //default constructor
  407. public GuestsInfo()
  408. {
  409. try {
  410. Class.forName("com.mysql.jdbc.Driver");
  411. con = DriverManager.getConnection(
  412. "jdbc:mysql://localhost:3306/3moronsdb","root","");
  413.  
  414. }
  415. catch (Exception e) {
  416. e.printStackTrace();
  417. }
  418. }
  419. public GuestsInfo(int guestID)
  420. {
  421. this();
  422. try{
  423. statement = con.prepareStatement("SELECT * FROM guest WHERE guestID=?");
  424. statement.setInt(1, guestID);
  425. ResultSet rs = statement.executeQuery();
  426. while(rs.next()){
  427. this.guestID = rs.getInt(1);
  428. this.firstName = rs.getString(2);
  429. this.lastName = rs.getString(3);
  430. this.age = rs.getString(4);
  431. this.gender= rs.getString(5);
  432. this.address= rs.getString(6);
  433. this.contactNo = rs.getString(7);
  434. this.time= rs.getString(8);
  435. this.stay = rs.getString(9);
  436. this.deposit = rs.getString(10);
  437. System.out.print(firstName +""+ lastName +""+ age +""+ gender +""+ address +""+ contactNo +""+
  438. time +""+ stay +""+ deposit);
  439. }
  440. }catch(Exception e){}
  441. }
  442. public GuestsInfo(String firstName, String lastName, String age, String gender,
  443. String address, String contactNo, String time, String stay, String deposit)
  444. {
  445. this();
  446. try
  447. {
  448. statement = con.prepareStatement("Insert into guest(firstName,lastName,age,gender,address,contactNo,time,stay,deposit)values(?,?,?,?,?,?,?,?,?)");
  449.  
  450. statement.setString(1, firstName);
  451. statement.setString(2, lastName);
  452. statement.setString(3, age);
  453. statement.setString(4, gender);
  454. statement.setString(5, address);
  455. statement.setString(6, contactNo);
  456. statement.setString(7, time);
  457. statement.setString(8, stay);
  458. statement.setString(9, deposit);
  459. statement.executeUpdate();
  460. }
  461. catch(Exception e)
  462. {
  463. e.printStackTrace();
  464. return;
  465. }
  466. }
  467. //get guestID from the database
  468. public int getGuestID(){
  469. return guestID;
  470. }
  471. //get firstName from the database
  472. public String getFirstName(){
  473. return this.firstName;
  474. }
  475.  
  476. //get lastName from the database
  477. public String getLastName(){
  478. return lastName;
  479. }
  480. //get age from the database
  481. public String getAge(){
  482.  
  483. return this.age;
  484. }
  485.  
  486. //get gender from the database
  487. public String getGender(){
  488. return this.gender;
  489. }
  490. //get address from database
  491. public String getAddress(){
  492. return this.address;
  493. }
  494. //get contactNo from the database
  495. public String getContactNo(){
  496. return this.contactNo;
  497. }
  498. //get time from database
  499. public String getTime()
  500. {
  501. return this.time;
  502. }
  503. //get stay from database
  504. public String getStay()
  505. {
  506. return this.stay;
  507. }
  508. //get roomType from database
  509. public String getDeposit()
  510. {
  511. return this.deposit;
  512. }
  513. }
  514.  
  515. ...
  516. JLabel[] bottomLeftFields = prof.getBottomLeftPanelFields();
  517. for(int i = 0; i<data.length; i++){
  518. bottomLeftFields[i].setText(" "+data[i]);
  519. }
  520.  
  521. //setting text on label
  522. prof.setVisible(true);
  523. ...
  524.  
  525. JLabel[] bot = label.inputLabels();
  526.  
  527. JLabel[] bot = inputLabels;
Add Comment
Please, Sign In to add comment