Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default How do I modify a specific component within a active internalframe?

    Hello everybody,

    I am still learning java at this point, but I seem to have surpassed the 3 java books I own.

    I have an issue here that I am confident that most of you can answer.

    I am attempting to change the icon of a button in a internalframe when pressed. The code below shows the text of a button changing (but only the last iframe is being changed, not the active iframe). By answering this question I will understand how to interact with internal frames.
    Here is a stripped down version of the code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.OverlayLayout;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.UIManager;
    import javax.swing.event.InternalFrameEvent;
    import javax.swing.event.InternalFrameListener;
    
    public class TestFrameButton extends JDesktopPane{
    
    	private static final long serialVersionUID = 1L;
    	JDesktopPane desk;
    	JDesktopPane ifdesk;
    	JScrollPane scrollpane;
    	JInternalFrame iframe;
    	JFrame frame;
    	JList jList1;
    	JInternalFrame currentframe;
    	Integer currentframenumber;
    	String currentframename;
    	Boolean[] downuptracker;
    	Integer deskwidth = 1000;
    	Integer deskheight = 1000;
    	Integer scrollwidth = 1000;
    	Integer scrollheight = 1000;
    	JButton DownUpButton;
    
    	public static void main(String[] args) {
    		TestFrameButton d = new TestFrameButton();
    	}
    	public TestFrameButton(){
    		downuptracker = new Boolean [99];
    		frame = new JFrame("Test Frame Button");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		scrollpane = new JScrollPane(desk,
    				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    		scrollpane.setPreferredSize(new java.awt.Dimension(scrollwidth, scrollheight));
    		desk = new JDesktopPane();
    		desk.setPreferredSize(new java.awt.Dimension(deskwidth, deskheight));
    		int i = 5;
    		for (int j = 0; j <= i; j++){
    			UIManager.getDefaults().put("InternalFrame.icon", "");
    			iframe = new JInternalFrame("Internal Frame: " + j, false, true, false, false);
    			iframe.setName(String.valueOf(j));
    			iframe.setBounds(30*j, 30*j,265 , 80);
    			iframe.addInternalFrameListener(new InternalFrameListener(){
    				public void internalFrameClosing(InternalFrameEvent e) {
    				}
    				public void internalFrameClosed(InternalFrameEvent e) {
    				}
    				public void internalFrameOpened(InternalFrameEvent e) {
    				}
    				public void internalFrameIconified(InternalFrameEvent e) {
    				}
    				public void internalFrameDeiconified(InternalFrameEvent e) {
    				}
    				public void internalFrameActivated(InternalFrameEvent e) {
    					currentframe = e.getInternalFrame();
    					currentframename = e.getInternalFrame().getName();
    					currentframenumber = Integer.valueOf(currentframename);
    				}
    				public void internalFrameDeactivated(InternalFrameEvent e) {
    				}
    			});
    			iframe.setTitle("Internal Frame :" + j);
    			iframe.setVisible(true);
    			downuptracker[j] = true;
    			ifdesk = new JDesktopPane();
    			iframe.getContentPane().add(ifdesk, BorderLayout.CENTER);
    			DownUpButton = new JButton("Old Icon here");
    			ifdesk.add(DownUpButton);
    			DownUpButton.setBounds(0, 0, 130, 20);
    			DownUpButton.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					if (downuptracker[currentframenumber].equals(true)){
    						// Colapse frame here and change icon
    						DownUpButton.setText("New Icon here");
    						downuptracker[currentframenumber] = false;
    
    					}else{
    						// Expand frame here and change icon
    						DownUpButton.setText("Old Icon here");
    						downuptracker[currentframenumber] = true;
    					}
    				}
    			});             
    			desk.add(iframe);
    			iframe.moveToFront();
    		}
    		scrollpane.setViewportView(desk);
    		frame.add(scrollpane);
    		scrollpane.setVisible(true);
    		frame.setSize(800,600);
    		frame.setVisible(true);
    	}
    }
    Thank you in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class TFB {
        private static final long serialVersionUID = 1L;
        JDesktopPane desk;
        JDesktopPane ifdesk;
        JScrollPane scrollpane;
        JInternalFrame iframe;
        JFrame frame;
        JList jList1;
        JInternalFrame currentframe;
        Integer currentframenumber;
        String currentframename;
        Boolean[] downuptracker;
        Integer deskwidth = 1000;
        Integer deskheight = 1000;
        Integer scrollwidth = 1000;
        Integer scrollheight = 1000;
        JButton DownUpButton;
    
        public static void main(String[] args) {
            new TFB();
        }
    
        public TFB(){
            downuptracker = new Boolean [99];
            frame = new JFrame("Test Frame Button");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            scrollpane = new JScrollPane(desk,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollpane.setPreferredSize(new Dimension(scrollwidth, scrollheight));
            // If you're going to make a new JDesktopPane here there is
            // no need to have the enclosing class extend JDesktopPane.
            desk = new JDesktopPane();
            desk.setPreferredSize(new Dimension(deskwidth, deskheight));
            // Only need to do this one time.
            UIManager.getDefaults().put("InternalFrame.icon", "");
            int i = 5;
            for (int j = 0; j <= i; j++){
    //            UIManager.getDefaults().put("InternalFrame.icon", "");
                iframe = new JInternalFrame("Internal Frame: " + j,                   
    
                           false, true, false, false);
                iframe.setName(String.valueOf(j));
                iframe.setBounds(30*j, 30*j,265 , 80);
                iframe.addInternalFrameListener(new InternalFrameListener(){
                    public void internalFrameClosing(InternalFrameEvent e) {}
                    public void internalFrameClosed(InternalFrameEvent e) {}
                    public void internalFrameOpened(InternalFrameEvent e) {}
                    public void internalFrameIconified(InternalFrameEvent e) {}
                    public void internalFrameDeiconified(InternalFrameEvent e) {}
    
                    public void internalFrameActivated(InternalFrameEvent e) {
                        currentframe = e.getInternalFrame();
                        currentframename = e.getInternalFrame().getName();
                        currentframenumber = Integer.valueOf(currentframename);
                        System.out.println("activatedFrame name = " +
                                            currentframename);
                    }
                    public void internalFrameDeactivated(InternalFrameEvent e) {}
                });
                iframe.setTitle("Internal Frame :" + j);
                iframe.setVisible(true);
                downuptracker[j] = true;
    //            ifdesk = new JDesktopPane();
    //            iframe.getContentPane().add(ifdesk, BorderLayout.CENTER);
                DownUpButton = new JButton("Old Icon here");
    //            ifdesk.add(DownUpButton);
                iframe.setLayout(null);
                DownUpButton.setBounds(0, 0, 130, 20);
                iframe.getContentPane().add(DownUpButton);
                DownUpButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        // Try getting a reference to the button that
                        // sent this event:
                        JButton button = (JButton)e.getSource();
                        if (downuptracker[currentframenumber].equals(true)) {
                            // Colapse frame here and change icon
                            // Since DownUpButton has been declared as a
                            // member variable in class scope this reference
                            // to "DownUpButton" will always refer/point to
                            // the last instance created.
                            //DownUpButton.setText("New Icon here");
                            button.setText("New Icon here");
                            downuptracker[currentframenumber] = false;
                        }else{
                            // Expand frame here and change icon
                            //DownUpButton.setText("Old Icon here");
                            button.setText("Old Icon here");
                            downuptracker[currentframenumber] = true;
                        }
                    }
                });
                desk.add(iframe);
                iframe.moveToFront();
            }
            scrollpane.setViewportView(desk);
            frame.add(scrollpane);
    //        scrollpane.setVisible(true);
            frame.setSize(800,600);
            frame.setVisible(true);
        }
    }

Similar Threads

  1. Modify A* Algorithm
    By prakharbirla in forum Advanced Java
    Replies: 1
    Last Post: 02-13-2008, 06:25 PM
  2. Modify/create AVI's INFO BLOCK
    By Agus211 in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 03:20 PM
  3. Active Content enable problem
    By kasipandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-01-2008, 02:15 PM
  4. Accessing Active Directory
    By javaplus in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2008, 11:25 PM
  5. Rejecting connections from a specific IP
    By javaplus in forum Networking
    Replies: 2
    Last Post: 12-21-2007, 02:28 PM

Posting Permissions

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