Call the method on my customer JPanel for changing label for all JInternalFrames in JFrame

StackOverflow https://stackoverflow.com/questions/22645453

  •  21-06-2023
  •  | 
  •  

質問

I have MDI project. I add the customer JPanel in the JInternalFrame. My customer JPanel has a public method to change some component background color. When the button on the JFrame is clicked, I want it to change the label or text on my customer JPanel for all InternalFrame. How can I call the method? Thanks in advance

The following code is the action for the button on JFrame

 private void categoryAction(ActionEvent e){
    try{
        JButton b=(JButton)e.getSource();
        Color c=b.getBackground();
         JInternalFrame[] allframes = desktop.getAllFrames();
         int count = allframes.length;
         for (int i=0; i<allframes.length-1; i++){
            //call the method on the PDFJPanel
         }

    }
    catch(Exception  err){
        Utility.DisplayErrorMsg(err.toString());

    }

The following code is add the internal frame into the desktop

private void AddNote(File file){        
        JInternalFrame internalFrame = new     JInternalFrame("PDFAnnotation"
                + file.getName(), true, true, true, true);      
       internalFrame.setBounds(0, 0, 600, 100);
       desktop.add(internalFrame);    


       PDFJPanel p=new PDFJPanel(file);       
       internalFrame.add(p, BorderLayout.CENTER);
       internalFrame.setVisible(true);
try {
           internalFrame.setSelected(true);
       }
       catch (java.beans.PropertyVetoException e) {}
       this.add(desktop, BorderLayout.CENTER);

      //resize the internal frame as full screen  
      Dimension size = desktop.getSize();
      int w = size.width ; 
      int h = size.height ; 
      int x=0;
      int y=0;
      desktop.getDesktopManager().resizeFrame(internalFrame, x, y, w, h);
    }

There is the method on my customer JPanel

Public void setDefaultColor(Color c){
      //change the label and textbox color
 }
役に立ちましたか?

解決

You can utilize JDesktopPane.getSelectedFrame that returns currently active frame. You can retrieve PDFJPanel from the layout manager, ie using BorderLayout.getLayoutComponent(). Or easier and cleaner, you can extend JInternalFrame, ie:

class PDFFrame extends JInternalFrame {
    private PDFJPanel panel;

    public PDFFrame(File file) {
        panel = new PDFJPanel(file); 
        add(panel, BorderLayout.CENTER);
    }

    public void setDefaultColor(Color c){
        panel.setDefaultColor();
    }
}

Then, access it:

JDesktopPane desktop = ...;

PDFFrame frame = (PDFFrame) desktop.getSelectedFrame();
frame.setDefaultColor(Color.BLUE);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top