Note that there are some explanatory texts on larger screens.

plurals
  1. POListener problems with Canvas
    text
    copied!<p>I'm trying to do an multiplayer airHockey game in Java. I already have the server/client socket working. <br/> My frame has a cardLayout with 2 panels, the canvas and the jtable.<br/> I want to do a lan scan to find servers, and then choose the server I want.<br/> I made this scan and put it in a JTable in the cardLayout.<br/> When I start the server it displays the canvas card directly, no problem with listeners.<br/> But when I start the client it checks the lan then displays the jtable card. Once a server is chosed I show the canvas and connect to the server. Here is the problem, no listeners work, neither my windowListener nor the keyListener.<br/></p> <p>I tried many things but without success.</p> <p>EDIT: I just made an SSCCE about the problem:</p> <pre><code>public class test extends Canvas{ public static final int SERVEUR = 10, CLIENT = 20; public static int STATUT; JFrame frame; CardLayout cardsC = new CardLayout(); Container container; JPanel servPanel = null; public BufferStrategy strategy; public test() { Object[] options = {"SERVER", "CLIENT"}; int answer = JOptionPane.showOptionDialog(null, "Choisissez une option:", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if(answer == 0){ STATUT = SERVEUR; newFrame(); frame.setVisible(true); gameLoop(); }else if(answer == 1){ STATUT = CLIENT; newFrame(); frame.setVisible(true); cardsC.show(container, "servs"); } } public void connection(){ if(servPanel != null) servPanel.removeAll(); cardsC.show(container, "canvas"); gameLoop(); } private void gameLoop() { while(true){ try {Thread.sleep(10);}catch (InterruptedException e){e.printStackTrace();} } } public void newFrame(){ frame = new JFrame("Space Invaders 101"); frame.setPreferredSize(new Dimension(600,750)); container = frame.getContentPane(); container.setLayout(cardsC); setBounds(0, 0, 600, 750); container.add(this, "canvas"); setIgnoreRepaint(true); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo(null); if(STATUT == CLIENT){ servPanel = new JPanel(); JButton button = new JButton("Valider"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { connection(); } }); servPanel.add(button); container.add(servPanel, "servs"); } KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); manager.addKeyEventDispatcher(new MyDispatcher()); requestFocusInWindow(); requestFocus(); createBufferStrategy(2); strategy = getBufferStrategy(); cardsC.show(container, "servs"); cardsC.show(container, "canvas"); } private class MyDispatcher implements KeyEventDispatcher { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_TYPED){ System.out.println(e.getKeyChar()); } return false; } } public static void main(String argv[]) { new test(); } </code></pre> <p>}</p> <p>So, you can try it, the keyListener is working when you chose SERVER whereas it's not if you chose CLIENT. I figured out that if I don't go in gameLoop the keylistener works. Also when the other card is displayed (not the canvas one) the keyListener works.</p> <p>I'd like to know how I could get the listeners working if I displays first the jtable card.</p> <p>If you need any more information.</p> <p>Thanks in advance,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload