working with Java GUI

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 47
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

working with Java GUI

 
0
  #1
Nov 5th, 2009
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.
  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. }
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #2
Nov 5th, 2009
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:

  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.

  1. String result="";
  2. ...
  3. result += map.get(stringArray[i]);
  4. ...
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz
 
0
  #3
Nov 5th, 2009
Write new class
MorseEncoder
Place in it your array
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 47
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster
 
0
  #4
Nov 5th, 2009
I'm using NetBeans IDE 6.7.1
Not sure if that means much in the way of what version I'm using.
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 261 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC