-2

Yesterday I asked a question because I was making a small basic app that would open a jFrame with a text field and two buttons. If you wrote something and then clicked on the button, another jFrame would appear with a jPanel inside, that would turn green if you wrote a zero, or black with any other result. I didn't know how to make it work. I've finally gotten it to work properly, but it only does it the first time. The thing is, this is what happens when you press the button:

    Taller2.opcion = jTextField2.getText();
    Panel p;
    p = new Panel();
    if (Taller2.panelabierto == false) {
        p.setVisible(true);
        Taller2.panelabierto = true;
    }
    else {
            // No hacer nada
    }

It turns the Taller2.opcion String variable into what's written in the text field and opens the second jFrame. If it's already open, then it will just take the text. Then, on the other jFrame, I wrote this:

    if ("0".equals(Taller2.opcion)) {
        jPanel1.setBackground(java.awt.Color.GREEN);
    }
    else {
        jPanel1.setBackground(java.awt.Color.BLACK);
    }

Which will basically turn the jPanel green if you wrote a zero. The problem is that it will only work the first time you press it. If you write another number and press the button again, it will stay the same color. I tried using a "while (true)" loop around that if-else statement, but whenever I ran it and pressed the button, the program froze. How could I fix this? I'm fairly new at Java, so I'd appreciate rather simple answers. I made both jFrames with the Netbeans designer, so I don't really know the code behind them, only what I added to change the colors and the buttons. Thanks!

EDIT: This is the only part of code I haven't added, which is the main class:

public class Taller2 {

    /**
     * @param args the command line arguments
     */
    public static String opcion;
    public static boolean panelabierto;
    public static void main(String[] args) {
        Pregunta a = new Pregunta();
        a.setVisible(true);
        opcion = null;
        panelabierto = false;

the rest has been generated by the Netbeans designer.

  • "I tried using a "while (true)" loop" >> add the following in your while(true) loop : try{Thread.sleep(100);}catch(InterruptedException e){}; Essentially pauase for 100ms in each loop. – Alan Hay May 28 '15 at 13:46
  • The freeze problem may come from the fact that your while take all the thread operation. Have you tried to debug to see if you entered you action code (after button click) again? We may need more code of how you handle the event on your button – RPresle May 28 '15 at 13:56
  • @AlanHay I just tried adding that try-catch and it made no difference. Still crashes when I press the button. – MightyGreenPanda May 28 '15 at 13:56
  • 1
    As you were [advised yesterday](http://stackoverflow.com/q/30476211/418556), post an MCVE. Not uncompilable code snippets, *certainly not* a link to a 'zip of my code'. Oh, and why are you still using multiple frames? – Andrew Thompson May 28 '15 at 14:34
  • What crash? Not behaving as expected isn't a crash. Unclear what you're asking. – user207421 Oct 22 '16 at 20:37

3 Answers3

2

Couple of issues with your code, I think you should fix them.

  1. As per not reflecting the color change, Your color change code is there in the another JFrame Class and you are not invoking that on the click event. You should fix the callback of the button, You can try calling that jPanel1.setBackground( in the button callback function.
  2. Also, You shouldn't be initializing p = new Panel() every-time you click the button, so you should move that inside if loop (or better to do it in constructor, and in "if loop" only set visibility .
  3. You are running while busy Loop on Main Thread, so it is freezing the UI.
  4. You should maintain the state of application with class instance instead of static variables.
Pratap Koritala
  • 1,557
  • 14
  • 16
  • I tried moving the jPanel1.setbackground to the button, but it didn't change anything, it still will only work once. Also, how could I fix the other two problems? As I said, I'm fairly new to Java and I don't understand that "running while busy loop" thing and the state of application. – MightyGreenPanda May 28 '15 at 14:05
  • I added the only part of code that was left. As I said, I used the Netbeans designer to make the jFrames, so the rest of the code was made automatically by the program. What's on my post is everything I've written so far. – MightyGreenPanda May 28 '15 at 14:15
  • 1) Can you include complete method of button callback including method signature. 2) Include the code of jPanel1 class – Pratap Koritala May 28 '15 at 14:19
0

What you want to realize is to handle every single keypress. This is done in an ActionEventListener. See this question for details: JFrame ActionListener

Community
  • 1
  • 1
sinclair
  • 2,812
  • 4
  • 24
  • 53
0

Hey I think this should work. Tested on my end and it worked just fine. I created my panels differently but the only part you should need to worry about is the logic. Basically I'm creating the First frame with the button and textfield in it by calling a method that builds the panel. Then we create our ActionListener for the button presses. Now here is the logic.

IF frame is already visible check if Value = 0 if it does set OPEN to green (the panel in the new frame) If not set it to black.

IF the frame is not open create one!

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class JavaApplication2 extends JFrame {
private JButton btnSubmit;
private JTextField jtf;
private JPanel mainPanel, current, open;
private JFrame  frame;
private String value;
public JavaApplication2()
{
    super("Panel colors");
    mainPanel = new JPanel();
    mainPanel.add(configureFirst());
    setSize(250,240);
    add(mainPanel);
    setVisible(true);
    setLocationRelativeTo(null);
    frame = new JFrame();
    open = new JPanel();
    btnSubmit.addActionListener((ActionEvent e) -> {
       value = jtf.getText();
       if(frame.isVisible())
       {
           if(value.equals("0"))
           {

                open.setBackground(Color.GREEN);
           }
           else
           {
                open.setBackground(Color.BLACK);
           }
       }
       else
       {
           frame.setSize(200,200);
           frame.setVisible(true);
           frame.add(open);
           if(value.equals("0"))
           {

                open.setBackground(Color.GREEN);
           }
           else
           {
                open.setBackground(Color.BLACK);
           }
       }

    });
}


private JPanel configureFirst()
{
    current = new JPanel(new GridLayout(2,1));
    jtf = new JTextField(12);
    btnSubmit = new JButton("Submit");
    current.add(jtf);
    current.add(btnSubmit);
    return current;
}

public static void main(String[] args)
{
    JavaApplication2 jp = new JavaApplication2();
}
}
basic
  • 3,348
  • 3
  • 21
  • 36