Carl Leatherbarrow
Carl Leatherbarrow

Reputation: 103

How to add an JInternalFrame to a JFrame

What I would like to do is open a JInternalFrame (or something that will work) as a child of the main JFrame. I have event listener within the TopButtons class, all the buttons work and what i want to do is open a JInternalFrame from there. This is my code.

public class MainWindow extends JFrame {

private static final long serialVersionUID = 1L;


//Constructor for the main window 
public MainWindow(){

    this.setLayout(new BorderLayout(5,5) );

    //Loads the buttons for the top of the window.
    TopButtons topPanel = new TopButtons();
    this.add(topPanel,BorderLayout.NORTH);


}//End of the Constructor




//Calls the constructor of the main class
public static void main(String[] args) {
    MainWindow window = new MainWindow();

    window.setSize(new Dimension(1000,1000));
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

}

This is the top buttons class.

public class TopButtons extends JPanel {

private JPanel panelLeft;
private JPanel panelRight;


private static final long serialVersionUID = 1L;

/**
 * Constructor creates the top panel.
 * @param frame
 */
public TopButtons(){
    createTopPanel();

}

//Creates the panel for the top of the window
    private void createTopPanel() {
        //Sets a grid layout for the top panel
        setLayout(new GridLayout());

        //Sets and right panels to be put inside the classes panel.
        panelLeft = new JPanel();
        panelRight = new JPanel();

        //Sets the layouts for the left and right panels.
        panelLeft.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
        panelRight.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));

        //Adds buttons to the left panel
        panelLeft.add(addButton("New Sale", "Icons/newSale.png"));
        panelLeft.add(addButton("Orders", "Icons/orders.png"));
        panelLeft.add(addButton("Products Search", "Icons/products.png"));
        panelLeft.add(addButton("Edit Products", "Icons/editProducts.png"));
        panelLeft.add(addButton("Customer Search", "Icons/customers.png"));
        panelLeft.add(addButton("Stock Levels", "Icons/stock.png"));

        //Adds buttons to the right panel
        panelRight.add(addButton("Logout", "Icons/logout.png"));

        //Adds the two panels to the grid layout
        add(panelLeft);
        add(panelRight);

    }//End of top panel


/*Creates a button and adds an image icon.
 *  
 */
private JButton addButton(String name, String imgPath){
    Image img = null;
    Image icon = null;      
    JButton button = new JButton();

    button.setActionCommand(name);
    button.setToolTipText(name);        

    //Gets the image from the image class.
    try {
        img = Images.getImage(imgPath);
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    }

    //Scales the image to the size that is best for appearance.
    icon = img.getScaledInstance(40, 40, java.awt.Image.SCALE_SMOOTH ) ;

    //Adds icon to button and removes the buttons margins.
    button.setIcon(new ImageIcon(icon));
    button.setMargin(new Insets(0,0,0,0));

    //Listener for the button.
    button.addActionListener(new Listener());


    return button;
}


/**
 * Private listener class to handle events for the panels. 
 * @author Carl
 *
 */
private class Listener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {

        //Checks the action command to see which button is pressed.

        if(((JButton)e.getSource()).getActionCommand().equals("New Sale")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Customer Search")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Products Search")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Orders")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Stock Levels")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Logout")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Edit Products")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
    }



}//End of private class.

}//End of Class

And this is the EditProducts no code as dont know how to add it as a child JInternalFrame.

public class EditProducts extends  JInternalFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;


public EditProducts(){

    this.add(new JLabel("hello"));
}

}

What I want the app to look like

Upvotes: 0

Views: 546

Answers (1)

user5364144
user5364144

Reputation:

Hello Carl Leatherbarrow!

I really don't know what do you mean, but if you want to create a JInternalFrame by clicking on the button, you must create JDesktopPane.

In official Java documentation is explained as follows: Example code:

...//In the constructor of InternalFrameDemo, a JFrame subclass:
    desktop = new JDesktopPane();
    createFrame(); //Create first window
    setContentPane(desktop);
    ...
    //Make dragging a little faster but perhaps uglier.
    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
...
protected void createFrame() {
    MyInternalFrame frame = new MyInternalFrame();
    frame.setVisible(true);
    desktop.add(frame);
    try {
        frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {}
}

...//In the constructor of MyInternalFrame, a JInternalFrame subclass:
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;

public MyInternalFrame() {
    super("Document #" + (++openFrameCount),
          true, //resizable
          true, //closable
          true, //maximizable
          true);//iconifiable
    //...Create the GUI and put it in the window...
    //...Then set the window size or call pack...
    ...
    //Set the window's location.
    setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}

So in your code you can implement it like this:

private class Listener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {

        //Checks the action command to see which button is pressed.

        if(((JButton)e.getSource()).getActionCommand().equals("New Sale")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        //Open the JInternalFrame
        MyInternalFrame frame = new MyInternalFrame();
            frame.setVisible(true);
            desktop.add(frame);
            try {
            frame.setSelected(true);
            } catch (java.beans.PropertyVetoException e) {}
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Customer Search")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Products Search")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Orders")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Stock Levels")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Logout")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
        if(((JButton)e.getSource()).getActionCommand().equals("Edit Products")){
            //TODO Program button.
            System.out.println(((JButton) e.getSource()).getActionCommand());
        }
    }

More about it you can read here.

Upvotes: 1

Related Questions