Skip to main content
gamedev.net
🔒 Locked

JPanel Focus

Started by NickyP101 7 replies 1.5k views
Original Post
NickyP101
NickyP101


public class War extends JFrame{
 
 
    String serverAddress;
    int width;
    int height;
 
    int tileWidth;
    int tileHeight;
 
    WarPanel panel;
    
    JPanel jpanel;
    Launch lPanel;
    ServerSelect sPanel;
 
 
    public War(){
        
        showLaunch();
 
    }
    
    public static void main(String args[]){
            
            new War();
        
    }
 
 
    public void init(String ip){
 
        jpanel.removeAll();
        serverAddress = ip;
        panel = new WarPanel(serverAddress);
 
        jpanel = (JPanel)getContentPane();
        jpanel.add(panel, BorderLayout.CENTER);
 
 
 
        BufferedImage cur = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
        Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cur,new Point(0,0),""); 
        setCursor(customCursor);
        setFocusable(true);
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize(800,800);
        setVisible(true);
 
        System.out.println(hasFocus());
        panel.init();
        panel.game();
 
    }
    
    
    public void showLaunch(){
 
 
        lPanel = new Launch(this);
 
        jpanel = (JPanel)getContentPane();
        jpanel.add(lPanel, BorderLayout.CENTER);
 
        System.out.println(hasFocus());
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize(800,800);
        setVisible(true);
 
    }
    
        public void showServerSelect(){
 
        jpanel.removeAll();
        sPanel = new ServerSelect(this);
 
        jpanel = (JPanel)getContentPane();
        jpanel.add(sPanel, BorderLayout.CENTER);
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize(800,800);
        setVisible(true);
 
 
    }
    
        public void showGame(String address){
 
            lPanel = null;
            sPanel = null;
            init(address);
 
    }
 
 
}
 

Launch and ServerSelect extend JPanel, they show fine and work fyn, when the button is clicked in ServerSelect, the method in was showGame is called. It shows the game, but still not focued on game, its not handling any key or mouse events and wont even close when i click the close button top right hand corner. Maybe its because im calling this method from within the other panel? Im a lil lost, please ne help would be awsome :) Cheers, Nick
H_o_p_s
H_o_p_s
Can't you do a request focus call on JPanels/Frames ?
BRING BACK THE BLACK (or at least something darker)
NickyP101
NickyP101
Im the WarPanel i request the focus, but it doesnt work. I am going about this the right way?
H_o_p_s
H_o_p_s
Make sure that it is the last thing that requests focus, other then that, I don't know, I rarely get focus problems...

[Edited by - H_o_p_s on March 19, 2005 11:18:11 PM]
BRING BACK THE BLACK (or at least something darker)
NickyP101
NickyP101
Yeah this is a rare thing for me 2. I never really get along with the Java GUI features lol anyway thx, ill check my code again :) One more thing, if u were tying to do wat im doing, would u go about it the same way? Have 3 JPanels, show one, then remove that from frame and show the next, then remove that from the frame and show the next?? Or is there a more appropriate way?

Haha its funny actually, this weekend i felt lyk having a little brake from the actual game programming and have an easy programming weekend, just make the mode selection screen (Multiplayer single player) and then make a Server Selection Screen, lol all in all the networking and everything took 20 mins, but ive been trying to fix this for lyk 12 hours lol
NickyP101
NickyP101
Ok it works perfectly if i dont call the showLaunch and showServerSelect. It straight away calls show game, it shows the game, the game handles all the events etc etc works perfectly. So i still think they problem is that i call showGame from a method in the ServerSelect panel, so is the game running from the ServerSelect panel?

Im soo confused lol
H_o_p_s
H_o_p_s
public void showServerSelect(){    //jpanel.removeAll();//WE'LL DO THIS LATER   sPanel = new ServerSelect(this);    //jpanel = (JPanel)getContentPane();//jpanel IS ALREADY REFERENCING THE CONTENT PANE   jpanel.removeAll();   jpanel.add(sPanel, BorderLayout.CENTER);   //setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );//ALREADY SET IN init()   //setSize(800,800);//ALREADY SET IN init()   //setVisible(true);//ALREADY SET IN init()   sPanel.requestFocus();//THIS IS WHAT YOU NEED }


Hope this helps... and just for reference, please try and spell out your words. It makes you sound more intelligent. Especially when you are trying to misspell on purpose. 'lyk' != 'like'
BRING BACK THE BLACK (or at least something darker)
NickyP101
NickyP101
Haha thanks for the advice H_o_p_s :) Haha ok ill try to spell everything out from now on, im not really that intelligent though :)

Anyway, ill give that a try and see how i go :) Thx again.

L8er lol, Jokes mate, Later :)
NickyP101
NickyP101
Sorry no luck there :( Dont worrie about it :) Ill give it a rest, it will come to me :P Have a good one!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.