| | |
GUI problem
![]() |
•
•
Join Date: Apr 2008
Posts: 23
Reputation:
Solved Threads: 0
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
Java Syntax (Toggle Plain Text)
package music; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class Music extends JFrame implements ActionListener { //construct components JTextPane textPane = new JTextPane(); //initialize data in arrays String artistName[] = {"Metallica", "Guns N Roses", "Dr Dre", "Soul Asylum"}; String genre[] = {"Heavy Metal", "Heavy Metal", "Rap", "Rock"}; String greatestHit[] = {"One", "Don't Cry", "Chronic", "Run Away Train"}; String recordLabel[] = {"Def Jams", "Sony", "DeathRow","Artista"}; //construct instance of FavMusic public Music() { super("Music information"); } //create the menu system public JMenuBar createMenuBar() { //create an instance of the the menu JMenuBar mnuBar = new JMenuBar(); setJMenuBar(mnuBar); //construct and populate the file menu JMenu mnuFile = new JMenu("File", true); mnuFile.setMnemonic(KeyEvent.VK_F); mnuFile.setDisplayedMnemonicIndex(0); mnuBar.add(mnuFile); JMenuItem mnuFileExit = new JMenuItem("Exit"); mnuFileExit.setMnemonic(KeyEvent.VK_X); mnuFileExit.setDisplayedMnemonicIndex(1); mnuFile.add(mnuFileExit); mnuFileExit.setActionCommand("Exit"); mnuFileExit.addActionListener(this); //construct and populate the Edit menu JMenu mnuEdit = new JMenu("Edit", true); mnuEdit.setMnemonic(KeyEvent.VK_E); mnuEdit.setDisplayedMnemonicIndex(0); mnuBar.add(mnuEdit); JMenuItem mnuEditInsert = new JMenuItem("Insert New Music"); mnuEditInsert.setMnemonic(KeyEvent.VK_I); mnuEditInsert.setDisplayedMnemonicIndex(0); mnuEdit.add(mnuEditInsert); mnuEditInsert.setActionCommand("Insert"); mnuEditInsert.addActionListener(this); JMenu mnuEditSort = new JMenu("Sort"); mnuEditSort.setMnemonic(KeyEvent.VK_R); mnuEditSort.setDisplayedMnemonicIndex(3); mnuEdit.add(mnuEditSort); JMenuItem mnuEditSortByartistName = new JMenuItem("by Artist"); mnuEditSortByartistName.setMnemonic(KeyEvent.VK_T); mnuEditSortByartistName.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortByartistName); mnuEditSortByartistName.setActionCommand("artistName"); mnuEditSortByartistName.addActionListener(this); JMenuItem mnuEditSortByGenre = new JMenuItem("by Genre"); mnuEditSortByGenre.setMnemonic(KeyEvent.VK_S); mnuEditSortByGenre.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortByGenre); mnuEditSortByGenre.setActionCommand("genre"); mnuEditSortByGenre.addActionListener(this); JMenuItem mnuEditSortBygreatestHit = new JMenuItem("by Greatest Hit"); mnuEditSortBygreatestHit.setMnemonic(KeyEvent.VK_Y); mnuEditSortBygreatestHit.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortBygreatestHit); mnuEditSortBygreatestHit.setActionCommand("greatestHit"); mnuEditSortBygreatestHit.addActionListener(this); JMenuItem mnuEditSortByrecordLabel = new JMenuItem("by Record Label"); mnuEditSortByrecordLabel.setMnemonic(KeyEvent.VK_T); mnuEditSortByrecordLabel.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortByrecordLabel); mnuEditSortByrecordLabel.setActionCommand("recordLabel"); mnuEditSortByrecordLabel.addActionListener(this); return mnuBar; } //create the content pane public Container createContentPane() { //create the JTextPane and center panel JPanel centerPanel = new JPanel(); setTabsAndStyles(textPane); textPane = addTextToTextPane(); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); centerPanel.add(scrollPane); //create container and set attributes Container c = getContentPane(); c.setLayout(new BorderLayout(1,5)); //c.add(northPanel, BorderLayout.NORTH); c.add(centerPanel, BorderLayout.CENTER); return c; } //method to create tabe stops and set font style protected void setTabsAndStyles(JTextPane textPane) { //create Tab Stops TabStop[] tabs = new TabStop[2]; tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); TabSet tabset = new TabSet(tabs); //set Tab Style StyleContext tabStyle = StyleContext.getDefaultStyleContext(); AttributeSet aset = tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset); textPane.setParagraphAttributes(aset, false); //set font style Style fontStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style regular = textPane.addStyle("regular", fontStyle); StyleConstants.setFontFamily(fontStyle, "SansSerif"); Style s = textPane.addStyle("italic", regular); StyleConstants.setItalic(s, true); s = textPane.addStyle("bold", regular); StyleConstants.setBold(s, true); s = textPane.addStyle("large", regular); StyleConstants.setFontSize(s, 16); } //method to add new text to the JTextpane public JTextPane addTextToTextPane() { Document doc = textPane.getDocument(); try { //clear previous text doc.remove(0, doc.getLength()); //insert artistName doc.insertString(0, "Artist\tGenre\tGreatest Hit\tRecord Label\n", textPane.getStyle("large")); //insert detail for (int j = 0; j < artistName.length; j++) { doc.insertString(doc.getLength(), artistName[j] + "\t", textPane.getStyle("bold")); doc.insertString(doc.getLength(), genre[j] + "\t", textPane.getStyle("italic")); doc.insertString(doc.getLength(), greatestHit[j] + "\t", textPane.getStyle("regular")); doc.insertString(doc.getLength(), recordLabel[j] + "\t", textPane.getStyle("regular")); } } catch (BadLocationException ble) { System.err.println("Couldn't insert text."); } return textPane; } //event to process user clicks public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); //user clicks exit on the file menu if (arg == "Exit") System.exit(0); //user clicks Insert New FavMusic on the edit menu if (arg == "Insert") { //accept new data String newartistName = JOptionPane.showInputDialog(null, "Please enter the new Artist"); String newGenre = JOptionPane.showInputDialog(null, "Please enter the genre for " + newartistName); String newgreatestHit = JOptionPane.showInputDialog(null, "Please enter the Greatest Hit for for " + newartistName); String newrecordLabel = JOptionPane.showInputDialog(null, "Please enter the Record Label for for " + newartistName); //enlarge array artistName = enlargeArray(artistName); genre = enlargeArray(genre); greatestHit = enlargeArray(greatestHit); recordLabel = enlargeArray(recordLabel); //add new data to arrays artistName[artistName.length-1] = newartistName; genre[genre.length-1] = newGenre; greatestHit[greatestHit.length-1] = newgreatestHit; recordLabel[recordLabel.length-1] = newrecordLabel; //call sort method // COMMENTED OUT s AND CHANGED SORT CALL TO NOW TAKE TWO PARAMETERS //String[] s = new String[]{arg, artistName}; sort(arg, artistName); } //user clicks artistName on the search submenu if (arg == "artistName") sort(arg, artistName); //user clicks genre on the search submenu if (arg == "genre") sort(arg, genre); //user clicks greatestHit on the search submenu if (arg == "greatestHit") sort(arg, greatestHit); //user clicks recordLabel on the search submenu if (arg == "recordLabel") sort(arg, recordLabel); } //method to enlarge an array by 1 public String[] enlargeArray(String[] currentArray) { String[] newArray = new String[currentArray.length + 1]; for (int i = 0; i < currentArray.length; i++) newArray[i] = currentArray[i]; return newArray; } //method to sort arrays public void sort(String arg, String tempArray[]) { //loop to control number of passes for (int pass = 1; pass <tempArray.length; pass++) { for (int element = 0; element < tempArray.length - 1; element ++) if (tempArray[element].compareTo(tempArray[element + 1]) > 0) { swap (artistName, element, element + 1); swap (genre, element, element + 1); swap (greatestHit, element, element + 1); swap (recordLabel, element, element + 1); } } addTextToTextPane(); } //method to swap two elements of an array public void swap(String swapArray[], int first, int second) { String hold; //temporary area for swap hold = swapArray[first]; swapArray[first] = swapArray[second]; swapArray[second] = hold; } //main method executes at run time public static void main(String arg[]) { JFrame.setDefaultLookAndFeelDecorated(true); Music f = new Music(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setJMenuBar(f.createMenuBar()); f.setContentPane(f.createContentPane()); f.setSize(600, 275); f.setVisible(true); } }
Last edited by Taker; Oct 29th, 2008 at 2:41 pm.
•
•
Join Date: Apr 2008
Posts: 23
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
package musicgui; import java.awt.event.ActionEvent; import javax.swing.*; public class MG extends JFrame { String Titles[] = {"Artist Name", "Title of Song", "Genre"}; //initialize data in arrays String r1[] = {"Metallica", "Memory Remains", "Rock"}; JPanel Panel = new JPanel(); JButton jbtNext = new JButton("Next"); JButton jbtPrev = new JButton("Previous"); jbtNext.setToolTipText("Takes you to next record"); jbtPrev.setToolTipText("Takes you to previous record"); Panel.add(jbtNext); Panel.add(jbtPrev); } public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); //user clicks exit on the file menu if (arg == "Next") { String r2[] = {"The Hives", "Walk Idiot Walk", "Rock"}; } if (arg == "Previous") { String r1[] = {"Metallica", "Memory Remains", "Rock"}; } } JFrame frame = new JFrame(); frame.add(Panel); frame.setTitle("Music Records"); frame.setSize(200,150); frame.setLocation(200,100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
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.
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.
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.
![]() |
Similar Threads
- Windows GUI - problem with dialog box (C++)
- Python GUI Problem (Python)
- passing values and displaying images in a GUI (Java)
- JFrame GUI Problem (Java)
- Java GUI problem... contents of JFrame is invisible... (Java)
- basic java GUI problem (Java)
Other Threads in the Java Forum
- Previous Thread: help agaaaaain ???
- Next Thread: AWT or Swing?
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






