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
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.
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
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.
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.
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
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.
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.
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.