Results 1 to 8 of 8
  1. #1
    robme is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Stop JFrame from requesting focus so parent creating JFrame has focus

    How can I transfer focus from new Jframe back to parent object that created JFrame when you can not pass reference of parent to JFrame?

    Parent A creates new JFrame B that gains focus but does not have reference to parent A to allow B to getARef and transfer focus to A with A.requestFocus() method

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    Huh? Why can't you pass a reference of the original JFrame? If you want help, you'll have to provide an SSCCE that demonstrates what you're trying to do.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    First off, its' a rare application that benefits from using more than one JFrame. Mostly, the need for an ancilliary window is better met by using a modal JDialog; some applications are better designed using a single JFrame in conjunction with a CardLayout.

    Secondly, we have a separate section for Swing questions, so I shall move this thread there.

    Third and most important, don't double post the same question. Your other thread in New to Java has been closed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    robme is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    Thanks Darry,
    I have a Chart Canvas application added to a Frame that I repaint with stock charts and update each time I change daily price data using a key listener which works fine. What I am trying to do is initiate a Market Commentary JFrame to display information from the Canvas application which also works but the new JFrame takes focus control and I need to click on Canvas application to give the Canvas application control but then when I shift price data, focus again goes back to the JFrame. Since the JFrame is initiated by the Canvas application I can not pass the Canvas reference to the JFrame application since the Canvas reference was initiated by another ReadYahooStockPrices application that reads price data for the Canvas application. If I could pass the Canvas reference to the MarketCommentary JFrame, I woud try Chart.requestFocus(); from the JFrame and hope it would cause focus to move to the Canvas application so I can move price data and repaint the Charts on the Canvas application each day while updating market commentary.

    I hope that helps.

  5. #5
    robme is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    Not sure this helps but here is some of the code with ReadYahooStockPrices application initiating Chart3 application and Chart3 application initiating MarketCommentary application.

    Java Code:
    public class ReadYahooStockPrices extends JFrame{        
    
    public void startChart(){
            //TechnicalAnalyzer ta;
            MarketData3 mdata;
            //Charter c;
            int range=12;
            String startDate=null;
            
            //new MarketData class - send stock, HLC arrays
            //ta= new TechnicalAnalyzer(stock,startDate,range,date,data,totalDays);
            mdata= new MarketData3(stock,startDate,range,date,data,totalDays,alert);
            
            //new Chart - send MarketData identifier, stock and fix range
            //Display data in chart in frame
            Frame f = new Frame(stock+" (Press -> arrow to Forward, "+
            "<- arrow to Reverse, and up arrow for more options.)");
    
                f.addWindowListener (new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    Frame f=(Frame)e.getSource();
                    f.dispose();
                }
            });
    
            // Initiate Chart3 object
            c3 = new Chart3(stock,range,mdata);
    
            f.add(c3);
            f.pack();
            f.show();
         
        }//end startChart
    
    
    }//end ReadYahooStockPrices Class
    
    
    
    
    
    
    
    
    
    
    public class Chart3 extends Canvas{
    	
         //constructor
         public Chart3(String stk, int rg, MarketData3 mdata) 	{
    	getData();
    	// charting logic
         }//end constructor
    
    
        public void getData(){ 
            //data below from MarketData application initiated by ReadYahooStockPrices 
           // applicaiton	
            date = mdata.dateData(shift);
            startCell = mdata.startCell();
            pricesHLC = mdata.marketData();//HLC prices 
            pricesHLC = mdata.getIndicators();//ma, etc.
            trend = mdata.getTrend();
            trendLT=mdata.getTrendLT();
            trendTest=mdata.getTrendTest();
            stop12=mdata.getStop12();
            stop6Test=mdata.getStop6();//stoptest
            momChange=mdata.getMomChange();//Rev11
            message = mdata.message(); 
            trade=mdata.tradeLogic();
            tradeTest=mdata.tradeTestLogic();
            saveColor=mdata.getEndOfTrend();
            saveColorLT=mdata.getEndOfTrendLT();
            saveColorTest=mdata.getEndOfTrendTest();
            saveColorTestLT=mdata.getEndOfTrendTestLT();
            tradeVariables=mdata.tradeVariables();
            marketHigh=mdata.marketHigh();
            marketDateTest=mdata.getMarketDateTest();
            dataTest=mdata.getDataTest();
            
         
    mc = new MarketCommentary(trendTest, stop6Test);
    
            gridLayout();
            if(paint!=1)
                repaint();
            
            paint=0;
    
        }//end getData Method
    
    
    }// Chart3 Class
    
    public class MarketCommentary extends JFrame{
    
    	//Constructor to display market commentary
    	public MarketCommentary(int [][] trendTest, String [] stop6Test) {
    		//Swing logic
    		…….
    		…….
    
    		setSize(185,740);
           		setLocation(835,0);
            		setVisible(true);
    	}//end constructor	
    
    }//end MarketCommentary Class
    Last edited by pbrockway2; 12-21-2011 at 02:57 AM. Reason: code tags added

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    When posting code, use "code" tags. You put [code] at the start of the code and [/code] at the end: that way the formatting will be preserved.

    I think posting code is a good idea in this case to help explain this business of applications initiating applications. In more standard terminology an application is just that: an application. Although it might have multiple windows (frames and dialogs) which can be created, displayed and hidden by calling the appropriate methods from all over the place.

    However what would be really helpful here is a Short, Self Contained, Correct Example. Something which others can run and see for themselves the focus problem you are having. It should not contain all the code to get data or manipulate it. But it should be runnable and contain the code that creates/displays/hides the various windows.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    And why are you trying to mix AWT components (Frame, Canvas, etc) with Swing components? Why not make it much easier on yourself and work with only Swing components? Also, you'll want to avoid subclassing JFrames or any top level container (or other class for that matter), unless you want to change the innate behavior of the parent class. Your subclassing JFrame here will prevent you from being more flexible and using either JDialogs or CardLayout JPanel swapping.

  8. #8
    robme is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stop JFrame from requesting focus so parent creating JFrame has focus

    Thanks Fubarable for the clue. I converted my Chart Canvas to a JPanel, set up the marketcommentary into a JPanel and added both to a JFrame with key listener in JFrame. I had to add the keyListener to the JFrame which called the Chart JPanel to determine logic and then call the MarketCommentary JPanel to display data. It worked but to get the JFrame keyListener to work, I had to use the pack() method in the JFrame.

    I appreciate your help.

Similar Threads

  1. Replies: 1
    Last Post: 12-20-2011, 02:05 PM
  2. Replies: 1
    Last Post: 10-10-2011, 04:51 PM
  3. How to set a JFrame as focus
    By Jarob22 in forum AWT / Swing
    Replies: 4
    Last Post: 03-06-2011, 04:45 PM
  4. Maintain focus on JFrame
    By nik_meback in forum AWT / Swing
    Replies: 2
    Last Post: 12-15-2010, 01:39 PM
  5. Creating a JButton that does not show focus
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:38 PM

Posting Permissions

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