A problem with JSplitPane and JInternalFrame

70 Views Asked by At

GUI:

https://i.stack.imgur.com/ufRiI.png

the bug:

https://i.stack.imgur.com/Lt4D0.png

I am making a small program to write my work in. I have been getting this bug for a while now and can't progress with it.

The number of panels doubles every time I press on the button to make an JInternalFrame and shows when I am trying to resize the internal frame.

I am suspicious that I am not using JSplitPane and internal frame correctly together, but I am not sure where the problem is.

Here is my entire code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudyNotesGUI extends JFrame{

    static JFrame f = new JFrame();
    JPanel panelmain = new JPanel();
    JPanel panelleft = new JPanel();
    JPanel panelfordoc = new JPanel();
    JPanel panelright = new JPanel();
    JPanel TextEditors = new JPanel();
    JPanel UpperToolsMenu = new JPanel();
    JPanel LowerToolsMenu = new JPanel();

    final JDesktopPane desktop = new JDesktopPane();

    JTextArea document = new JTextArea();

    JButton[] UTButtons = new JButton[2];
    JButton[] LTButtons = new JButton[4];

    JComboBox fontBox;

    static JPanel panelCalc = new JPanel();
    static JPanel panelWB = new JPanel();

    JInternalFrame web_browser = new JInternalFrame("Web Browser", true,true, true, false);
    JInternalFrame calculator = new JInternalFrame("Calculator", true,true, true, false);

    public StudyNotesGUI() {

        //Frame characteristics
        f.setTitle("Study Notes v 0.00.1");
        f.setLayout(new GridLayout());

        // document characteristics
        document.setLineWrap(true);
        document.setWrapStyleWord(true);
        document.setBorder(BorderFactory.createRaisedSoftBevelBorder());

        //scroll pane
        JScrollPane scrollPane = new JScrollPane(document);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

        // Main panel layouts
        //f.setLayout(new BorderLayout());
        panelmain.setLayout(new BorderLayout());
        panelleft.setLayout(new BorderLayout());
        panelright.setLayout(new BorderLayout());


        JSplitPane splitPane = new JSplitPane();
        splitPane.setBounds(1,1,1,1);

        // Additional panel layouts
        panelfordoc.setLayout(new GridLayout());
        TextEditors.setLayout(new FlowLayout());
        UpperToolsMenu.setLayout(new GridLayout());
        LowerToolsMenu.setLayout(new GridLayout());
        //MetaMenu.setLayout(new GridLayout());

        // Document settings
        document.setFont(new Font("Arial", Font.PLAIN, 20));


        //Menu bar
        JMenuBar menuBar = new JMenuBar();
        JMenu saveMenu = new JMenu("Save");
        JMenu loadMenu = new JMenu("Load");
        menuBar.add(saveMenu);
        menuBar.add(loadMenu);


        //Buttons
        // Text editor buttons
        // Font editor
        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        JComboBox font = new JComboBox(fonts);
        //font.setEditable(true);
        font.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                document.setFont(new Font((String) font.getSelectedItem(), Font.PLAIN, document.getFont().getSize()));
            }
        });
        TextEditors.add(font);

        JButton bold = new JButton("B");
        TextEditors.add(bold);
        JButton italic = new JButton("I");
        TextEditors.add(italic);
        JButton underline = new JButton("U");
        TextEditors.add(underline);
        JButton underline2 = new JButton("U2");
        TextEditors.add(underline2);
        JButton upper = new JButton("UP");
        TextEditors.add(upper);
        JButton lower = new JButton("LO");
        TextEditors.add(lower);
        JButton paragraph = new JButton("Par");
        TextEditors.add(paragraph);

        // Upper tools for the free space buttons
        UTButtons[0] = new JButton("");
        UpperToolsMenu.add(UTButtons[0]);
        UTButtons[0].addActionListener(e -> {
            //web_browser.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
            //web_browser.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
            web_browser.setVisible(true);
            panelWB.setBackground(Color.GREEN);
            //new SNGUINote();
            web_browser.add(panelWB);
            web_browser.setMinimumSize(new Dimension(200,200));
            web_browser.setPreferredSize(new Dimension(600,400));
            web_browser.pack();
            desktop.add(web_browser);

        });
        UTButtons[1] = new JButton("Calculator");
        UpperToolsMenu.add(UTButtons[1]);
        UTButtons[1].addActionListener(e -> {
            //calculator.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
            //calculator.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
            new SNGUICal();
        });




        for (int i = 0; i < 2 ; i++) {
            UTButtons[i].setFocusable(false);
        }

        // Lower tools for the free space buttons
        LTButtons[0] = new JButton("FastFind");
        LowerToolsMenu.add(LTButtons[0]);
        LTButtons[1] = new JButton("Draw");
        LowerToolsMenu.add(LTButtons[1]);
        LTButtons[2] = new JButton("Analytics");
        LowerToolsMenu.add(LTButtons[2]);
        LTButtons[3] = new JButton("Schedule");
        LowerToolsMenu.add(LTButtons[3]);

        for (int i = 0; i < 4 ; i++) {
            LTButtons[i].setFocusable(false);
        }

        desktop.setBackground(Color.gray);

        // // // // // // // // //
        desktop.add(calculator);
        //scrollPane.add(document);
        panelleft.add(scrollPane, BorderLayout.CENTER);
        //panelleft.add(document);
        panelleft.add(TextEditors, BorderLayout.NORTH);
        panelright.add(UpperToolsMenu, BorderLayout.NORTH);
        panelright.add(LowerToolsMenu, BorderLayout.SOUTH);
        panelright.add(desktop, BorderLayout.CENTER);

        splitPane.setLeftComponent(panelleft);
        splitPane.setRightComponent(panelright);
        panelmain.add(splitPane);

        f.add(panelmain);
        f.setJMenuBar(menuBar);

        splitPane.setResizeWeight(0.4);
        document.setPreferredSize(new Dimension(450,450));
        calculator.pack();
    }

    /* public void actionPerformed(ActionEvent e) {
        if (e.getSource() == UTButtons[0]) {

        }

        if (e.getSource() == UTButtons[1]) {
        }
    } */

    public static void main(String[] args) {

        new StudyNotesGUI();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setResizable(true);
        f.setVisible(true);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setSize(1440,900);
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);

    }
}
0

There are 0 best solutions below