How To Access Keylistener If It Is Within A Method
Feb 1, 2014
I am trying to make a program where a ball moves up continuously when you press space, then begins to move down when you reach a certain point. My method for this is to have a timer, and have a method that does this: When you press space, add 10 y coords every second (using a timer), and if you reach 470 y, then begin to drop 10 y coords. I made a method to hold the keylistener, and am running that method inside the actionPerformed method which is within another class. However, since it is a method, I cannot add my keylistener to my frame in the main method.
main
error line 9
Java Code: import javax.swing.*;
public class Main {
Why is it that a protected method cannot be accessed polymorphically outside its package. I suspect the reason is that some compile-time checks have to be performed. Is this correct?
I have 2 classes. TestClassA has 1 getter and 1 setter. From my second class, TestClassB, I want to access the getter method of TestClassA. If I try to access it, I get null. How do I do it?I do not want the methods to be declared as static. How can the getter method value be printed in TestClassB (without marking anything as static)?
public class TestA { private String name; public String getName() { return name;
Why can't I access a method from another class? For example, I want to get the value of get method from another class. It's giving me an error on if(getExamType() == 'M') That's what I've done, for example:
Java Code:
public static Exam[] collateExams(Exam[] exams){ Exam [] r = new Exam[50]; r = exams; Exam [] finalExam = new Exam[50]; for(int i = 0; i < r.length; i++) { if(getExamType() == 'M') { } } return r; mh_sh_highlight_all('java');
This time I have to make a Black Jack game ( I guess this is a classic) I have created Three classes for this BlackJack( Main), Card, and Player.
What I am trying to do is put the Give one card to the player and remove it from the deck into a separate procedure because I will be doing this several times during the game.
This is the code I have so far Under the class BlackJack.
now, i want to access a managed bean's method to execute a service call related to the code embedded in the hyperlink.
My Managed bean
@ManagedBean(name="details") @SessionScoped public class XXXX extends Bean implements Serializable{ public XXXX(){...... } public myMethod(..){ service.getDataRelatedToHyperlinkCode(....passing code here to fetch details from DB) } }
if i use postConstruct annotation it is getting executed only once since it is a session scope. and point to be noted is i cannot use viewscope and requestscope.
So I have created a array list of pizza toppings, only 4 in total, and each ingredient needs to have a cost property. I'm supposed to access the cost through a get / set method, as the user can create their own pizza and it will total up the cost, but how to.
I've got a CDI bean which is a facade for a JPA entity. Such entity has got a many to one relationship with itself and I've got the following method:
public Set<Account> getChildren() { return this.children; }
which is called in a JSF/Facelets page:
<h:commandLink action="#{accountController.destroy}" value="#{bundle.ListAccountDestroyLink}" rendered="#{item.children.size() == 0}"/>I then decided to return an unmodifiable set and changed getChildren() accordingly: public Set<Account> getChildren() { return Collections.unmodifiableSet(children); }
The page now reports this error:
java.lang.IllegalAccessException: Class javax.el.BeanELResolver can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65) at java.lang.reflect.Method.invoke(Method.java:588)
[Code] ....
javax.el.BeanELResolver is incorrectly failing because it's trying to invoke the size() method using reflection without correctly taking into account the method visibility (which is indeed invokable programmatically). I'm running NetBeans 6.8, Glassfish 3.0 and jdk1.6.0_17.
I'm trying to add a KeyListener, but every time i do i have to implement methods that i will not use.
import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class GameFrame extends JFrame implements KeyListener{ private GamePanel panel; */ public GameFrame()
[code]....
it works just concerned about the unimplemented methods.I'm supposed to make a row of images all the way across the screeen and down it unless the row contains the player. which is always at the bottom.
private void drawRow(int y, int blockWidth, int blockHeight, GameRow row, Graphics g) { Image img = Toolkit.getDefaultToolkit().getImage("blocks.png"); for(int i = rows.size()-1; i >= 0; i--) { if(rows.get(i).isEmpty()) { g.drawImage(img, width,y,blockWidth, blockHeight, null); } } }
As of right now it will only paste on the FIRST column not the rows.
I'm programming a game, but the keyListener doesn't work. Here is the source code:
import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class InputHandler implements KeyListener{ public InputHandler(Game game) { game.addKeyListener(this);
[code]....
My Question is: Why will it not say "up", if I press "w"?
I am trying to program a version of the "Worlds Hardest Game" using ready to program and applets for a final class project. Unfortunately I cannot figure out how to get my keylistener to work because I am using a loop for the enemies to go back and forth.
import java.applet.*; import java.awt.*; import java.awt.event.*; public class HardestGame extends Applet implements KeyListener, MouseListener { final int WIDTH = 400; final int HEIGHT = 123; int myX = 400; int myY = 400;
I wrote this program that makes a little square move around with the wasd keys. A huge problem that I had while creating it is adding the keylistener to my JFrame. In the end, I ended up creating an anonymous KeyListener class and adding that anonymous class to the JFrame. However, this anonymous class requires two other methods that I don't use, so any other thing I could have done to add these keylisteners to the JFrame without creating an anonymous class? I had to create 4...
I want to try and run a thread that starts running on start and quits after pressing a key.I cant seem to detect keypresses. ci cant seem to add code: Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.
its just a code snippit without links or anything..this is a small part... but how do i add a keyListener in here that listends to a random key?The class implements KeyListener and the overrided method "keyPressed" sets the isRunning boolean to false... but the keyPressed method is never executed.
public static void main(String[] args) { Thread thread = new Thread() { public void run() { while(isRunning){ System.out.println("running"); try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; thread.start(); }
I start my thread, it's for a real basic game I'm learning. Everything was working fine, until I got to recognizing keys. It runs, and I can close using the mouse on the close command, but the keys aren't being generated from the keyboard. I copied it to a working sample, and here are the two files.
The idea was to set the return value to true if any key is pressed, thus quitting, for this short sample program. Problem, it never recognizes any keys pressed on the keyboard. New to java and threading.
I have a JMenuItem "Find" in the Edit Menu. I want to add a shortcut key to the JMenuItem.
For Find for example i want to use Ctrl + F
Here is my 'Action' for 'Find' which can be used via Edit->Find. It works.
Find = new AbstractAction(){ public void actionPerformed(ActionEvent e){ String word = JOptionPane.showInputDialog(new MainWindow(),"","Find",PLAIN_MESSAGE); new WordSearcher(textArea,word); } };
WordSearcher() is a class i am using to search the word
Now i want to add a KeyListener for ctrl+F, for doing the same purpose. Even this one works.
private KeyListener k1 = new KeyAdapter() { public void keyPressed(KeyEvent e) { if(e.isControlDown()) { if(e.getKeyCode()== KeyEvent.VK_F)
[Code] ....
But the problem here is i have to write the same code twice. Is there some way by which I can Use the already written Action Find in the KeyListener.
i can use this current code to change it so that it use TextArea in jsp and the keylistener code in servlet. i know i have to take out the JFrame,JTextfield and ContentPane but still i could not do it.
I had a game written in a 500x400 window using JFrame and a threaded JPanel. It used KeyListener to detect user input and worked just fine, problem was it was very click dependent and therefore I wanted to put it into fullscreen mode so I would no longer come across the problem of clicking outside the window and being taken out of the game. But when I ported the game to Full Screen Exclusive Mode the KeyListener no longer seems to be working and I have no clue why. I tried using KeyBindings as well, that code didn't work and is left commented out in the FSEM code.Here is my code for the windowed application:
import java.awt.Container; import javax.swing.*; public class Main extends JFrame{ private GamePanel panel; public Main(){ panel = new GamePanel();
make the keylistener "listen" across the entire application? Currently whenever I've used it I have always attached it to something else, like a JTextField or the like. However that doesn't work all that well if I for example want to close down the current JDialog by pressing escape, because no object that has the keylistener attached to it is currently focused.
When I searched around for the answer if found similar questions on stackoverflow, but those seemed to focus on listening globally, even when the application itself is not focused, which isn't quite what I want.
Here is a simple piece of code that I'm currently experimenting with:
It's supposed to simply create a frame and give me a message whenever I press the A or D buttons, as well as notify me whenever they are released. As of currently it obviously doesn't work because the keylistener hasn't been attached to anything, but that's also where I'm unsure of how to proceed.
I am trying to create the Breakout game and cannot figure how to get the paddle to move and make constraints to bricks disappear when the ball hits them but not the paddle. I tried this:
I have noticed, that if i call setVisible(true) before i add the button, then it works, but the button is not displayed... How can i achieve that the button is displays AND the KeyListener works?