Results 1 to 7 of 7
  1. #1
    rgallagher is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default JInternalFrame listener problems ?

    Hello,
    I have a small problem with a project i am working on where I have multiple instances of an object opening in separate JInternalFrames.

    The problem is that when i open a second JInternalFrame the first loses its listeners functionality. e.g. when i click on the first windows open file button the action is performed on the second window

    Sorry if this is a trivial question :)

    Thanks for any Help, Ross.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rgallagher View Post
    Hello,
    I have a small problem with a project i am working on where I have multiple instances of an object opening in separate JInternalFrames.

    The problem is that when i open a second JInternalFrame the first loses its listeners functionality. e.g. when i click on the first windows open file button the action is performed on the second window

    Sorry if this is a trivial question :)

    Thanks for any Help, Ross.

    It's not a trivial problem, and about all I can say is that you've got a bug in your code, but I'm not sure we can say more (or how you'd expect us to) without more information/code.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ... and welcome to the Java-Forums, by the way!

  4. #4
    rgallagher is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks :d

    Java Code:
    public class MainViewer
    {
    	private static final int SHORTCUT_MASK = 0;
    	private JFrame frame;
    	private JDesktopPane desktop;
    	private ImagePanel backImage;
    	private OFImage backgroundImage;
    	private JInternalFrame INFrame; 
    
    	public MainViewer()
    	{
    	frame = new JFrame("Image Viewer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JPanel content = (JPanel) frame.getContentPane();
    
        backImage = new ImagePanel();
        backImage.setImage(backgroundImage);
        content.add(backImage);
        
    	desktop = new JDesktopPane();
    	desktop.setVisible(false);
        content.add(desktop, BorderLayout.CENTER);
        
        try {
    		backgroundImage = new OFImage (ImageIO.read( new File( "ImageViewerOpenView.jpg" ) ));
    	} catch (IOException e1) {
    		e1.printStackTrace();
    	}    
        
        			//Create Menu Bar.
    			    JMenuBar menubar = new JMenuBar();
    			    frame.setJMenuBar(menubar);
    			    
    			    JMenu menu;
    			    JMenuItem item;
    			    
    			    // create the File menu
    			    menu = new JMenu("File");
    			    menubar.add(menu);
    			    
    			    //Add Open Menu Item.
    			    item = new JMenuItem("Open...");
    			        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
    			        item.addActionListener(new ActionListener() {
    			                           public void actionPerformed(ActionEvent e) { 
    			                        	   openNewImage();
    			                           }
    			                       });
    			    menu.add(item);
    			    
    			    menu.addSeparator();
    			    
    			    item = new JMenuItem("Quit");
    			    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
    			    item.addActionListener(new ActionListener() {
    			                       public void actionPerformed(ActionEvent e) { 
    			                    	   closeWindow();
    			                       }
    			                   });
    			    menu.add(item);
        
    	
    	//Display the frame.
        frame.pack();
        // place the frame at the center of the screen and show
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
        frame.setVisible(true);
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    	}
    	
    	public void openNewImage()
    	{
    		desktop.setVisible(true);
    		ImageViewer viewer = new ImageViewer();
    		INFrame = viewer.makeFrame();
    		desktop.add(INFrame);
    		frame.setVisible(true);
    	}
    	
    	public void closeWindow()
    	{
    		System.exit(0);
    	}
    }
    This is the main frame set up.

    Java Code:
    public JInternalFrame makeFrame()
    	    {
    	        frame = new JInternalFrame("ImageViewer");
    	        frame.setResizable(true);
    	        frame.setMaximizable(true);
    	        frame.setClosable(true);
    	        JPanel contentPane = (JPanel)frame.getContentPane();
    	        contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
    	       
    	        //Set draw panel
    	        drawPane = new JPanel();
    	        //Set colour panel.
    	        colourPane = new JPanel();
    	        //Set the edit panel.
    	        editPane = new JPanel();
    	        editPane.setLayout(new BorderLayout(6, 6));
    	        makeMenuBar(frame);
    	        
    	        // Specify the layout manager with nice spacing
    	        contentPane.setLayout(new BorderLayout(6, 6));
    	        
    	        // Create the image pane in the center
    	        imagePanel = new ImagePanel();
    	        
    	        //imagePanel.setBorder(new EtchedBorder());
    	        JScrollPane scrollPane = new JScrollPane(imagePanel);
    	        scrollPane.setEnabled(true);
    	        contentPane.add(scrollPane, BorderLayout.CENTER);
    
    	        // Create two labels at top and bottom for the file name and status messages
    	        filenameLabel = new JLabel();
    	        contentPane.add(filenameLabel, BorderLayout.NORTH);
    
    	        statusLabel = new JLabel(VERSION);
    	        contentPane.add(statusLabel, BorderLayout.SOUTH);
    	        
    	        // Create the toolbar with the buttons
    	        JPanel toolbar = new JPanel();
    	        toolbar.setLayout(new GridLayout(0, 1));
    	        
    	        smallerButton = new JButton("Smaller");
    	        smallerButton.addActionListener(new ActionListener() {
    	                               public void actionPerformed(ActionEvent e) { storedImages.add(currentImage);
    	                               												currentImage = manipulate.makeSmaller(currentImage);
    														                        imagePanel.setImage(currentImage); 
    	                               												}
    	                           });
    	        toolbar.add(smallerButton);
    	        
    	        largerButton = new JButton("Larger");
    	        largerButton.addActionListener(new ActionListener() {
    	                               public void actionPerformed(ActionEvent e) { storedImages.add(currentImage);
    	                               												currentImage = manipulate.makeLarger(currentImage);
    														                        imagePanel.setImage(currentImage); 
    	                               												}
    	                           });
    	        toolbar.add(largerButton);
    	        
    	        //Add crop button
    	        cropButton = new JButton("Crop Image");
    	        cropButton.addActionListener(new ActionListener() {
    					               public void actionPerformed(ActionEvent e) {storedImages.add(currentImage);
    					            	   										   currentImage = manipulate.crop(currentImage); 
    					               											   imagePanel.setImage(currentImage);  }
    	        				  });
    	        toolbar.add(cropButton);
    	        
    	        //Add rotate button.
    	        rotateButon = new JButton("Rotate Image");
    	        rotateButon.addActionListener(new ActionListener() {
    						            public void actionPerformed(ActionEvent e) {storedImages.add(currentImage);
    						            											currentImage = manipulate.rotate(currentImage, 90);
    																	            imagePanel.setImage(currentImage); 
    						            										    }
    								  });
    	        toolbar.add(rotateButon);
    	        
    	        drawButton = new JButton("Draw");
    	        drawButton.addActionListener(new ActionListener() {
    	            public void actionPerformed(ActionEvent e) {editPane.setVisible(true);
    	            										    }
    			  });
    	        toolbar.add(drawButton);
    	        
    	        //Create drawing panel.
    	        drawPanel();
    	        colourPanel();
    	        
    	        // Add toolbar into panel with flow layout for spacing
    	        JPanel flow = new JPanel();
    	        flow.add(toolbar);
    	        
    	        drawPane.add(colourPane, BorderLayout.WEST);
    	        
    	        contentPane.add(flow, BorderLayout.WEST);
    	        editPane.add(drawPane, BorderLayout.NORTH);
    	        editPane.add(colourPane, BorderLayout.SOUTH);
    	        contentPane.add(editPane,BorderLayout.SOUTH);
    	        
    	        editPane.setVisible(false);
    	        
    	        // building is done - arrange the components      
    	        showFilename(null);
    	        setButtonsEnabled(false);
    	        frame.pack();
    	        
    	        frame.setVisible(true);
    	        
    			return frame;
    	        
    	        //frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    	    }
    And this is the method it calls to set up the internal Frame.

    I didnt want to post this first time as it is quite long but there it is.

    Thanks for the quick reply :D

    Ross

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rgallagher View Post
    I didnt want to post this first time as it is quite long but there it is.
    That it is, too long and too many unavailable dependencies for my little brain. Are you using any static variables? Which button has the bad behavior? What specifically happens? In your smallerButton (not a helpful name) ActionListener, what is adding currentImage to the private Container storedImages supposed to do since storedImages is only visible inside of this listener and nothing is ever done with it?

    Details would be very helpful and even better would be if you could create and post an SSCCE (please see the link), a small program that shows your problem but that has no outside dependencies and that compiles and runs for us.

  6. #6
    rgallagher is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    The buttons aren't the actual problem per-say it is, I think the focus of the window when using the JInternalFrame, as the listeners used in the ImageViewer part of my code will only work and update the most recently opened JInternalFrame.

    ???

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rgallagher View Post
    The buttons aren't the actual problem per-say it is, I think the focus of the window when using the JInternalFrame, as the listeners used in the ImageViewer part of my code will only work and update the most recently opened JInternalFrame.

    ???
    Please tell us more. When does this happen? What part of the code? Details may matter.

Similar Threads

  1. jInternalFrame
    By kiranjava in forum Advanced Java
    Replies: 1
    Last Post: 05-20-2010, 01:04 PM
  2. How to reload JInternalFrame
    By chyrl in forum AWT / Swing
    Replies: 42
    Last Post: 04-16-2010, 02:29 PM
  3. JInternalFrame Help
    By collin389 in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 01:23 AM
  4. JInternalFrame Help
    By collin389 in forum AWT / Swing
    Replies: 1
    Last Post: 03-15-2010, 10:24 PM
  5. Control JInternalFrame ?
    By h9h in forum AWT / Swing
    Replies: 1
    Last Post: 10-11-2009, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •