Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reverse

Posted on 2007-10-07
5
Medium Priority
?
181 Views
Last Modified: 2010-03-30
At the end of my program, where I have reverse the button does not reverse looking for some help on how to make it reverse. you need both of these programs for it to work.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class demo extends JFrame implements ActionListener
{

   public static final int WIDTH = 600;
   public static final int HEIGHT = 300;
   public static final int LINES = 10;
   public static final int CHAR_PER_LINE = 40;

   private JTextArea theText;
   private String memo1 = "No Memo 1.";
   private String memo2 = "No Memo 2.";
   private String myName = "My name is charlie";
   private String myHometown = " diego garcia.";

     public static void main(String[] args)
   {
       demo guiMemo = new demo( );
       guiMemo.setVisible(true);
   }

   public demo( )

   {

     setSize (WIDTH, HEIGHT);
       addWindowListener(new WindowDestroyer( ));

       setTitle("Memo Saver");

       Container contentPane = getContentPane( );//text panel
       contentPane.setLayout (new BorderLayout ( ));
       JPanel textPanel = new JPanel ( );
       textPanel.setBackground(Color.BLUE);
       theText = new JTextArea (LINES, CHAR_PER_LINE);
       theText.setBackground(Color.WHITE);
       textPanel.add(theText);
       theText.setLineWrap(true);
contentPane.add(textPanel, BorderLayout.NORTH);//text panel north

        JPanel contentPanel = new JPanel ();//new panel with grids
            contentPanel.setLayout(new GridLayout(3,9));
        contentPane.add(contentPanel);//display grid 9 placed in grid





        JButton memo1Button = new JButton("Save Memo 1");
        memo1Button.addActionListener(this);
        contentPanel.add(memo1Button);

        JButton memo2Button = new JButton("Save Memo 2");
        memo2Button.addActionListener(this);
        contentPanel.add(memo2Button);

        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(this);
        contentPanel.add(clearButton);

        JButton get1Button = new JButton("Get Memo 1");
        get1Button.addActionListener(this);
        contentPanel.add(get1Button);

          JButton get2Button = new JButton("Get Memo 2");
        get2Button.addActionListener(this);
        contentPanel.add(get2Button);

        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        contentPanel.add(exitButton);

          JButton reverseButton = new JButton("Reverse");//
        reverseButton.addActionListener(this);
        contentPanel.add(reverseButton);

        JButton myName = new JButton("My Name");
        myName.addActionListener(this);
        contentPanel.add(myName);

        JButton myInfo = new JButton("My Info");
        myInfo.addActionListener(this);
        contentPanel.add(myInfo);

}

public void actionPerformed(ActionEvent e)//buttons pop-up
{
    String actionCommand = e.getActionCommand( );
    if (actionCommand.equals("Save Memo 1"))
    {
        memo1 = theText.getText( );
        theText.setText("Memo 1 Saved");
    }
    else if (actionCommand.equals("Save Memo 2"))
    {
              memo2 = theText.getText( );
              theText.setText("Memo 2 Saved");
    }

    else if (actionCommand.equals("My Info"))
          theText.setText(myName + myHometown);

    else if (actionCommand.equals("My Name"))//pop up window
    JOptionPane.showMessageDialog(
    null, "My name is ");

    else if (actionCommand.equals("Reverse"))
         theText.setText("dont work");
    else if (actionCommand.equals("Exit"))
          System.exit(0);
    else if (actionCommand.equals("Clear"))
         theText.setText("");
    else if (actionCommand.equals("Get Memo 1"))//retrieve memo 1
        theText.setText(memo1);
    else if (actionCommand.equals("Get Memo2"))//retrieve memo 2
        theText.setText(memo2);

    else
        theText.setText("Error in memo interface");
    }

}



import java.awt.*;
import java.awt.event.*;

/**
 If you register an object of this class as a listener to any
 object of the class JFrame, then if the user clicks the
 close-window button in the JFrame, the object of this class
 will end the program  and close the JFrame.
*/
public class WindowDestroyer extends WindowAdapter
{
    public void windowClosing(WindowEvent e)
    {
         System.exit(0);
    }
}
0
Comment
Question by:dioniciochingon
  • 3
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20030007
whats it meant to do?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20030124
You can do

      else if      (actionCommand.equals("Reverse")) {
            StringBuilder sb = new StringBuilder(theText.getText()).reverse();
            theText.setText(sb.toString());
      }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030160
:-)
0
 

Author Comment

by:dioniciochingon
ID: 20030364
CEHJ thanks for the solution. Its a beautiful thing.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030372
No problem
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question