1

I have a java app to open the file in separate windows, but the menu bar and frame title doesn’t show on the second windows. As you can see when the file is open on the first windows that the title and menu bar appear. When I open the file by clicking the menu on the first windows. The file opens on the second windows but no frame title and menu bar. Would someone tell me how I can solve this problem? Thank in advance.

The image is the first file opening on the windowsenter image description here

The image is the second file opening on the separate windowenter image description here

There is my code:

package PDFAnnotationPackage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.*;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextField;
import PDFAnnotationPackage.WindowMenu.ChildMenuItem;
import com.qoppa.pdf.IEmbeddedFile;
import com.qoppa.pdf.annotations.IAnnotSelectionListener;
import java.io.File;



public class Question extends JFrame implements ActionListener{

public final static  String PDFEXTENSION= "pdf";
private ArrayList<File> fileList=new  ArrayList<File>();
private PDFInternalFrame  internalFrame=null;
private ArrayList<PDFInternalFrame>  lstInternalFrame=new ArrayList<PDFInternalFrame>();


public static void main(String[] args) {
    // TODO Auto-generated method stub
    if (args.length>0){
        Utility.DisplayTestMsg(args[0]);

    }

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run()
        {               
            new Question();              
        }
    });

}

public Question(File  newFile) {

    String extension=getExtension(newFile);
     if ( PDFEXTENSION.equalsIgnoreCase(extension)){
          AddJPDFNote(newFile);
     }   else{
                     Utility.DisplayWarningMsg("Only PDF File");
           }


    // TODO Auto-generated constructor stub
}
public  Question(){
    super("Question");

            //it is equal to this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {                      
                }
            });


           this.setMinimumSize(new Dimension(400, 500));        
           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
           this.setExtendedState(JFrame.MAXIMIZED_BOTH);            
           this.setLayout(new BorderLayout());


            // Name the JMenu & Add Items
            JMenu menu = new JMenu("File");
            menu.add(makeMenuItem("Open"));
            menu.add(makeMenuItem("Save"));
            menu.add(makeMenuItem("Save As"));
            menu.add(makeMenuItem("Close"));
            menu.add(makeMenuItem("Print"));
            menu.add(makeMenuItem("Quit"));


            // Add JMenu bar
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);            
            setJMenuBar(menuBar);   
            setVisible(true);          

}
 public void actionPerformed(ActionEvent e) {

        // Menu item actions
        String command = e.getActionCommand().trim();  

        if (command.length()>1){
            menuAction(command);
        } 


    }
  private String getExtension(File file){
       return      file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
   }

private JMenuItem makeMenuItem(String name) {
    JMenuItem m = new JMenuItem(name);
    m.addActionListener(this);
    return m;
}

private void menuAction(String command){

        boolean blnGo=false;

             if (command.equals("Quit")) {

             } else if (command.equals("Open")) {
                 // Open menu item action           
                 OpenPDFFile();

             }  else if (command.equalsIgnoreCase("Save")) {


             } else if (command.equalsIgnoreCase("Save As")) {


             }else if (command.equalsIgnoreCase("Close")){

                 }

             else if (command.equalsIgnoreCase("Print")) {
             }

    }      



private void AddJPDFNote(File file){                
    try{                             

         internalFrame = new PDFInternalFrame(file, this.getWidth(), this.getHeight());
         fileList.add(file);
         lstInternalFrame.add(internalFrame);               
         internalFrame.setBounds(0, 0, 600, 100);       
         this.add(internalFrame,BorderLayout.CENTER);
         internalFrame.setVisible(true);

      try {

           internalFrame.setSelected(true);
       }
       catch (java.beans.PropertyVetoException e) {              
           Utility.DisplayExcecptionStrackTrack(e,"MainForm - AddJPDFNote line 401");

       }

       catch(Exception  err){
           Utility.DisplayExcecptionStrackTrack(err,"MainForm - AddJPDFNote line 406");
       }



    }
    catch(Exception  err){
        Utility.DisplayExcecptionStrackTrack(err,"MainForm - AddJPDFNote");


    }
}



private void  OpenPDFFile(){
      JFileChooser fileChooser = new JFileChooser();  
    FileNameExtensionFilter  pdfType=new FileNameExtensionFilter("PDF File (."+ PDFEXTENSION + ")", PDFEXTENSION);
    fileChooser.addChoosableFileFilter(pdfType);
    fileChooser.setFileFilter(pdfType);
  //clear "All files" from dropdown filter box
    fileChooser.setAcceptAllFileFilterUsed(false);

    int returnVal = fileChooser.showOpenDialog(Question.this);
    if (returnVal ==  fileChooser.APPROVE_OPTION) {

        File file = fileChooser.getSelectedFile();           
        String extension =getExtension(file);

        if ( PDFEXTENSION.equalsIgnoreCase(extension)){
          if (fileList.size()==0){

                      AddJPDFNote(file);
                  }
                  else{
                      Question a=new Question(file);
                      a.pack();
                      a.setVisible(true);
                  }
            } else{
             Utility.DisplayWarningMsg("Only PDF File");
            }

    } else if (returnVal == JFileChooser.CANCEL_OPTION ) {
    // Do something else

    } 
  } 




}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user819774
  • 1,456
  • 1
  • 19
  • 48

2 Answers2

1

It seems that you're calling the constructor that takes a File as an argument...

Question a=new Question(file);

But nowhere in that constructor do you set the title of the frame or set its menu bar.

TNT
  • 2,900
  • 3
  • 23
  • 34
0

On button action listner you are calling

Question a=new Question(file);

but Jmenu items is written on constructor with no argument.so change your construtors as below and try,it worked for me

    public Question(File  newFile) {

    String extension=getExtension(newFile);
    if ( PDFEXTENSION.equalsIgnoreCase(extension)){
        AddJPDFNote(newFile);
    }   else{
       // Utility.DisplayWarningMsg("Only PDF File");
    }
    //it is equal to this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
        }
    });


    this.setMinimumSize(new Dimension(400, 500));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setLayout(new BorderLayout());


    // Name the JMenu & Add Items
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Save As"));
    menu.add(makeMenuItem("Close"));
    menu.add(makeMenuItem("Print"));
    menu.add(makeMenuItem("Quit"));


    // Add JMenu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    setJMenuBar(menuBar);
    setVisible(true);

    // TODO Auto-generated constructor stub
}
public  Question(){
    //super("Question");
    this(new File(""));
} 

enter image description here

VaibJ
  • 84
  • 5