Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to pass JTextField txtSearch between classes

$
0
0
I am try to pass txtSearch as an JTextField object from testUserInterface class to testSearch class.
However, I got an error saying


java.lang.StackOverflowError
        at java.awt.Insets.<init>(Insets.java:103)
        at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
        at sun.awt.windows.WToolkit.getScreenInsets(WToolkit.java:567)
        at java.awt.window.init(window.java:498)
        at java.awt.window.<init>(window.java:536)
        at java.awt.Frame.<init>(Frame.java:420)
        at java.awt.Frame.<init>(Frame.java:385)
        at javax.swing.JFrame.<init>(JFrame.java:180)
        at testUserInterface.<init>(testUserInterface.java:65)
        at testSearch.<init>(testSearch.java:29)
        at testUserInterface.<init>(testUserInterface.java:168)



I wonder that something is wrong with my code,

line 29 in testSearch shows problem with public testSearch()

line 168 in testUserInterface shows problem with ts = new testSearch();

below are what I have changed

I added a method:

public JTextField getSearchBox()
{
    return txtSearch;
}


in testUserInterface class


import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    import javax.swing.KeyStroke;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    import java.util.*;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Random;
    
    import com.aetrion.flickr.*;        // For FlickrJ classes.
    import com.aetrion.flickr.photos.*;
    
    import java.io.*; //for writing and reading file
    
    public class testUserInterface extends JFrame implements ActionListener
    {
    /**
     * @param Declaring all components for user interface
     * @param Firslty, to declare components of menu such as
     * @param JMenuBar to create a menu bar
     * @param JMenu to create a menu on the menu bar such as menu File.
     * @param JMenuItem to create a submenu of the menu such as fExit is a submenu of menu File.
    */    
        private JMenuBar menuBar;
        private JMenu file, menu, help;
        private JMenuItem fExit, mUpload, mRecentPhoto, mSearch, hGuide, hAuthor;
        private JPanel upperPanel, centerPanel, InforPanel,imgPanel, recentPhotoPanel; 
        public  JPanel searchPhotoPanel;
        private JLabel welcome, lWelcome, lAbout, recentPhotoTitle, searchTitle;
        
        ImageIcon iWelcome = new ImageIcon("IMAGE/flickr.jpg", "Flickr Image");
        ImageIcon iAbout = new ImageIcon("IMAGE/FairyTail.jpg", "Fairy Tail Team");
    
    
     * @param this to declare all components that should be in Search Panel
     * @param JButton btnStart, btnExit.
     * @param JLabel lblSearch
     * @param JTextField txtSearch
     */
        private JButton btnStart, btnExit;
        private JLabel lblSearch;
        private JTextField txtSearch;
        private JPanel sContent;
        private testSearch ts;
        
        
        public testUserInterface ()
        {
            
            //Add regular components to the window, using the default BorderLayout.
            Container contentPane = getContentPane(); //the app window        
            // create upper panel to hold menu bar on the top of interface
            upperPanel = new JPanel();
            contentPane.add(upperPanel, BorderLayout.NORTH);
            upperPanel.setLayout(new BorderLayout());
            // Create menu bar to hold menus and submenus
            menuBar = new JMenuBar();
            //add menu bar to the top of interface
            upperPanel.add(menuBar);      
                
            // Build File menu and its short-cut key
            file = new JMenu ("File");        
            file.setMnemonic(KeyEvent.VK_F);
            file.getAccessibleContext().setAccessibleDescription(
                "File related operations");
            // Create a submenu for File menu and ist short-cut key
            fExit = new JMenuItem ("Exit", KeyEvent.VK_E);
            fExit.addActionListener(this);
            // add components of File menu
            menuBar.add(file);
            file.add(fExit);
            
            // Build  Menu menu and its short-cut key
            menu = new JMenu ("Menu");        
            menu.setMnemonic(KeyEvent.VK_M);
            menu.getAccessibleContext().setAccessibleDescription(
                "Menu related operations");
            // Create submenus for Menu menu and its short-cut key
            mRecentPhoto = new JMenuItem ("Check Recent Photos", KeyEvent.VK_C);
            mSearch = new JMenuItem ("Search Photos", KeyEvent.VK_S);
            // add components of File menu
            menuBar.add(menu);
            menu.add(mRecentPhoto);
            mRecentPhoto.addActionListener(this);
            menu.addSeparator();
            menu.add(mSearch);
            mSearch.addActionListener(this);
            
            // Build  Help Menu and its short-cut key
            help = new JMenu ("Help");     
            help.setMnemonic(KeyEvent.VK_H);
            help.getAccessibleContext().setAccessibleDescription(
                "Help related operations");
            // Create submenus for Help Menu and their short-cut key
            hGuide = new JMenuItem ("Short-Cut Key Guide", KeyEvent.VK_S);
            hAuthor = new JMenuItem ("About Us", KeyEvent.VK_A);
            // add components of help menu
            menuBar.add(help);
            help.add(hGuide);
            hGuide.addActionListener(this);
            help.addSeparator();
            help.add(hAuthor); 
            hAuthor.addActionListener(this);
            
            // Build Center Panel which is stayed in the center of interface.
            centerPanel = new JPanel();
            centerPanel.setLayout (new FlowLayout());
            contentPane.add(centerPanel, BorderLayout.CENTER);      
            
            // Build Infor Panel and display Image and Text.
            InforPanel = new JPanel();
            InforPanel.setLayout (new BorderLayout());
            centerPanel.add(InforPanel);
            // Build components for Infor Panel
            welcome = new JLabel ("Welcome to Java Flickr Application", JLabel.CENTER);
            InforPanel.add(welcome, BorderLayout.NORTH );
            lWelcome = new JLabel (iWelcome);   
            InforPanel.add(lWelcome);
            
            //Build recent Photo Panel to display most recent photos
            recentPhotoPanel = new JPanel();
            recentPhotoPanel.setLayout (new BorderLayout());
            recentPhotoPanel.setVisible(false);
            recentPhotoTitle = new JLabel ("Most Recent Photos on Flickr");
            recentPhotoPanel.add(recentPhotoTitle);
            centerPanel.add(recentPhotoPanel);
            
            //Build Search Panel to display area where user can search photos with a keyword
            searchPhotoPanel = new JPanel ();
            searchPhotoPanel.setLayout (new BorderLayout());
            searchPhotoPanel.setVisible(false);
            searchTitle = new JLabel ("Welcome to Search Area", JLabel.CENTER);
            searchPhotoPanel.add(searchTitle, BorderLayout.NORTH);
            centerPanel.add(searchPhotoPanel);
            
    /**
    
         * @param code below to build all components that should be in Search Panel
         * @param adding all components and display them in Search Panel
         * @param Handling events when user clicks on button Search 
         * @param JTextField txtSearch
         */
                // Build Components for Search
                sContent = new JPanel(); // content Panel to display
                sContent.setLayout( new FlowLayout()); 
                lblSearch =  new JLabel("Search");
                txtSearch = new JTextField(20);
                btnStart = new JButton("Start");
                btnExit = new JButton("Exit");
                ts = new testSearch();
                // add action handling for 2 buttons
                btnStart.addActionListener(ts);
                btnExit.addActionListener(ts);
                //Add components to Search Panel and action handling    
                sContent.add(lblSearch);
                sContent.add(txtSearch);
                sContent.add(btnStart);
                sContent.add(btnExit);
                searchPhotoPanel.add(sContent, BorderLayout.CENTER);
                // End Search components
                
            }
            
            public static void main(String[] args) 
            {
                testUserInterface test = new testUserInterface ();
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.setTitle("Java Flickr Application");
                test.setSize(800,600);
                test.setVisible(true);
            }
            
            public JTextField getSearchBox()
            {
                return txtSearch;
            }
            
            public void actionPerformed(ActionEvent e) 
            { 
                if (e.getSource() instanceof JMenuItem) 
                {
                    JMenuItem source = (JMenuItem)(e.getSource());
                    //If user selects "Short-cut Key Guide" sub-menu, a dialog will be shown
                    if (source.equals(hGuide))
                    {
                        
                                JOptionPane.showMessageDialog( testUserInterface.this,
                                "Select appropriate menu option that you want from Java Flickr Application\n" +
                                "User should navigate to menu before submenu\n" +
                                "Alt + F => select File menu \n" +
                                "Alt + E => select Exit submenu \n" +
                                "Alt + M => select Menu menu \n" +
                                "Alt + C => select Check Recent Photos submenu \n" +
                                "Alt + S => select Search submenu \n" +
                                "Alt + H => select Help menu \n" +
                                "Alt + S => select Short-cut Key Guide submenu \n" +
                                "Alt + A => select About Us submenu \n" ,
                                "Help Contents", JOptionPane.PLAIN_MESSAGE);
                        
                    }
                    //If user selects "About Us" sub-menu, a dialog will be shown
                    if (source.equals(hAuthor))
                    {
                        JOptionPane.showMessageDialog( testUserInterface.this,
                                "Java Flickr Application is created by Fairy Tail Group \n\n" +
                                "Hung Vu Pham\n\n" +
                                "Jennifer Naiaretti\n\n" +
                                "Panagiota Tzana\n\n" + 
                                "Athina Ioannou",
                                "About Us", JOptionPane.PLAIN_MESSAGE, iAbout);
                    }
                    //If user selects "Check Recent Photos" sub-menu, a panel to retrieve all photos from Flickr and display photos 
                    if (source.equals(mRecentPhoto))
                    {
                        InforPanel.setVisible(false);
                        searchPhotoPanel.setVisible(false);
                        recentPhotoPanel.setVisible(true);
                    }
                    //If user selects "Search Photos" sub-menu, a panel to help user search photos on Flickr with specific keyword
                    //This panel also contains all photo after searching.
                    if (source.equals(mSearch))
                    {
                        InforPanel.setVisible(false);
                        recentPhotoPanel.setVisible(false);
                        searchPhotoPanel.setVisible(true);   
                        
                    }
                    //If "Exit" sub-menu is selected, a window closes.
                    if (source.equals(fExit))
                    {
                        dispose();
                        System.exit(0);
                    }
                }
            }
        }



then I call the method getSearchBox()

 public testSearch()
 {
  getSearchBox();
 }


in testSearch class

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Random;
    import javax.swing.*;
    import com.aetrion.flickr.*;        // For FlickrJ classes.
    import com.aetrion.flickr.photos.*;
    
    public class testSearch extends testUserInterface implements ActionListener{
        private PhotoPanel pp;
        private String keyWord;
        private String photoUrl;
        private PhotoFinder pf;
        private Timer timer;
        private ArrayList<Photo> photoList;
        private String originalKeyWord, oldKeyWord;
        /**
         * Creates new form SearchInterface
         */
        public testSearch() 
        {
            getSearchBox();
        }
        
        public void init()
        {
            pp = new PhotoPanel();
            pp.setLayout(new FlowLayout());
            searchPhotoPanel.add(pp, BorderLayout.SOUTH);
            pp.setVisible(true);
            pf = new PhotoFinder();
            photoList = new ArrayList<Photo>();
            originalKeyWord = "";
            oldKeyWord = "";
            //titles = new ArrayList<String>();
            timer = new Timer(1000, this);
            //btnStart = new JButton("Search");
            pack();
            setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            
            if (e.getActionCommand().equals("Exit"))
            {
                    System.exit(0);
            }
            if (e.getActionCommand().equals("btnStart"))
            {
                if (timer.isRunning())
                {
                    timer.stop();
                }
                keyWord = getSearchBox().getText();
                originalKeyWord = keyWord;
                oldKeyWord = keyWord;
                timer.start();
            }
            else
            {
                if (keyWord != null)
                {
                   Random r = new Random();
                    photoList.clear();
                    
                    photoList = pf.doQuery(keyWord);
                    int i = r.nextInt(photoList.size());
                    pp.setImage(photoList.get(i).getSmallUrl());
                    keyWord = photoList.get(i).getTitle();
                    Iterator it = photoList.iterator();
                    while (keyWord.equals("")) 
                    {
                        Photo p = (Photo)it.next();
                        keyWord = p.getTitle();
                    }
                   // if (oldKeyWord.equals(keyWord))
                    //{
                       // keyWord = originalKeyWord;
                       // }
                    //oldKeyWord = keyWord;
                    keyWord = originalKeyWord;
                    {
                         //int num = photoList.size();
                    //for (int k=1; k< num; k++)
                    pp.repaint();
                    }
                    System.out.println(keyWord);
                }
            }
        }
    }


Finally, I got error with StackOverflow

How can I fix this error ??? please help me sorting out the problem

Thanks a lot for your help

Kind Regards

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>