GUI problem

Reply

Join Date: Apr 2008
Posts: 23
Reputation: Taker has a little shameless behaviour in the past 
Solved Threads: 0
Taker Taker is offline Offline
Newbie Poster

GUI problem

 
0
  #1
Oct 29th, 2008
Hi i am tryin to write a recording class in java which i have done but i have to convert the recording class into a GUI i have done the coding for it but i want buttons that allows you to cyle to the nex and previous record detail if anyone can help me it would be fantastic

  1. package music;
  2.  
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. import javax.swing.text.*;
  8.  
  9. public class Music extends JFrame implements ActionListener
  10. {
  11. //construct components
  12. JTextPane textPane = new JTextPane();
  13.  
  14. //initialize data in arrays
  15. String artistName[] = {"Metallica", "Guns N Roses", "Dr Dre", "Soul Asylum"};
  16. String genre[] = {"Heavy Metal", "Heavy Metal", "Rap", "Rock"};
  17. String greatestHit[] = {"One", "Don't Cry", "Chronic", "Run Away Train"};
  18. String recordLabel[] = {"Def Jams", "Sony", "DeathRow","Artista"};
  19.  
  20. //construct instance of FavMusic
  21. public Music()
  22. {
  23. super("Music information");
  24. }
  25.  
  26. //create the menu system
  27. public JMenuBar createMenuBar()
  28. {
  29. //create an instance of the the menu
  30. JMenuBar mnuBar = new JMenuBar();
  31. setJMenuBar(mnuBar);
  32.  
  33. //construct and populate the file menu
  34. JMenu mnuFile = new JMenu("File", true);
  35. mnuFile.setMnemonic(KeyEvent.VK_F);
  36. mnuFile.setDisplayedMnemonicIndex(0);
  37. mnuBar.add(mnuFile);
  38.  
  39. JMenuItem mnuFileExit = new JMenuItem("Exit");
  40. mnuFileExit.setMnemonic(KeyEvent.VK_X);
  41. mnuFileExit.setDisplayedMnemonicIndex(1);
  42. mnuFile.add(mnuFileExit);
  43. mnuFileExit.setActionCommand("Exit");
  44. mnuFileExit.addActionListener(this);
  45.  
  46. //construct and populate the Edit menu
  47. JMenu mnuEdit = new JMenu("Edit", true);
  48. mnuEdit.setMnemonic(KeyEvent.VK_E);
  49. mnuEdit.setDisplayedMnemonicIndex(0);
  50. mnuBar.add(mnuEdit);
  51.  
  52. JMenuItem mnuEditInsert = new JMenuItem("Insert New Music");
  53. mnuEditInsert.setMnemonic(KeyEvent.VK_I);
  54. mnuEditInsert.setDisplayedMnemonicIndex(0);
  55. mnuEdit.add(mnuEditInsert);
  56. mnuEditInsert.setActionCommand("Insert");
  57. mnuEditInsert.addActionListener(this);
  58.  
  59. JMenu mnuEditSort = new JMenu("Sort");
  60. mnuEditSort.setMnemonic(KeyEvent.VK_R);
  61. mnuEditSort.setDisplayedMnemonicIndex(3);
  62. mnuEdit.add(mnuEditSort);
  63.  
  64. JMenuItem mnuEditSortByartistName = new JMenuItem("by Artist");
  65. mnuEditSortByartistName.setMnemonic(KeyEvent.VK_T);
  66. mnuEditSortByartistName.setDisplayedMnemonicIndex(3);
  67. mnuEditSort.add(mnuEditSortByartistName);
  68. mnuEditSortByartistName.setActionCommand("artistName");
  69. mnuEditSortByartistName.addActionListener(this);
  70.  
  71. JMenuItem mnuEditSortByGenre = new JMenuItem("by Genre");
  72. mnuEditSortByGenre.setMnemonic(KeyEvent.VK_S);
  73. mnuEditSortByGenre.setDisplayedMnemonicIndex(3);
  74. mnuEditSort.add(mnuEditSortByGenre);
  75. mnuEditSortByGenre.setActionCommand("genre");
  76. mnuEditSortByGenre.addActionListener(this);
  77.  
  78. JMenuItem mnuEditSortBygreatestHit = new JMenuItem("by Greatest Hit");
  79. mnuEditSortBygreatestHit.setMnemonic(KeyEvent.VK_Y);
  80. mnuEditSortBygreatestHit.setDisplayedMnemonicIndex(3);
  81. mnuEditSort.add(mnuEditSortBygreatestHit);
  82. mnuEditSortBygreatestHit.setActionCommand("greatestHit");
  83. mnuEditSortBygreatestHit.addActionListener(this);
  84.  
  85. JMenuItem mnuEditSortByrecordLabel = new JMenuItem("by Record Label");
  86. mnuEditSortByrecordLabel.setMnemonic(KeyEvent.VK_T);
  87. mnuEditSortByrecordLabel.setDisplayedMnemonicIndex(3);
  88. mnuEditSort.add(mnuEditSortByrecordLabel);
  89. mnuEditSortByrecordLabel.setActionCommand("recordLabel");
  90. mnuEditSortByrecordLabel.addActionListener(this);
  91.  
  92. return mnuBar;
  93. }
  94.  
  95. //create the content pane
  96. public Container createContentPane()
  97. {
  98. //create the JTextPane and center panel
  99. JPanel centerPanel = new JPanel();
  100. setTabsAndStyles(textPane);
  101. textPane = addTextToTextPane();
  102. JScrollPane scrollPane = new JScrollPane(textPane);
  103. scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  104. scrollPane.setPreferredSize(new Dimension(500, 200));
  105. scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  106. scrollPane.setPreferredSize(new Dimension(500, 200));
  107. centerPanel.add(scrollPane);
  108.  
  109. //create container and set attributes
  110. Container c = getContentPane();
  111. c.setLayout(new BorderLayout(1,5));
  112. //c.add(northPanel, BorderLayout.NORTH);
  113. c.add(centerPanel, BorderLayout.CENTER);
  114.  
  115. return c;
  116. }
  117.  
  118. //method to create tabe stops and set font style
  119. protected void setTabsAndStyles(JTextPane textPane)
  120. {
  121. //create Tab Stops
  122. TabStop[] tabs = new TabStop[2];
  123. tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
  124. tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
  125. TabSet tabset = new TabSet(tabs);
  126.  
  127. //set Tab Style
  128. StyleContext tabStyle = StyleContext.getDefaultStyleContext();
  129. AttributeSet aset =
  130. tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
  131. textPane.setParagraphAttributes(aset, false);
  132.  
  133. //set font style
  134. Style fontStyle =
  135. StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  136.  
  137. Style regular = textPane.addStyle("regular", fontStyle);
  138. StyleConstants.setFontFamily(fontStyle, "SansSerif");
  139.  
  140. Style s = textPane.addStyle("italic", regular);
  141. StyleConstants.setItalic(s, true);
  142.  
  143. s = textPane.addStyle("bold", regular);
  144. StyleConstants.setBold(s, true);
  145.  
  146. s = textPane.addStyle("large", regular);
  147. StyleConstants.setFontSize(s, 16);
  148. }
  149.  
  150. //method to add new text to the JTextpane
  151. public JTextPane addTextToTextPane()
  152. {
  153. Document doc = textPane.getDocument();
  154. try
  155. {
  156. //clear previous text
  157. doc.remove(0, doc.getLength());
  158.  
  159. //insert artistName
  160. doc.insertString(0, "Artist\tGenre\tGreatest Hit\tRecord Label\n", textPane.getStyle("large"));
  161.  
  162. //insert detail
  163. for (int j = 0; j < artistName.length; j++)
  164. {
  165. doc.insertString(doc.getLength(), artistName[j] + "\t",
  166. textPane.getStyle("bold"));
  167. doc.insertString(doc.getLength(), genre[j] + "\t", textPane.getStyle("italic"));
  168. doc.insertString(doc.getLength(), greatestHit[j] + "\t", textPane.getStyle("regular"));
  169. doc.insertString(doc.getLength(), recordLabel[j] + "\t", textPane.getStyle("regular"));
  170. }
  171. }
  172. catch (BadLocationException ble)
  173. {
  174. System.err.println("Couldn't insert text.");
  175. }
  176. return textPane;
  177. }
  178.  
  179. //event to process user clicks
  180. public void actionPerformed(ActionEvent e)
  181. {
  182. String arg = e.getActionCommand();
  183.  
  184. //user clicks exit on the file menu
  185. if (arg == "Exit")
  186. System.exit(0);
  187.  
  188. //user clicks Insert New FavMusic on the edit menu
  189. if (arg == "Insert")
  190. {
  191. //accept new data
  192. String newartistName = JOptionPane.showInputDialog(null, "Please enter the new Artist");
  193. String newGenre = JOptionPane.showInputDialog(null, "Please enter the genre for " + newartistName);
  194. String newgreatestHit = JOptionPane.showInputDialog(null, "Please enter the Greatest Hit for for " + newartistName);
  195. String newrecordLabel = JOptionPane.showInputDialog(null, "Please enter the Record Label for for " + newartistName);
  196.  
  197. //enlarge array
  198. artistName = enlargeArray(artistName);
  199. genre = enlargeArray(genre);
  200. greatestHit = enlargeArray(greatestHit);
  201. recordLabel = enlargeArray(recordLabel);
  202.  
  203. //add new data to arrays
  204. artistName[artistName.length-1] = newartistName;
  205. genre[genre.length-1] = newGenre;
  206. greatestHit[greatestHit.length-1] = newgreatestHit;
  207. recordLabel[recordLabel.length-1] = newrecordLabel;
  208.  
  209. //call sort method
  210.  
  211. // COMMENTED OUT s AND CHANGED SORT CALL TO NOW TAKE TWO PARAMETERS
  212.  
  213. //String[] s = new String[]{arg, artistName};
  214. sort(arg, artistName);
  215. }
  216.  
  217. //user clicks artistName on the search submenu
  218. if (arg == "artistName")
  219. sort(arg, artistName);
  220.  
  221. //user clicks genre on the search submenu
  222. if (arg == "genre")
  223. sort(arg, genre);
  224.  
  225. //user clicks greatestHit on the search submenu
  226. if (arg == "greatestHit")
  227. sort(arg, greatestHit);
  228.  
  229. //user clicks recordLabel on the search submenu
  230. if (arg == "recordLabel")
  231. sort(arg, recordLabel);
  232. }
  233.  
  234. //method to enlarge an array by 1
  235. public String[] enlargeArray(String[] currentArray)
  236. {
  237. String[] newArray = new String[currentArray.length + 1];
  238. for (int i = 0; i < currentArray.length; i++)
  239. newArray[i] = currentArray[i];
  240. return newArray;
  241. }
  242.  
  243. //method to sort arrays
  244. public void sort(String arg, String tempArray[])
  245. {
  246. //loop to control number of passes
  247. for (int pass = 1; pass <tempArray.length; pass++)
  248. {
  249. for (int element = 0; element < tempArray.length - 1; element ++)
  250. if (tempArray[element].compareTo(tempArray[element + 1]) > 0)
  251. {
  252. swap (artistName, element, element + 1);
  253. swap (genre, element, element + 1);
  254. swap (greatestHit, element, element + 1);
  255. swap (recordLabel, element, element + 1);
  256. }
  257. }
  258. addTextToTextPane();
  259. }
  260.  
  261. //method to swap two elements of an array
  262. public void swap(String swapArray[], int first, int second)
  263. {
  264. String hold; //temporary area for swap
  265. hold = swapArray[first];
  266. swapArray[first] = swapArray[second];
  267. swapArray[second] = hold;
  268. }
  269.  
  270.  
  271. //main method executes at run time
  272. public static void main(String arg[])
  273. {
  274. JFrame.setDefaultLookAndFeelDecorated(true);
  275. Music f = new Music();
  276. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  277. f.setJMenuBar(f.createMenuBar());
  278. f.setContentPane(f.createContentPane());
  279. f.setSize(600, 275);
  280. f.setVisible(true);
  281. }
  282. }
Last edited by Taker; Oct 29th, 2008 at 2:41 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: GUI problem

 
0
  #2
Oct 29th, 2008
Perhaps you would like to explain which methods you are having trouble with in that wall of code? That's a lot of extraneous UI code to wade through to distill the relevant pieces you want to work with.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 23
Reputation: Taker has a little shameless behaviour in the past 
Solved Threads: 0
Taker Taker is offline Offline
Newbie Poster

Re: GUI problem

 
0
  #3
Oct 29th, 2008
  1. package musicgui;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class MG extends JFrame
  8. {
  9.  
  10.  
  11. String Titles[] = {"Artist Name", "Title of Song", "Genre"};
  12.  
  13. //initialize data in arrays
  14. String r1[] = {"Metallica", "Memory Remains", "Rock"};
  15.  
  16. JPanel Panel = new JPanel();
  17. JButton jbtNext = new JButton("Next");
  18. JButton jbtPrev = new JButton("Previous");
  19. jbtNext.setToolTipText("Takes you to next record");
  20. jbtPrev.setToolTipText("Takes you to previous record");
  21. Panel.add(jbtNext);
  22. Panel.add(jbtPrev);
  23. }
  24.  
  25. public void actionPerformed(ActionEvent e)
  26. {
  27. String arg = e.getActionCommand();
  28.  
  29. //user clicks exit on the file menu
  30. if (arg == "Next")
  31. {
  32. String r2[] = {"The Hives", "Walk Idiot Walk", "Rock"};
  33. }
  34. if (arg == "Previous")
  35. {
  36. String r1[] = {"Metallica", "Memory Remains", "Rock"};
  37. }
  38. }
  39.  
  40. JFrame frame = new JFrame();
  41. frame.add(Panel);
  42. frame.setTitle("Music Records");
  43. frame.setSize(200,150);
  44. frame.setLocation(200,100);
  45. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. frame.setVisible(true);
  47.  
  48. }
  49. }

Re-wrote code instead of a JMenu it know takes Jbuttons the problem i am having know is getting the buttons to go backwards and forwards through the set of recordings i have named above and also getting the record to display in the GUI it just show me a blnk GUI. I am trying to get the details of the current recording should be displayed at any point in time.
Last edited by Taker; Oct 29th, 2008 at 5:16 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: GUI problem

 
0
  #4
Oct 29th, 2008
Consider that it would be much more effective to work with objects that contain the data for each entry rather than arrays of individual Strings that are arbitrarily related by their positions.

A single list of those objects can easily be traversed by keeping a current index pointer.

To change the GUI fields, you have to set the new values to be shown from the object at the current index.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC