3

In my project,i am using JFrame as main application window and placed JDesktopPane in this JFrame. I set MigLayout to the JDesktopPane, now the problem is that i have been able to display JInternalFrame on this JDesktopPane but unable to resize this JInternalFrame,is there any way to change the size of JInternalFrame in such situation ? Thank you.

 package Design;

//Header Files
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import net.miginfocom.swing.MigLayout;

public class MainClass extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        MainClass frame = new MainClass();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int xAxisSize = ((int) toolkit.getScreenSize().getWidth());
        int yAxisSize = ((int) toolkit.getScreenSize().getHeight());
        frame.setSize(xAxisSize, yAxisSize);
        frame.setVisible(true);
        frame.setVisible(true);

    }

    /**
     * Create the frame.
     */
    public MainClass() {
        setAlwaysOnTop(true);
        final JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.setLayout(new MigLayout("center panel",
                "[100px:100px:1366px,grow,shrink 50,center]",
                "[100px:100px:768px,grow,shrink 50,center]"));
        desktopPane.setBorder(new LineBorder(new Color(0, 0, 0)));
        desktopPane.setBackground(Color.WHITE);
        setForeground(Color.WHITE);
        setTitle("JFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setPreferredSize(screenSize);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("Select");
        menuBar.add(mnNewMenu);

        JMenuItem mntmGrooups = new JMenuItem("Grooups");
        mntmGrooups.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GroupGUIForm form = new GroupGUIForm();//Here i am calling another jinternalframe
                desktopPane.removeAll();
                desktopPane.repaint();
                form.pack();
                form.setVisible(true);
                desktopPane.add(form);
            }
        });
        mnNewMenu.add(mntmGrooups);

        JSeparator separator = new JSeparator();
        mnNewMenu.add(separator);

        JSeparator separator_2 = new JSeparator();
        mnNewMenu.add(separator_2);

        JSeparator separator_1 = new JSeparator();
        mnNewMenu.add(separator_1);

        JSeparator separator_3 = new JSeparator();
        mnNewMenu.add(separator_3);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new MigLayout("", "[grow,shrink 50,fill]",
                "[grow]"));

        contentPane.add(desktopPane, "cell 0 0,grow");

    }
}









package Design;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;

public class GroupGUIForm extends JInternalFrame {

    private JPanel groupMainContentPane;
    private JTable jTableGroupNameList;
    private JTextField textField;
    private JTextField jTextFieldGroupName;
    private JTextField jTextFieldGroupAlias;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GroupGUIForm frame = new GroupGUIForm();//main method
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GroupGUIForm() {
        setMaximizable(true);
        setClosable(true);
        setResizable(true);
        setFont(new Font("SansSerif", Font.PLAIN, 12));
        setVisible(true);
        setTitle("Create & Modify Group Information");
        setBounds(100, 100, 700, 450);
        groupMainContentPane = new JPanel();
        groupMainContentPane.setForeground(new Color(0, 0, 0));
        groupMainContentPane.setBorder(new TitledBorder(null, "GroupForm", TitledBorder.LEADING, TitledBorder.TOP, null, Color.GRAY));
        setContentPane(groupMainContentPane);
        groupMainContentPane
                .setLayout(new MigLayout("", "[][100px:100px:100px,grow,fill][225px:225px:225px,grow,fill][10px:10px:10px,grow,fill][][grow]", "[20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][][][grow][]"));

        JPanel jTablePanel = new JPanel();
        jTablePanel.setBorder(new TitledBorder(null, "Available Group List",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        groupMainContentPane.add(jTablePanel, "cell 5 1 1 15,grow");
        jTablePanel.setLayout(new MigLayout("", "[235px]", "[][321px]"));

        JLabel lblSearch = new JLabel("Search");
        jTablePanel.add(lblSearch, "flowx,cell 0 0");

        JScrollPane jTableGroupNameScrollPane = new JScrollPane();
        jTablePanel.add(jTableGroupNameScrollPane, "cell 0 1,grow");

        jTableGroupNameList = new JTable();
        jTableGroupNameList.setFont(new Font("SansSerif", Font.PLAIN, 11));
        jTableGroupNameList
                .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTableGroupNameList.setName("");
        jTableGroupNameList.setAutoCreateRowSorter(true);
        jTableGroupNameList.setBorder(new LineBorder(new Color(0, 0, 0)));
        jTableGroupNameList.setModel(new DefaultTableModel(new Object[][] {
                { "John", null }, { "Bill", null }, { "Edmond", null },
                { "Adam", null }, { "", null },
                { null, null }, { null, null }, { null, null }, { null, null },
                { null, null }, { null, null }, { null, null }, { null, null },
                { null, null }, { null, null }, { null, null }, { null, null },
                { null, null }, { null, null }, { null, null }, },
                new String[] { "Avaliable Group Names", "" }));
        jTableGroupNameList.getColumnModel().getColumn(1).setPreferredWidth(15);
        jTableGroupNameList.getColumnModel().getColumn(1).setMinWidth(0);
        jTableGroupNameList.getColumnModel().getColumn(1).setMaxWidth(0);
        jTableGroupNameScrollPane.setViewportView(jTableGroupNameList);

        textField = new JTextField();
        jTablePanel.add(textField, "cell 0 0");
        textField.setColumns(20);

        JLabel lblNewLabel = new JLabel("Group Name:");
        groupMainContentPane.add(lblNewLabel, "cell 1 3,alignx trailing");

        jTextFieldGroupName = new JTextField();
        groupMainContentPane.add(jTextFieldGroupName, "cell 2 3,growx");
        jTextFieldGroupName.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("Group Alilas:");
        groupMainContentPane.add(lblNewLabel_1, "cell 1 6,alignx trailing");

        jTextFieldGroupAlias = new JTextField();
        groupMainContentPane.add(jTextFieldGroupAlias, "cell 2 6,growx");
        jTextFieldGroupAlias.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("Group Under:");
        groupMainContentPane.add(lblNewLabel_2, "cell 1 9,alignx trailing");

        JComboBox jComboBoxGroupUnder = new JComboBox();
        groupMainContentPane.add(jComboBoxGroupUnder, "cell 2 9,growx");

        JPanel jButtonsPanel = new JPanel();
        groupMainContentPane
                .add(jButtonsPanel, "cell 1 12 5 1,grow,dock south");
        jButtonsPanel.setLayout(new MigLayout("",
                "[55px][53px][63px][65px][][][]", "[23px]"));

        JButton btnBack = new JButton("Back");
        jButtonsPanel.add(btnBack, "cell 1 0,alignx left,aligny top");

        JButton btnNew = new JButton("New");
        jButtonsPanel.add(btnNew, "cell 2 0,alignx left,aligny top");

        JButton btnDelete = new JButton("Delete");
        jButtonsPanel.add(btnDelete, "cell 3 0,alignx left,aligny top");

        JButton btnSubmit = new JButton("Submit");
        jButtonsPanel.add(btnSubmit, "cell 6 0,alignx left,aligny top");
    }

    public JLabel getLblNewLabel() {
        return getLblNewLabel();
    }

    public JPanel getGroupMainContentPane() {
        return groupMainContentPane;
    }

    public JTable getTable() {
        return jTableGroupNameList;
    }
}
FullStack
  • 665
  • 11
  • 26
  • 1
    You need an SSCCE. There is something else happening here. – jzd Dec 30 '13 at 11:57
  • Hello Mr.jzd , i have placed my main class code here please have a look and let me know where am i going wrong ?, thank you. – FullStack Dec 30 '13 at 12:20
  • 1
    @user3008532 SSCCE is about short, runnable and compilable code, your code contains useless Look And Feel == remove, unknow Custom FileIO == store value in local variable and missing important issue from your question here `How to change size of JInternalFrame displayed in JDesktopPane that implements MigLayout?` == empty GroupGUIForm ??? or do you joking .... – mKorbel Dec 30 '13 at 12:41
  • Hello Mr.mKorbel,here i have put the entire executable code,please have a look and let me know about my mistake,thank you. – FullStack Dec 30 '13 at 13:03

1 Answers1

4

The problem is that you are setting a layout on the JDesktopPane. Just center the form on the desktop pane by setting the size and location. The point of a JDesktopPane is to allow a user to move and resize components, setting a MiGLayout on the pane defeats this.

Also, your SSCCE is way to long and includes a lot of junk. Here is an example of what your SSCCE could have looked liked:

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import net.miginfocom.swing.MigLayout;

public class MainClass {

    public static void main(String[] args) {
        JFrame frame = new JFrame ();
        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
        final JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.setLayout(new MigLayout("center panel",
                "[100px:100px:1366px,grow,shrink 50,center]",
                "[100px:100px:768px,grow,shrink 50,center]"));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        JInternalFrame form = new JInternalFrame();
        form.setResizable(true);      
        form.setVisible(true);
        desktopPane.add(form);
        frame.add(desktopPane);
    }
}
jzd
  • 23,473
  • 9
  • 54
  • 76
  • Thank you for your suggestion,but if i do not apply miglayout to the jdesktoppane then internal components will not shrink as per the screen size,is there any other way ? – FullStack Dec 30 '13 at 14:07
  • Continue to use the mig layout for the internal frame but for the JDesktopPane you will need to decide to allow users to rearrange/resize internal frames or have it automatically resized when the main frame is changed. If you want both you will have to implement it some other way than with a layout manager. – jzd Dec 30 '13 at 14:19
  • can you tell me how miglayout is deciding the size of jinternalframe here in my application ? or in general ? – FullStack Dec 30 '13 at 14:31
  • You are passing the parameters to MiGLayout. If you are asking how MiGLayout is using these parameters then look at the source code or ask another question because you are definitely leaving the scope of this question. – jzd Dec 30 '13 at 14:51