String Search within a JTextArea Component?

2001-06-28 Thread D. Michael Nelson

Hello,

I am writing a small text editor using the JTextArea component.  Does any
know how to facilitate a search for a string within the contents of a
JTextArea component?  Thanks in advance!

Take It Easy,
D. Michael Nelson




___
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing



Re: Escape key not dismising dialog that contains JTable (TESTCASE included)

2001-06-28 Thread Kleopatra

John, 

It's not a bug: JTable registers the escape-keypressed for canceling an
edit. 

If you don't allow edit's in the table then you can simply
remove the registration from the
inputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); note that you actually
have to remove it from the parent map, that is
table.getInputMap(ancesterCondition).getParent().remove(escKeyStroke).

Things get a bit more complicated if you need a two stage "escape" (one
for cancelling the edit without cancelling the dialog, the other for
cancelling the dialog). A keyEvent is consumed if an inputMap is found
that has interest in it. So you need two different keyStrokes (pressed
vs. released) plus dynamic updates of the table's inputMap to handle it.

Attached is a modified version of your sample. It's a quick test to show
the principle, needs some polish for production of course.

Greetings
Jeanette

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

public class JTableEscapeKeyBug extends JDialog {

   public static void main(String[] args) {
  new JTableEscapeKeyBug();
   }
   public JTableEscapeKeyBug() {
  addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent e) {
System.exit(0);
 }}
  );

  JButton b = new JButton("Hello");
  Object[][] data = {
 {"one"},
 {"two"},
 {"three"}
  };
  String[] hdr = {"Header"};
  JTable table = new JTable(data, hdr);
  table.setBorder(BorderFactory.createLoweredBevelBorder());
  JTextField tf = new JTextField();
  tf.setText("some text");

  getContentPane().setLayout(new BorderLayout(10, 10));
  getContentPane().add(b, BorderLayout.NORTH);
  getContentPane().add(table);
  getContentPane().add(tf, BorderLayout.SOUTH);

  pack();
  setVisible(true);
  InputMap map = table.getInputMap(table.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  KeyStroke esc = KeyStroke.getKeyStroke("ESCAPE");
// this is if you want to keep table's ability to cancel edits
  Action escAction = table.getActionMap().get(map.get(esc));
  Action newAction = new FilteredAction(table, escAction);
  table.getActionMap().put("cancel", newAction);
// this is if you want to cancel edits and the dialog at the same time
   /*   InputMap parent = map.getParent();
  System.out.println("parent: " + parent);
  parent.remove(esc);
  System.out.println("after parent: " + parent.get(esc));
*/  
  
   }
 
public class FilteredAction extends AbstractAction {
Action action;
JComponent comp;
KeyStroke stroke = KeyStroke.getKeyStroke("released ESCAPE");
public FilteredAction(JComponent comp, Action action) {
this.comp = comp;
this.action = action;
}

public void actionPerformed(ActionEvent e) {
if (!comp.hasFocus()) {
comp.getInputMap().put(stroke, "cancel");
action.actionPerformed(e);
} else {
comp.getInputMap().remove(stroke);
}
} 
}

   protected void showKeys(String message, InputMap map) {
   KeyStroke[] keys = map.allKeys();
  System.out.println(message);
  if (keys != null) {
  for (int i = 0; i < keys.length; i++) {
System.out.println(keys[i]);
  }
}
   }
   
   protected JRootPane createRootPane() {

  // Create an actionListener that is used to close the
  // dialog in response to the Escape key being hit
  ActionListener actionListener = new ActionListener() {
 public void actionPerformed(ActionEvent e) {
System.exit(0);
 }
  };
  
  JRootPane rootPane = new JRootPane();

  rootPane.registerKeyboardAction(
 actionListener, 
// cancel the dialog on keyReleased instead of keyPressd
 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true),
 JComponent.WHEN_IN_FOCUSED_WINDOW
  );
  return rootPane;
   }
}




Re: String Search within a JTextArea Component?

2001-06-28 Thread Dmitry Beransky

you might find this document helpful: 


___
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing



Re: Escape key not dismising dialog that contains JTable (TESTCASE included)

2001-06-28 Thread Kleopatra

After re-reading the article about keybindings at the swing connection I
have to partly correct my own post:

I wrote:

> If you don't allow edit's in the table then you can simply
> remove the registration from the
> inputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); note that you actually
> have to remove it from the parent map, that is
> table.getInputMap(ancesterCondition).getParent().remove(escKeyStroke).

That's not recommended because parent is (always?) shared among tables.
The recommended procedure to "remove" a keybinding is to bind it to a
no-action like:

inputMap.put(escKeyStroke, "none")

Greetings
Jeanette

___
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing



Re: JMenuBar Problem.

2001-06-28 Thread KC_Eilander




I thought I'd give everybody an update.  To fix the problem, all
you have to do is add the indicated two lines (how easy is that?):

 private InternalFrameListener listener=new
> InternalFrameListener() {
>   internalFrameActivated(internalFrameEvent e)  {
>if(toggle) {
> toggle=false;
> mainFrame.setJMenuBar(menu1);
>} else {
> toggle=true;
> mainFrame.setJMenuBar(menu2);
>}

   frame.validate(); //new
 frame.repaint(); //new

>   }
>   ...
>  }





Steve Barrett <[EMAIL PROTECTED]> on 06/27/2001 05:59:27 PM

Sent by:  Steve Barrett <[EMAIL PROTECTED]>


To:   KC Eilander/MTN/US/3Com
cc:
Subject:  Re: JMenuBar Problem.



I do this with jdk1.3 and haven't had any trouble, but I only replace
the menu column on the menubar, not the whole menu bar.  I have seen
lots of odd repaint problems though and this may be related.
Specifically in jdk11.3 if I am trying to change something from an
event driven method (like yours) I almost always have to perform the
action by doing something like:
 // in internalFrameActivated()
 SwingUtilities.invokeLater(new Thread() {
   public void run () {
>if(toggle) {
> toggle=false;
> //mainFrame.remove(menu2);  //doesn't help
> mainFrame.setJMenuBar(menu1);
>} else {
> toggle=true;
> //mainFrame.remove(menu1);  //doesn't help
> mainFrame.setJMenuBar(menu2);
>}
   }});

I realize the code you included is probably just a snippet but
menu1 can't be both a menubar and a menu and frame.remove() won't
work because the menubar is actually not in the JFrame but in the
container inside the frame which holds the menubar, glasspane and
contentpane.

--- [EMAIL PROTECTED] wrote:
>
>
>
>  I have an MDI application with which I want to
> switch the menubar of the desktop pane depending on
> which window is active.  In the following version of my test app,
> the toggling works, and Menu1 shows up fine on its turn, but
> garbage pixels show up for Menu2 on its turn...
>
>
> class whatever {
>  JDesktopPane desktop=new JDesktopPane();
>  JMenuBar menu1=new JMenuBar();
>  JMenuBar menu2=new JMenuBar();
>  JMenu menu1=new JMenu("menu1");
>  JMenu menu2=new JMenu("menu2");
>
>  private InternalFrameListener listener=new
> InternalFrameListener() {
>   internalFrameActivated(internalFrameEvent e)  {
>if(toggle) {
> toggle=false;
> //mainFrame.remove(menu2);  //doesn't help
> mainFrame.setJMenuBar(menu1);
>} else {
> toggle=true;
> //mainFrame.remove(menu1);  //doesn't help
> mainFrame.setJMenuBar(menu2);
>}
>//frame.repaint(); // doesn't help
>   }
>   ...
>  }
>
>  main() {
>   ...
>   mainFrame.setContentPane(desktop); // where mainFrame is
> just my
> application frame
>   mainFrame.setJMenuBar(menu1);
>   ...
>  }
> };
>
>
> ___
> Swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/swing


__
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/




___
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing