Results 1 to 4 of 4
- 12-14-2012, 03:29 AM #1
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
How to pass JTextField txtSearch between testUserInterface class and testSearch class
I am try to pass txtSearch as an JTextField object from testUserInterface to testSearch.
However, I got an error saying
PHP Code: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:
in testUserInterface classPHP Code:public JTextField getSearchBox() { return txtSearch; }
PHP Code: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()
in testSearch classPHP Code:public testSearch() { getSearchBox(); }
Finally, I got error with StackOverflowPHP Code: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); } } } }
How can I fix this error ??? please help me sorting out the problem
Thanks a lot for your help
Kind RegardsLast edited by thientanchuong; 12-14-2012 at 03:38 AM.
-
Re: How to pass JTextField txtSearch between testUserInterface class and testSearch c
You appear to be using inheritance incorrectly. There is likely no reason to have the one class extend the other, and in fact your doing this is what is causing your problem. Your base class creates an instance of the Child class (something that should *never* be done), and then this will force the Child class to continue to create itself infinitely, or until the JVM runs out of memory.
Solution: completely re-think your design.
- 12-14-2012, 02:32 PM #3
Re: How to pass JTextField txtSearch between testUserInterface class and testSearch c
If you don't understand my response, don't ignore it, ask a question.
-
Re: How to pass JTextField txtSearch between testUserInterface class and testSearch c
And he was asked in his previous thread to post links to cross-posts but yet he ignores this request. Shame.
Similar Threads
-
Pass a string from a class to another
By Matth963 in forum New To JavaReplies: 1Last Post: 09-21-2012, 09:32 AM -
JTextField on JPanel-class within JFrame-class
By floris in forum AWT / SwingReplies: 5Last Post: 06-25-2012, 12:54 PM -
Pass text to jTextArea class from another class(different layers)
By jeata in forum New To JavaReplies: 1Last Post: 08-02-2011, 01:34 PM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
unable to pass value from one class to another
By ddatta8 in forum New To JavaReplies: 14Last Post: 12-28-2008, 02:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks