2

I made some menu and it is to update conmmon variables (for text on grid) then the out-of-focus dialog must repaint the grid. Here is the screenshot:

enter image description here

The main control panel is always at top position and 'Data Display' panel is always sitting behind it. When press a button on front panel, Data Display must update its grid. Currently, the common variable 0.4 on the grid is updated by adding listener and works fine. But the grid itself is not repainting anymore. How can I repaint the out-of-focus dialog in real time?

Here is the code of the front panel:

public class MainDisplayForm extends javax.swing.JFrame {

Storage st = new Storage();
DisplayForm dF = new DisplayForm();
....
public MainDisplayForm() {
    initComponents();
    Btn_IncreaseGain.addActionListener(new ButtonListener_IncreaseGain());
}
....
} //MainDisplayForm ends here.

class ButtonListener_IncreaseGain implements ActionListener {

DisplayForm dF = new DisplayForm();
Storage st = new Storage();

ButtonListener_IncreaseGain()
{

}

public void actionPerformed(ActionEvent e) {    

    st.iGain = 20;

    dF.revalidate();
    dF.repaint();
    System.out.println("Testing"); 
    }
}//Listener ends here.

Here is code of Data Display:

public void paint(Graphics g)
{        
    g2 = (Graphics2D) g;
    paintComponents(g2);

    //added numbers are for adjustment.
    int x = this.jPanel1.getX()+8;
    int y = this.jPanel1.getY()+30;
    int width = this.jPanel1.getWidth()+19;
    int height = this.jPanel1.getHeight()+40;


    //labelling voltages
    label0.setText(st.zero);
    label1.setText(st.v1);
    label2.setText(st.v2);
    label3.setText(st.v3);
    label4.setText(st.v4);
    label5.setText(st.v3);
    label6.setText(st.v4);


    g2.setColor(Color.darkGray);

    for(int i=x; i<width; i=i+80)
    {      
        g2.drawLine(i, y, i, height);          
    }              

    int j = 0;
    for(int i=y; i<height; i=i+80)
    {   
        j++;
        //st.iGain
        g2.setColor(Color.orange);
        if(j==1)
        {
           double k1 = st.iGain * 0.4;
           st.v1 = Double.toString(k1);

           g2.drawString(st.v1, x+5, y+10);
        }            

        if(j==2)
        {               
           double k2 = st.iGain * 0.3;
           st.v2 = Double.toString(k2);

           g2.drawString(st.v2, x+5, y+90);
        }
        g2.setColor(Color.DARK_GRAY);
        g2.drawLine(x, i, width, i); 
       ....         

    } //grid info is not completed yet.

Thanks,

user1098761
  • 579
  • 1
  • 5
  • 16

1 Answers1

3

Focus isn't the issue and has nothing to do with your current problem. The solution is to change the properties of the data grid by updating fields it contains via setter methods and calling repaint on the JComponent (perhaps a JPanel, or some other component that derives ultimately from JComponent) held by the data grid. The paintComponent method of this component should use its class fields to update what it draws.

You almost never paint in the paint method of a JComponent and certainly you don't want to draw directly into a top-level window. You also probably don't want to set text of JLabels, JTextFields, or any other JTextComponent. from within paint/paintComponent.

I can't see why your code is not working and can only guess that the likely cause of your problem is in code not shown.

Edit 1:
Just guessing, but you may have a problem of references. I notice that your listener class creates new DisplayForm and Storage objects:

DisplayForm dF = new DisplayForm();
Storage st = new Storage();

There's a good possibility that these objects are not the ones being displayed, especially if you create these objects elsewhere and display them. Again I'm just guessing since I don't see the rest of your code, but perhaps you should to pass references for these objects into the DisplayForm via constructor or setter method parameters.

Edit 2:
e.g.,

public void setDisplayForm(DisplayForm dF) {
   this.dF = dF;
}

// same for Storage

And in the main program:

public MainDisplayForm() {
    initComponents();
    ButtonListener_IncreaseGain btnListenerIncreaseGain = new ButtonListener_IncreaseGain();
    btnListenerIncreaseGain.setDisplayForm(....);
    btnListenerIncreaseGain.setStorage(....);
    Btn_IncreaseGain.addActionListener(btnListenerIncreaseGain);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373