Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Help displaying a new string to GUI

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help displaying a new string to GUI

    So my assignment was to write a search and replace GUI without using the replaceAll method. My problem is in my search and replace code I am able to display the new string in a printout but I'm needing some help on how to display the new string in the original window when you open up the txt file. Can someone help me out with this please?

    import java.io.*;
    import java.util.Scanner;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Gui extends JFrame implements ActionListener
    {
    public JMenuBar mainMenu;
    public JMenu fileMenu;
    public JMenuItem searchMenuItem;
    public JMenuItem openItem;
    public JMenuItem saveItem;
    public JMenuItem exitItem;
    public JMenuItem miscItem;
    public JMenuItem aItem;
    public JMenuItem bItem;
    public JMenuItem cItem;
    public Container contentPane;
    String inputStream;
     
    JTextArea displayArea;
    JScrollPane scrollPane;
    Gui copyOfMainWindow;
     
    public static void main (String [ ]args)
    {
    Gui mainWindow = new Gui ();
    mainwindow.setVisible (true);
     
    }
     
    public Gui ()
    {
    setSize (400,600);
    contentPane = getContentPane ( );
    setDefaultCloseOperation (EXIT_ON_CLOSE);
     
    mainMenu = new JMenuBar ();
    fileMenu = new JMenu ("file");
    searchMenuItem = new JMenuItem ("search");
    searchMenuItem.addActionListener(this);
     
    mainMenu.add (fileMenu);
     
     
    openItem = new JMenuItem ("open");
    openItem.addActionListener(this);
    saveItem = new JMenuItem ("save");
    exitItem = new JMenuItem ("exit");
     
    miscItem = new JMenu ("misc");
    aItem = new JMenuItem ("a");
    bItem = new JMenuItem ("b");
    cItem = new JMenuItem ("c");
    miscItem.add (aItem);
    miscItem.add (bItem);
    miscItem.add (cItem);
     
    fileMenu.add (openItem);
    fileMenu.add (saveItem);
    fileMenu.add (searchMenuItem);
    fileMenu.add (exitItem);
    fileMenu.add (miscItem);
     
    setJMenuBar (mainMenu);
     
    displayArea = new JTextArea ("",40,50);
    scrollPane = new JScrollPane (displayArea);
    contentPane.add (scrollPane); //default: FlowLayout
     
     
     
    }
     
    public void actionPerformed (ActionEvent ex)
    {
    String whichOne = ex.getActionCommand();
    if (whichOne.equals ("open"))
    openAndDisplay ();
    else if (whichOne.equals ("search"))
    searchAndReplace ();
     
     
     
     
     
    }
     
    public void searchAndReplace ()
    {
     
    DialogBox a = new DialogBox (inputStream, displayArea);
     
     
     
    }
     
    public void openAndDisplay ()
    {
    File aFile = null;
    JFileChooser fileChooser = new JFileChooser ( );
    if (fileChooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION)
    aFile = fileChooser.getSelectedFile ( );
     
    Scanner inFile = null;
    boolean fileOK = false;
     
    try
    {
    inFile = new Scanner (aFile);
    fileOK = true;
    }
    catch (FileNotFoundException ex)
    {
    inputStream = "";
    }
     
    if (fileOK)
    {
    while (inFile.hasNext ( ))
    inputStream += (inFile.nextLine ( ) + "\n");
     
    }
     
    displayArea.setText (inputStream);
    inFile.close ();
     
    }
     
     
    }
     
     
     
     
     
    Here is where the code is for the search and replace
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class DialogBox extends JFrame implements ActionListener
    {
    private JTextField searchField;
    private JTextField replaceField;
    private JLabel searchLabel;
    private JLabel replaceLabel;
    private Container contentPane;
    private JMenuBar mainMenu;
    private JMenuItem okMenuItem;
    private JMenuItem clearMenuItem;
    private JMenu actionMenu;
     
    private String copyOfInputStream;
    private JTextArea copyOfDisplayArea;
     
     
    public DialogBox (String inputStream, JTextArea displayArea)
    {
    setSize (200,50);
    setDefaultCloseOperation (DISPOSE_ON_CLOSE);
    contentPane = getContentPane ();
     
     
    mainMenu = new JMenuBar ();
    actionMenu = new JMenu ("action");
    mainMenu.add (actionMenu);
    okMenuItem = new JMenuItem ("ok");
    clearMenuItem = new JMenuItem ("clear");
    okMenuItem.addActionListener(this);
    clearMenuItem.addActionListener(this);
     
    actionMenu.add (okMenuItem);
    actionMenu.add (clearMenuItem);
    setJMenuBar (mainMenu);
     
    searchLabel = new JLabel ("search for:");
    replaceLabel = new JLabel ("replace with:");
    searchField = new JTextField (20);
    replaceField = new JTextField (20);
     
    searchField.setText ("");
    replaceField.setText ("");
     
     
    contentPane.setLayout (new FlowLayout ());
    contentPane.add (searchLabel);
    contentPane.add (searchField);
    contentPane.add (replaceLabel);
    contentPane.add (replaceField);
     
    copyOfInputStream = inputStream;
    copyOfDisplayArea = displayArea;
     
    this.setVisible (true);
     
     
     
    }
     
    public void actionPerformed (ActionEvent ex)
    {
    String whichOne = ex.getActionCommand();
     
    if (whichOne.equals ("clear"))
    {
    searchField.setText ("");
    replaceField.setText ("");
    }
    else if (whichOne.equals ("ok"))
    {
    String original = copyOfInputStream;
    String toSearch = searchField.getText ();
    String toReplace = replaceField.getText ();
     
    int startPt = 0;
    int floatPt = 0;
    do
    {
    if (floatPt == original.length ( ))
    System.exit (0);
     
    int searchPt = 0;
     
    do
    {
    if (toSearch.charAt (searchPt) == original.charAt (floatPt))
    break;
    else
    {
    floatPt++;
    if (floatPt == original.length ( ))
    System.exit (0);
     
    }
     
    } while (true);
     
     
     
     
    startPt = floatPt;
    searchPt++; floatPt++;
     
    boolean aMatchFound = true;
    while (searchPt < toSearch.length ( ))
    {
    if (toSearch.charAt (searchPt) == original.charAt (floatPt))
    {
     
    searchPt++; floatPt++;
     
    }
    else
    {
    aMatchFound = false;
    break;
    }
    }
     
    if (!aMatchFound)
    {
     
    continue;
    }
     
    else
    {
    String first = original.substring (0,startPt);
    String second = original.substring (floatPt);
    original = first + toReplace + second;
    floatPt = startPt + toReplace.length ( );
    System.out.println ("a match found");
    System.out.println ("\nnew string = " + original + "\n");
    System.out.println ("new floatPt = " + floatPt);
     
     
    }
    } while (true);
     
     
    }
    }
     
    }
    Last edited by DaAznSmurf; January 31st, 2012 at 09:13 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting.

    how to display the new string in the original window when you open up the txt file.
    Can you explain what this means? What is "the original window"?
    You can use a text field to display a String. Or perhaps a JLabel.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    The original window is the first window that opens when you run the program, it's the one that displays the text, from the text file that you open. What's suppose to happen is when you use the search from the drop down menu, the Search and Replace window opens and you enter your desired information, however when you hit replace it just closes the Windows and doesn't replace text from the original window. However the printout lines display the correct information

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    doesn't replace text from the original window.
    You don't say what component the String is being passed to for display.
    Can you give me a clue of the name of the variable holding the String and the name of the variable in the GUI where the String is supposed to be displayed?

    The code you posted has compile errors in it. How do you execute it?
    Last edited by Norm; January 31st, 2012 at 09:28 PM.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    So the variable name for holding the string from the GUI is copyOfInputStream and the variable holding the new string is newString. And what I need is for newString to display in the Original window of the GUI

    {
                        String first = original.substring (0,startPt);
                        String second = original.substring (floatPt);
                        original = first + toReplace + second;
                        floatPt = startPt + toReplace.length ( );
                      String newString = original + floatPt;
                      copyOfDisplayArea.setText(newString);
                    }

    Here is new code that I changed recently but it's still now working

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    what I need is for newString to display in the Original window of the GUI
    Where do you what to display it? Do you have a place to show it? It must be in an object in the GUI.
    Do you have a reference variable for the Original window? Use that to access the variable where you want to pass newString.

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    oh sorry the code should be like this

    import java.io.*;
    import java.util.Scanner;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Gui extends JFrame implements ActionListener
    {
        public JMenuBar mainMenu;
        public JMenu fileMenu;
        public JMenuItem searchMenuItem;
        public JMenuItem openItem;
        public JMenuItem saveItem;
        public JMenuItem exitItem;
        public JMenuItem miscItem;
        public JMenuItem aItem;
        public JMenuItem bItem;
        public JMenuItem cItem;
        public Container contentPane;
        String inputStream;
     
        JTextArea displayArea;
        JScrollPane scrollPane;
        Gui copyOfMainWindow;
     
        public static void main (String [ ]args)
        {
         Gui mainWindow = new Gui ();
          mainWindow.setVisible (true);
     
        }
     
        public Gui ()
        {
            setSize (400,600);
            contentPane = getContentPane ( );
            setDefaultCloseOperation (EXIT_ON_CLOSE);
     
            mainMenu = new JMenuBar ();
            fileMenu = new JMenu ("file");
            searchMenuItem = new JMenuItem ("search");
            searchMenuItem.addActionListener(this);
     
            mainMenu.add (fileMenu);
     
     
            openItem = new JMenuItem ("open");
            openItem.addActionListener(this);
            saveItem = new JMenuItem ("save");
            exitItem = new JMenuItem ("exit");
     
            miscItem = new JMenu ("misc");
            aItem = new JMenuItem ("a");
            bItem = new JMenuItem ("b");
            cItem = new JMenuItem ("c");
            miscItem.add (aItem);
            miscItem.add (bItem);
            miscItem.add (cItem);
     
            fileMenu.add (openItem);
            fileMenu.add (saveItem);
            fileMenu.add (searchMenuItem);
            fileMenu.add (exitItem);
            fileMenu.add (miscItem);
     
            setJMenuBar (mainMenu);
     
            displayArea = new JTextArea ("",40,50);
            scrollPane = new JScrollPane (displayArea);
            contentPane.add (scrollPane); //default: FlowLayout
     
     
     
        }
     
        public void actionPerformed (ActionEvent ex)
        {
            String whichOne = ex.getActionCommand();
            if (whichOne.equals ("open"))
                openAndDisplay ();
            else if (whichOne.equals ("search"))
                searchAndReplace ();
     
     
     
     
     
        }
     
        public void searchAndReplace ()
        {
     
            DialogBox a = new DialogBox (inputStream, displayArea);
     
     
     
        }
     
        public void openAndDisplay ()
        {
            File aFile = null;
            JFileChooser fileChooser = new JFileChooser ( );
    	if (fileChooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION)
    		aFile = fileChooser.getSelectedFile ( );
     
            Scanner inFile = null;
            boolean fileOK = false;
     
            try
            {
                inFile = new Scanner (aFile);
                fileOK = true;
            }
            catch (FileNotFoundException ex)
            {
                inputStream = "";
            }
     
            if (fileOK)
            {
                while (inFile.hasNext ( ))
    		inputStream += (inFile.nextLine ( ) + "\n");
     
            }
     
            displayArea.setText (inputStream);
            inFile.close ();
     
        }
     
     
    }

    and
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class DialogBox extends JFrame implements ActionListener
    {
        private JTextField searchField;
        private JTextField replaceField;
        private JLabel searchLabel;
        private JLabel replaceLabel;
        private Container contentPane;
        private JMenuBar mainMenu;
        private JMenuItem okMenuItem;
        private JMenuItem clearMenuItem;
        private JMenu actionMenu;
     
        private String copyOfInputStream;
        private JTextArea copyOfDisplayArea;
     
     
        public DialogBox (String inputStream, JTextArea displayArea)
        {
            setSize (200,50);
            setDefaultCloseOperation (DISPOSE_ON_CLOSE);       
            contentPane = getContentPane ();
     
     
            mainMenu = new JMenuBar ();
            actionMenu = new JMenu ("action");
            mainMenu.add (actionMenu);
            okMenuItem = new JMenuItem ("ok");
            clearMenuItem = new JMenuItem ("clear");
            okMenuItem.addActionListener(this);
            clearMenuItem.addActionListener(this);
     
            actionMenu.add (okMenuItem);
            actionMenu.add (clearMenuItem);
            setJMenuBar (mainMenu);
     
            searchLabel = new JLabel ("search for:");
            replaceLabel = new JLabel ("replace with:");
            searchField  = new JTextField (20);
            replaceField = new JTextField (20);
     
            searchField.setText ("");
            replaceField.setText ("");
     
     
            contentPane.setLayout (new FlowLayout ());
            contentPane.add (searchLabel);
            contentPane.add (searchField);
            contentPane.add (replaceLabel);
            contentPane.add (replaceField);
     
            copyOfInputStream = inputStream;
            copyOfDisplayArea = displayArea;
     
            this.setVisible (true);
     
     
     
        }
     
        public void actionPerformed (ActionEvent ex)
        {
            String whichOne = ex.getActionCommand();
     
            if (whichOne.equals ("clear"))
            {
                searchField.setText ("");
                replaceField.setText ("");
            }
            else if (whichOne.equals ("ok"))
            {
            	String original = copyOfInputStream;
            	String toSearch = searchField.getText ();
                String toReplace =  replaceField.getText ();
     
                int startPt = 0;
                int floatPt = 0;
                do
                {
                    if (floatPt == original.length ( ))
                     System.exit (0);
     
                    int searchPt = 0; 
     
                    do
                    {
                        if (toSearch.charAt (searchPt) == original.charAt (floatPt))
                            break;
                        else
                        {
                            floatPt++;
                            if (floatPt == original.length( )) 
                            	System.exit (0);
     
                        }        
     
                    } while (true);
     
     
     
     
                    startPt = floatPt;
                    searchPt++; floatPt++;
     
                    boolean aMatchFound = true;
                    while (searchPt < toSearch.length ( ))
                    {
                       if (toSearch.charAt (searchPt) == original.charAt (floatPt))
                       {
     
                          searchPt++; floatPt++;
     
                       }
                       else
                       {
                         aMatchFound = false;  
                         break;
                       }
                    }
     
                    if (!aMatchFound)
                    {
     
                       continue;
                    }
     
                    else
                    {
                        String first = original.substring (0,startPt);
                        String second = original.substring (floatPt);
                        original = first + toReplace + second;
                        floatPt = startPt + toReplace.length ( );
                      String newString = original + floatPt;
                      copyOfDisplayArea.setText(newString);
                    }
                } while (true);
     
     
     
            }
      }
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    You didn't answer any of my questions.
    What component do you want to display the string in?

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    oh sorry, I want newString from DialogBox to pass back to displayArea in my GUI

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    Do you have a reference to the class that contains displayArea in the DialogBox class?
    Use it to reference displayArea:
    theRefToTheClass.displayArea.setText(newString);

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    so i would put in
    Gui.displayArea.setText(newString);
    but when I do all my windows still close when using the search and replace

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    Your code already calls setText() .
    Is is being executed? Add some printlns to show where the execution flow is going to prove that the setText is called.

    It is not called when I execute the program.
    The contents of the file the program reads is displayed in the text area.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    Your code executes and displays the contents of the file that was read in the text area.
    Can you explain what the problem is? It is displaying text for me.

  14. #14
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    are you running the second set of code i posted? because when i run that one all my windows still close....

    do you also do the search and replace?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    You need to create a testing version of the code that requires minimum interactions from the user.
    Pre load all the variables that need values so that all a tester needs to do is press the Search menu item and then press the ok menu item and the code does whatever it needs to do to show the problem.
    Just those two menu items are all I want to press for testing. Trying to set up the rest of the program is a big waste of time for the problem you are trying to solve.
    Set it up for easy testing or wait for someone else that is willing to go through all the steps it takes to get to where the problem is.

  16. #16
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    alright then well thank you for your help, i don't have time to do test code and stuff, thanks again

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    Good luck. In the future try to prepare the code for easy testing. This one takes too much time to set up.

    i run that one all my windows still close....
    That's because the code calls: System.exit(0);
    Remove that call and the windows will stay open

  18. The Following User Says Thank You to Norm For This Useful Post:

    DaAznSmurf (January 31st, 2012)

  19. #18
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help displaying a new string to GUI

    thank that stoped the windows from closing and actually does the replace, but a new error now lol

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help displaying a new string to GUI

    If you are getting error messages, you should post the full text here if you want help fixing them.

Similar Threads

  1. Displaying the other
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 08:58 AM
  2. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  3. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  4. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM
  5. Need help with String Trimming.. and displaying Keys on maps.
    By bh-chobo in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 9th, 2009, 01:15 PM