Home arrow static arrow Java Programming [Archive] - Help reqd using Find dialog box with JTextField
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Help reqd using Find dialog box with JTextField
This topic has 2 replies on 1 page.

Posts:5
Registered: 7/21/04
Help reqd using Find dialog box with JTextField  
Aug 3, 2004 12:38 AM



 
Hi i am using JTextField and getting a file read and then displayed in JTextField. Now i have used a dialog box for finding string within that file displayed in JTextField. I want to make it such that it ignores cases while finding the string and also want to make find next available in Find dialog box.

My code is: -

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.swing.*;
public class TextAreaEx extends JPanel implements ActionListener {
static JFrame fr;
JButton button1;
JTextArea tx;
TextAreaEx()
{
super(new BorderLayout());
tx=new JTextArea();
FileInputStream fis=null;
StringBuffer sb=new StringBuffer();
try{
fis=new FileInputStream("C:/customers.xml");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String h="";
while((h = br.readLine()) != null) {
sb.append(h + "\n");
}
}
catch(Exception e)
{ e.printStackTrace();}

tx.setText(sb.toString());

button1=new JButton("Find");
button1.addActionListener(this);
add(button1,BorderLayout.PAGE_START);
button1.setFocusable(false);
JScrollPane p1=new JScrollPane(tx);
add(p1);

}
public static void createShow()
{
JFrame.setDefaultLookAndFeelDecorated(true);
fr=new JFrame("My Frame");
TextAreaEx ob=new TextAreaEx();
ob.setOpaque(true);
fr.setContentPane( ob);
fr.setSize(400,400);
// fr.pack();
fr.setVisible(true);
}
public static void main(String s[])
{
createShow();
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1)
{

String s=(String)JOptionPane.showInputDialog(fr,"FIND:\n","Find",JOptionPane.INFORMATION_MESSAGE,
null,null,null);
System.out.println(s);
String text=tx.getText();

int offset=text.indexOf(s);
System.out.println(offset);
tx.setFocusable(true );
tx.select(offset,s.length()+offset );

}
}
}

 

Posts:5,451
Registered: 17/08/03
Re: Help reqd using Find dialog box with JTextField  
Aug 3, 2004 5:59 PM (reply 1 of 2)



 
Here's something really rough, but it might get you started (F3 key triggers additional find)
import java.awt.BorderLayout;import java.awt.event.*;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.InputStreamReader;import javax.swing.*;class TextAreaEx extends JFrame implements ActionListener,KeyListener{  JButton button1;  JTextArea tx = new JTextArea();;  int startFrom = 0;  int offset = 0;  String find = "";  String text = "";  TextAreaEx()  {    super("My Frame");    FileInputStream fis=null;    StringBuffer sb=new StringBuffer();    try    {      fis=new FileInputStream("Test.txt");      BufferedReader br=new BufferedReader(new InputStreamReader(fis));      String h="";      while((h = br.readLine()) != null)      {        sb.append(h + "\n");      }    }    catch(Exception e){e.printStackTrace();}    text = sb.toString();    tx.setText(text);    text = text.toLowerCase();    button1=new JButton("Find");    button1.addActionListener(this);    getContentPane().add(button1,BorderLayout.PAGE_START);    button1.setFocusable(false);    JScrollPane p1=new JScrollPane(tx);    getContentPane().add(p1);    JFrame.setDefaultLookAndFeelDecorated(true);    tx.addKeyListener(this);    setSize(400,400);    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    setLocation(400,300);    setVisible(true);  }  public static void main(String s[]){new TextAreaEx();}  public void actionPerformed(ActionEvent e)  {    startFrom = 0;    offset = 0;    if(e.getSource()==button1)    {      find = (String)JOptionPane.showInputDialog(this,"FIND:\n","Find",JOptionPane.INFORMATION_MESSAGE,null,null,null);      find = find.toLowerCase();      findWord();    }  }  public void findWord()  {    offset = text.indexOf(find,startFrom);    if(offset > -1)    {      tx.setFocusable(true);      tx.select(offset,find.length()+offset );      startFrom = find.length()+offset+1;    }    else JOptionPane.showMessageDialog(this,"No (more) matches");  }  public void keyPressed(KeyEvent ke)  {    if(ke.getKeyCode() == KeyEvent.VK_F3)    {      findWord();    }  }  public void keyReleased(KeyEvent ke){}  public void keyTyped(KeyEvent ke){}}
 

Posts:5
Registered: 7/21/04
Re: Help reqd using Find dialog box with JTextField  
Aug 4, 2004 10:50 PM (reply 2 of 2)



 
Thanx Michael its working now that i have created a dialog box i want to create custom dialog box for find dialog which includes check boxes for matching cases as well and searches data from JTextArea
 
This topic has 2 replies on 1 page.