Results 1 to 6 of 6
  1. #1
    premtonx is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Java search problem

    I have to do a word search app.

    I made the gui and all the codes I just do not know how to link the search button to search from the textarea.

    The code is working:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.BoxLayout;
    import java.awt.FlowLayout;
    import javax.swing.JLabel;
    import java.awt.Font;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import java.awt.Scrollbar;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.TextField;
    import java.awt.Button;
    import java.awt.Label;
    import javax.swing.JTextField;
    
    
    public class frame1 extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField found_field;
    	private JTextField search_field;
    
    	
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					frame1 frame = new frame1();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public frame1() {
    		setTitle("Word Search");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 571, 401);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		found_field = new JTextField();
    		found_field.setBounds(305, 319, 39, 20);
    		contentPane.add(found_field);
    		found_field.setColumns(10);
    		
    		JLabel lblNewLabel = new JLabel("Enter or Copy & Paste Text");
    		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
    		lblNewLabel.setBounds(142, 29, 164, 14);
    		contentPane.add(lblNewLabel);
    		
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setBounds(23, 82, 509, 216);
    		contentPane.add(scrollPane);
    		
    		final JTextArea text_field = new JTextArea();
    		scrollPane.setViewportView(text_field);
    		text_field.setLineWrap(true);
    		text_field.setWrapStyleWord(true);
    		text_field.setRows(10);
    		text_field.setColumns(100);
    		
    		Scrollbar scrollbar = new Scrollbar();
    		scrollbar.setBounds(515, 136, 17, 50);
    		contentPane.add(scrollbar);
    		
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.setBounds(0, 0, 555, 21);
    		contentPane.add(menuBar);
    		
    		JMenu mnNewMenu = new JMenu("File");
    		menuBar.add(mnNewMenu);
    		
    		JMenuItem mntmResetAll = new JMenuItem("Reset All");
    		mntmResetAll.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				search_field.setText("");
    				found_field.setText("0");
    				text_field.setText("");
    			}
    		});
    		mnNewMenu.add(mntmResetAll);
    		
    		JMenuItem mntmExit = new JMenuItem("Exit");
    		mntmExit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				System.exit(0);
    			}
    		});
    		mnNewMenu.add(mntmExit);
    		
    		Label label = new Label("#Found");
    		label.setFont(new Font("Dialog", Font.BOLD, 12));
    		label.setBounds(251, 317, 48, 22);
    		contentPane.add(label);
    		
    		search_field = new JTextField();
    		search_field.setBounds(23, 319, 129, 20);
    		contentPane.add(search_field);
    		search_field.setColumns(10);
    		
    		JButton btnNewButton = new JButton("Clear");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text_field.setText("");
    			}
    		});
    		btnNewButton.setBounds(320, 25, 89, 23);
    		contentPane.add(btnNewButton);
    		
    		JButton btnNewButton_1 = new JButton("Search");
    		btnNewButton_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    		        if(text_field.getText().equals(""))
    		        {
    		            JOptionPane error = new JOptionPane();
    		            error.showMessageDialog(null,"Target TextArea is empty", " target text is empty", JOptionPane.WARNING_MESSAGE);
    		        }
    		        else if(search_field.getText().equals(""))
    		        {
    		            JOptionPane error = new JOptionPane();
    		            error.showMessageDialog(null,"Search TextField is empty", "search text is empty", JOptionPane.WARNING_MESSAGE);
    		        }
    			}
    		});
    		btnNewButton_1.setBounds(156, 318, 89, 23);
    		contentPane.add(btnNewButton_1);
    		
    		JButton btnReset = new JButton("Reset");
    		btnReset.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				search_field.setText("");
    				found_field.setText("0");
    			}
    		});
    		btnReset.setBounds(389, 318, 72, 23);
    		contentPane.add(btnReset);
    		
    		JButton btnExit = new JButton("Exit");
    		btnExit.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent e) {
    				 JOptionPane error = new JOptionPane();
    		            error.showMessageDialog(null,"Good-Bye-Thank You!", " E", JOptionPane.INFORMATION_MESSAGE);
    				System.exit(0);
    			}
    		});
    		btnExit.setBounds(471, 318, 61, 23);
    		contentPane.add(btnExit);
    	}
    }

  2. #2
    premtonx is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java search problem

    I forgot to mention that these two are the main problem that are not working at my project:

    • Searches text for specified string
    • Properly counts multiple occurrences

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Java search problem

    Well, your code already checks if either the text field or search field are empty.
    That means you know how to get the Strings held in them.
    So where is the problem?
    Here's the API for String, which has methods for finding strings within strings.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    premtonx is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java search problem

    Quote Originally Posted by Tolls View Post
    Well, your code already checks if either the text field or search field are empty.
    That means you know how to get the Strings held in them.
    So where is the problem?
    Here's the API for String, which has methods for finding strings within strings.
    can anyone do that cuz i have no idea how to do that i am a beginner :S

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Java search problem

    You managed to write the above code and yet cannot use the String API?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: Java search problem

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  2. search problem
    By jperson in forum New To Java
    Replies: 1
    Last Post: 12-25-2010, 03:48 AM
  3. i have a problem with search in java
    By mikethebiker in forum New To Java
    Replies: 1
    Last Post: 11-10-2010, 10:53 AM
  4. Binary search problem
    By billy in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 08:43 PM
  5. Binary Search Problem
    By sharpnova in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •