Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 16 May 2004
Offline Last Active Jun 08 2014 03:29 AM

#5146478 JTextField Overrides KeyListener

Posted by on 12 April 2014 - 02:42 AM

If you only want to know when the enter key is pressed, you can use an action listener instead.

There is a good exmaple in the Oracle documentation.


In a nut-shell it's:

final JTextField textField = new JTextField(20);


  new ActionListener() {

    public void actionPerformed(final ActionEvent evt) {

      // Enter pressed.

If you do need to capture other key strokes, consider using a DocumentListener.

KeyListeners can be troublesome when the registered component doesn't have focus.

#5092786 Problem setting up GLSurfaceView.Renderer

Posted by on 09 September 2013 - 01:31 PM

I can't see anything wrong with your OpenGL setup.

I even tried running your sample code on my phone, and it works okay.


Do you get any runtime errors?

Does the device support OpenGl ES 2? Maybe try ES 1 and see if it works.


Are you using any other threads? It's possible the flow of execution is stuck some where else in the code.

#5019994 error trying to setup slick2d with eclipse

Posted by on 10 January 2013 - 02:24 PM

Are you using 64bit version of Eclipse & Java VM?


It looks like Slick2D is trying to load 32bit native dll's.


LWJGL does provide 64bit builds; but you will probably need to modify your Slick2D build to load them.

#4973317 method undefined by arraylist

Posted by on 25 August 2012 - 01:50 PM

You can use "List.get(...)", to return the element at a given index in the ArrayList.

[source lang="java"] for(int i = 0; i < BRICKS.size(); i++) { BRICKS.get(i).paintBrick(g); }[/source]

Generic collections can also take advantage of the For-Each loop.

[source lang="java"] for (final Brick b : BRICKS) { b.paintBrick(g); }[/source]

#4970500 setting up swingworker

Posted by on 17 August 2012 - 04:32 AM

First thing is to fix the generic parameters used by the SwingWorker.
It should be SwingWorker< "Result Type", "Publish Type" >

"Result Type" is the class of the final result we want from running this task.
"Publish Type" is the class encapsulating data we're going to send to GUI components.

In this case, the result we want is convertedFile; which is a File. ("Result Type").
The data we want to send to the UI (JTextArea) is counter, which is an int. ("Publish Type).
NB: Java Generics can't use primitive types directly, instead must use their equivalent wrapper class.

class FileProcessor extends SwingWorker<File, Integer>

It keeps telling me INPUT isn't recognized in the FileProcessor class.

In the constructor of FileProcessor, you need to store the object reference to "INPUT" as an
instance variable to be used later by "doInBackground()".
See next answer Posted Image

How would I get counter in FileProcessor to constantly update to outerTextArea in class X?

This can be done using "publish/process" methods of SwingWorker.
First you also need to store an object reference to the JTextArea.

The complete constructor will look something like this:
private File INPUT;

private JTextArea innerTextArea;

FileProcessor(File INPUT, JTextArea innerTextArea) {

    this.INPUT = INPUT;

    this.innerTextArea = innerTextArea;

Next, whenever counter is changed; you pass it's new value to the publish method.


    //processing stuff


    publish( counter ); // Tell the GUI about counter.

Finally, override the "process" method which receives the new counter values.
Notice that process gets a list of all pending values; rather than been invoked once per publish.

protected void process(List<Interger> chunks) {

    for (Integer i : chunks) {
	    innerTextArea.append("counter = " + i + "\n");

How do I get convertedFile in FileProcessor to be selectedFileHolder in Class X?

There are at least 3 differrent ways todo this. The best way depends on what you want to do with "convertedFile".

1. The simplest is to use "get" method of SwingWorker.
But this will block the current thread until the SwingWorker is finished. (You probably don't want that!).

FileProcessor task = new FileProcessor(selectedFile, outerTextArea);


selectedFileHolder = task.get();

2. You can make FileProcessor an inner class of X, override the "done()" method of SwingWorker.

protected void done() {
    selectedFileHolder = get();

3. You can add a "PropertyChangeListener" to the SwingWorker, and wait to be notified it's finished.

final FileProcessor task = new FileProcessor(selectedFile, outerTextArea);

    new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent evt) {

            if (task.getState() == StateValue.DONE) {

                selectedFileHolder = task.get();


#4823318 [java] Java deployment to MS Windows

Posted by on 14 June 2011 - 01:02 PM

I know that Minecraft has been able to convert his whole project to an EXE binary, but I'm not real sure how he managed to do that, any ideas?

There are compilers available that can directly produce machine code: Example

Any other ideas as to how to get a non-tech-savy person to be able to double-click a Java application to run it?

For smaller applications I'd reccommend Java WebStart

#4766348 Using Graphics2D for all sprites.

Posted by on 28 January 2011 - 02:56 PM

That's the right idea, you just pre-render the sprite to an off screen buffer.

When you want to display the sprite, you simply copy it to the back buffer.

This is a basic example of how to do it:

// Setup sprites.
final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

final GraphicsDevice device = env.getDefaultScreenDevice();

final GraphicsConfiguration gc = device.getDefaultConfiguration();

final BufferedImage spriteBI = gc.createCompatibleImage(
	100,				// Width
	100,				// Height
	Transparency.TRANSLUCENT	// Transparency mode

final Graphics2D g2dSprite = (Graphics2D) spriteBI.getGraphics();

g2dSprite.fillRect(0, 0, 100, 100);

// ...

Then for you main render method.

public void render(final Graphics2D g2d) {

	g2d.drawImage(	spriteBI,	// Buffer
			0,		// x coordinate
			0,		// y coordinate
			null		// Not used. We know image is loaded.