1

Here is what I am trying to do: I have a swing gui with two JFrames. The first has a JCheckBox and the second displays some text. Also the second has a javax.swing.Timer that is waiting for the checkbox in the first frame to be clicked. Once it is clicked, some more text is to be displayed. It works if I have only one condition (click the checkbox) and the condition is directly in the if-statement, like this:

javax.swing.Timer timer = new javax.swing.Timer(100, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if ( otherGUI.jCheckBox.isSelected() ){
            //add some text to second JFrame
            timer.stop();
        }
    }
});

Now for the twist: This should happen not only once, but multiple times. There is an ArrayList of timers, each with its own text and condition, one starting after the other. My problem is: If I store the conditions as strings in an ArrayList, they seem to be evaluated once at the start of the programme, so the condition from above stays false, even when I click the checkbox. Here is my actual code:

    SomeGUI gui = new SomeGUI();

    ArrayList<javax.swing.Timer> timer = new ArrayList<javax.swing.Timer>();
    ArrayList<String> text = new ArrayList<String>();
    ArrayList<String> cond = new ArrayList<String>();

    text.add("some text");
    cond.add("gui.jCheckBox.isSelected()");

    text.add("some more text");
    cond.add(new Condition("true"));

    //etc.

    for ( int i = 0; i < text.size() - 1; i++  ){
        int j = i;//not sure why this trick is necessary. i doesn't work later on
        timer.add( new javax.swing.Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bool = false;
                try{
                    bool = Boolean.parseBoolean( cond.get(j) );
                }
                catch(Exception ex){}
                if ( bool ){
                    addText(p, text.get(j+1));
                    timer.get(j).stop();
                    timer.get(j+1).start();
                }
            }
        }));
    }
    timer.get(0).start();

I already tried an ArrayList<Boolean> for the conditions to the same effect. The code above just represents my present state of trial and error. I hope that I could make clear what I am trying to achieve. So how can I store boolean expressions in a list/array and have them evaluated in an if-statement again and again at runtime and not only once when the programme is started?

Ludwitch
  • 185
  • 1
  • 1
  • 8
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Mar 27 '15 at 11:41
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Mar 27 '15 at 15:15
  • 1
    *Don’t* use trial and error when programming. Read the [documentation of the methods](http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html#parseBoolean(java.lang.String)) you are about to use to learn what they do. – Holger Mar 27 '15 at 17:04
  • And just in case: let me know if there is anything else I can do to make my answer upvote worthy in your eyes. But no hurry - I hit the daily limit already today so upvotes no longer count... – GhostCat Aug 09 '17 at 17:59

1 Answers1

0

There is no simple "evaluation" of strings in Java. It is possible, but really not "java style". See Convert String to Code

One other option would be that your strings represent method names (which exist on a well known object); then you could use reflection to invoke that method based on the provided string. But also, pretty ugly.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248