944,099 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 658
  • Java RSS
Nov 5th, 2009
0

working with Java GUI

Expand Post »
This project is similar to, but not exactly like, my phonetic project. I am having to construct a GUI and have done so. I have been able to get everything except one part.

What I need it to do is:
1) User inputs a string (this part works)
2) When user clicks button "Code It" it needs to convert the string into Morse code. (this part is what isn't working)

I don't know how to set it up to do it. I'm trying to use an array to do it.

What it does:
1) Takes user's input
2) When user clicks button "Code It" it just displays the string they entered.

Any ideas and help will be appreciated.
Java Syntax (Toggle Plain Text)
  1. package morsecode;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class MorseCode
  8. {
  9. public static class morseCode implements ActionListener
  10. {
  11. JFrame frame;
  12. JPanel contentPane;
  13. JTextField words;
  14. JButton CodeIt;
  15. JLabel enterString, coded;
  16.  
  17. public morseCode()
  18. {
  19. // Create and set up the frame
  20. frame = new JFrame("Morse Code");
  21. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.  
  23. // Create a content pane with a BoxLayout
  24. // and empty borders
  25. contentPane = new JPanel();
  26. contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 100));
  27. contentPane.setLayout(new GridLayout(4,2,5,10));
  28.  
  29. // Create a text field and a descriptive label
  30. enterString = new JLabel("Enter string: ");
  31. contentPane.add(enterString);
  32.  
  33. words = new JTextField(50);
  34. contentPane.add(words);
  35.  
  36. // Create a display morseCode button
  37. CodeIt = new JButton("Code It");
  38. CodeIt.addActionListener(this);
  39. contentPane.add(CodeIt);
  40.  
  41. // Create a label that will display text in morse code
  42. coded = new JLabel(" ");
  43. contentPane.add(coded);
  44.  
  45. // Add content pane to frame
  46. frame.setContentPane(contentPane);
  47.  
  48. // Size and then diplay the frame
  49. frame.pack();
  50. frame.setVisible(true);
  51. }
  52.  
  53.  
  54. //////////// I think this is the section where I need to put my array
  55. //////////// set up since it retrieves the user input and I believe
  56. //////////// displays it
  57. public void actionPerformed(ActionEvent event)
  58. {
  59. String text = words.getText();
  60. // char[] stringArray = text.toCharArray();
  61. // String[] morse = {".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- ","-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- ","..- ","...- ",".-- ","-..- ","-.-- ","--.. "};
  62. coded.setText(text);
  63. }
  64. }
  65.  
  66. private static void runGUI()
  67. {
  68. JFrame.setDefaultLookAndFeelDecorated(true);
  69.  
  70. morseCode greeting = new morseCode();
  71. }
  72.  
  73. public static void main(String[] args)
  74. {
  75. javax.swing.SwingUtilities.invokeLater(new Runnable()
  76. {
  77. public void run()
  78. {
  79. runGUI();
  80. }
  81. });
  82. }
  83. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
_dragonwolf_ is offline Offline
51 posts
since Jul 2009
Nov 5th, 2009
0
Re: working with Java GUI
What java version are you using?
If you are using 1.5 for example then you can map the letters with the morse code using a hashmap:

Java Syntax (Toggle Plain Text)
  1. HashMap<Character,String> map = HashMap<Character,String>(26);
  2.  
  3. map.put('a', ".-");
  4. map.put('b', "-...");
  5. ...
  6.  
  7.  
  8. String text = words.getText().toLowerCase();
  9. char[] stringArray = text.toCharArray();

Then you can loop through the array and use the char[i] as the key to get the String value from the map.

Java Syntax (Toggle Plain Text)
  1. String result="";
  2. ...
  3. result += map.get(stringArray[i]);
  4. ...
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Nov 5th, 2009
0
Re: working with Java GUI
Write new class
Quote ...
MorseEncoder
Place in it your array
Quote ...
String[] morse
Start from single char. Get result.
Later use array of char or String.
//
Alternatively you can do all work inside single static method.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 5th, 2009
0
Re: working with Java GUI
I'm using NetBeans IDE 6.7.1
Not sure if that means much in the way of what version I'm using.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
_dragonwolf_ is offline Offline
51 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: HELP!!!!!
Next Thread in Java Forum Timeline: Help with hw...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC