Jump to content

  • Log In with Google      Sign In   
  • Create Account

Angex

Member Since 16 May 2004
Offline Last Active Today, 04:30 AM
-----

Posts I've Made

In Topic: validating JTextField data from result set

05 May 2013 - 01:18 PM

In the validateLog() method, the prepared statement only has 2 placeholders, but you're supplying data to indices 2 & 3.

 

The index for placeholders starts from 1 smile.png


In Topic: error trying to setup slick2d with eclipse

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.


In Topic: method undefined by arraylist

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.

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

In Topic: setting up swingworker

18 August 2012 - 03:44 AM

Sorry I didn't check the code compiled properly. Posted Image

Make sure to include an import statement for the PropertyChangeListener

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;


Also SwingWorker.get(), declares some checked exceptions so it needs to be inside a try-catch block.

task.addPropertyChangeListener(

    new PropertyChangeListener() {

        @Override
	    public void propertyChange(PropertyChangeEvent evt) {

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

         	   try {
             	   selectedFileHolder = task.get();
         	   } catch (InterruptedException exp) {
             	   exp.printStackTrace();
                } catch (ExecutionException exp) {
             	   exp.printStackTrace();
         	   }
     	   }
	    }
    }
);

In Topic: setting up swingworker

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.

e.g.
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) {
    //initialize

    this.INPUT = INPUT;

    this.innerTextArea = innerTextArea;
}

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

e.g.
while(somecondition==condition)
{

    //processing stuff

    counter++;

    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.

e.g.
@Override
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!).

e.g.
FileProcessor task = new FileProcessor(selectedFile, outerTextArea);

task.execute();

selectedFileHolder = task.get();


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

e.g.
@Override
protected void done() {
    selectedFileHolder = get();
}


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

e.g.
final FileProcessor task = new FileProcessor(selectedFile, outerTextArea);

task.addPropertyChangeListener(
    new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {

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

                selectedFileHolder = task.get();
            }
  	  }
    }
);

task.execute();

PARTNERS