0

I have added copy and clear to my codes and they work fine, but i still have problem with the cut and the find command that allow user to search the output area for specifed test

public class rr extends JFrame implements ActionListener {

private JTextArea outputArea;



public rr()
{
    associatedTextSet = new HashSet<String>();

    initialiseWindow();
}

private void initialiseWindow()

{
    setTitle("rr");

    setLocation(50, 50);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    contentPane = getContentPane();

    contentPane.setBackground(Color.magenta);  

    contentPane.setLayout( new BoxLayout(contentPane, BoxLayout.Y_AXIS) );

    WindowAdapter windowListener = new WindowAdapter(){

            public void windowClosing(WindowEvent e)
            {
                ActionEvent action = new ActionEvent(this, 0, "Exit");

                actionPerformed(action);
            } };
    addWindowListener(windowListener);

    setupMenusAndActions();

    setUpOutputArea();

    setVisible(true);
}

private void setUpOutputArea()
{
    outputArea = new JTextArea();
    outputArea.setFont(new Font("Courier", Font.PLAIN, 12));
    outputArea.setEditable(false);
    outputArea.setBackground(Color.white);
    outputArea.setLineWrap(true);
    outputArea.setWrapStyleWord(true);
    outputArea.setMargin(new Insets( 5, 10, 5, 10));
    JScrollPane areaScrollPane = new JScrollPane(outputArea);
    areaScrollPane.setPreferredSize( new Dimension(WIDTH, HEIGHT) );
    Border b = BorderFactory.createLoweredBevelBorder();
    areaScrollPane.setViewportBorder(BorderFactory.createTitledBorder(b, "Output View"));
    contentPane.add(areaScrollPane);
    pack();
    TextAreaOutputStream textOutput = new TextAreaOutputStream(outputArea);
    PrintStream capturedOutput = new PrintStream(textOutput);
    System.setOut(new PrintStream(capturedOutput));  // divert StandardOutput to capturedOutput
}
private void setupMenusAndActions()
{
    JMenuBar menuBar = new JMenuBar();

    // Edit menu
    editMenu = setupMenu(menuBar, "Edit", 'E');
    setupMenuItem(editMenu, "Copy", "Copy selected text from Output area to clipboard", 'C', true, KeyStroke.getKeyStroke("ctrl C"));
    setupMenuItem(editMenu, "Cut", "Cut selected text from Output area to clipboard", 'T', true );
    setupMenuItem(editMenu, "Find", "  Find selected text from Output area to clipboard", 'F', true );
    setupMenuItem(editMenu, "Clear", "Clear Output area", 'L', true, KeyStroke.getKeyStroke("ctrl F3"));

    setJMenuBar(menuBar);
}
 public void actionPerformed(ActionEvent e)
{
     String action = e.getActionCommand();
    //
    // Edit menu
    //
    else if( action.equals("Copy") )
    {
        outputArea.copy();
    }
    else if(action.equals("Cut"))
    {
        outputArea.cut();
    }

    else if( action.equals("Clear") )
    {
        outputArea.selectAll();
        outputArea.setText("");
    }
    else if(action.equals("Find"))
    {

    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I'm not sure what you're issue with `Cut` might be, but then I can't see how your menus are setup up. But you could take a look [this](http://stackoverflow.com/questions/13437865/java-scroll-to-specific-text-inside-jtextarea/13438455#13438455) as an example for highlighting words in a `JTextArea` which should suit for a bases for your find functionality... – MadProgrammer May 03 '13 at 23:56
  • Do you check [**THIS**](http://stackoverflow.com/questions/13437865/java-scroll-to-specific-text-inside-jtextarea/13438455#13438455) link? – MadProgrammer May 04 '13 at 00:11

2 Answers2

2

, but i still have problem with the cut

Use the default Action provided by the EditorKit:

JMenuItem cut =  new DefaultEditorKit.CutAction();

You can also do this for copy.

In general you should be extending TextAction and create a custom Action for each function instead of sharing a common listener. Something like:

class ClearAction extends TextAction
{
    public ClearAction()
    {
        super("Clear");
    }

    public void actionPerformed(ActionEvent e)
    {
        getFocusedComponent().setText("");
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288