Two Split pane place on a tabpane

480 views Asked by At
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;


public class MenuPage extends JFrame{

    final static int extraWindowWidth = 100;

    JSplitPane jSplitPane, jSplitPane2,jSplitPane3,jSplitPane4;
    JPanel jPanel2a, jPanel2b, jPanel3;

    public static JFrame frame; 

    public MenuPage(){
    }

    private final JList<String> list1=new JList<>(new String[]{"A","B"});
    private final JList<String> list2 = new JList<>();
    private final List<DefaultListModel> models = new ArrayList<>();



    public void addComponentToPane(Container pane) {
        JTabbedPane tabbedPane = new JTabbedPane();



        // Create the "cards".
        JPanel card1 = new JPanel() {
            // Make the panel wider than it really needs, so
            // the window's wide enough for the tabs to stay
            // in one row.
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                size.width += extraWindowWidth;
                return size;
            }
        };


         DefaultListModel<String> model1 = new DefaultListModel<>();
            model1.addElement("A1");
            model1.addElement("A2");
            model1.addElement("A3");
            models.add(model1);
            DefaultListModel<String> model2 = new DefaultListModel<>();
            model2.addElement("B1");
            model2.addElement("B2");
            models.add(model2);
            list2.setModel(model1);
            list1.addListSelectionListener((ListSelectionEvent e) -> {
                if (!e.getValueIsAdjusting()) {
                    list2.setModel(models.get(list1.getSelectedIndex()));
                }
            });






        jPanel2a = new JPanel();
        jPanel2b = new JPanel();
        jPanel3 = new JPanel();







        jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);





        jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
        jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);




        // panelin icerisine component eklendi
        JLabel lblHelloWorld = new JLabel("Hello World!");
        jPanel2a.add(lblHelloWorld);

        jSplitPane2.setOneTouchExpandable(true);
        jSplitPane2.setDividerLocation(100);


        jSplitPane3.setOneTouchExpandable(true);
        jSplitPane3.setDividerLocation(150);


        jSplitPane4.setOneTouchExpandable(true);
        jSplitPane4.setDividerLocation(300);



        JPanel card3 = new JPanel();
        JPanel card4 = new JPanel();
        JPanel card5 = new JPanel();
        JPanel card6 = new JPanel();
        JPanel card7 = new JPanel();
        JPanel card8 = new JPanel();



        card3.add(jSplitPane3,jSplitPane4);


        tabbedPane.addTab("Main", jSplitPane2);

        tabbedPane.addTab("Leagues",jSplitPane4);


        tabbedPane.addTab("Teams",card3 );
        tabbedPane.addTab("Ratios", card4);
        tabbedPane.addTab("Competitions", card5);
        tabbedPane.addTab("Analyze", card6);
        tabbedPane.addTab("Help", card7);
        tabbedPane.addTab("About", card8);

        pane.add(tabbedPane, BorderLayout.CENTER);


    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */

    public static void createAndShowGUI() {
        // Create and set up the window.
        frame = new JFrame("BetAnalyzer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(594, 310);

        // Create and set up the content pane.
        MenuPage demo = new MenuPage();
        demo.addComponentToPane(frame.getContentPane());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setBounds(100, 100, 635, 251);

    }
}

I want the list1, list2, and jpanel3 which takes part 2 JSplitPane to be in the league tab There is something wrong in my code. Just jSplitPane3 or jSplitPane4 could be appear in the layout.

Also, I tried to add two JSplitPane into Box then I put the Box in tabbedpane but this's not working neither.

1

There are 1 answers

0
Timothy Truckle On BEST ANSWER

Your code has several issues.

do not inherit without reason

Your class inherits from JFrame but its does not extend its behavior (it does not override a public or protected method of JFrame).

Components can only be added once

In contrast to the other points this is not an issue per se but part of your problem.

 jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
 // ...     
 card3.add(jSplitPane3,jSplitPane4);

When the second statement is executed the jSplitPane3 is removed from the jSplitPane4 and only shown as content of card3.

use of standard layouts

The (dummy) content of your panels is rather small so that their preferedSize is less then the available space in the parent container.

The JPanels default Layout is FlowLayout which reduces each of its components to its preferedSize.

You should set the Layout of your JPanels to BorderLayout which allows its content to stretch out over the complete space.

In turn you cannot use JPanels varag method .add(Component, component) anymore.


here are the relevant changes:

    jPanel2a = new JPanel(new BorderLayout());
    jPanel2b = new JPanel(new BorderLayout());
    jPanel3 = new JPanel(new BorderLayout());

    jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);

    jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
    jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jSplitPane3, jPanel3);

    // panelin icerisine component eklendi
    JLabel lblHelloWorld = new JLabel("Hello World!");
    jPanel2a.add(lblHelloWorld);

//      jSplitPane2.setOneTouchExpandable(true);
        jSplitPane2.setDividerLocation(100);
//
//      jSplitPane3.setOneTouchExpandable(true);
        jSplitPane3.setDividerLocation(150);
//
//      jSplitPane4.setOneTouchExpandable(true);
        jSplitPane4.setDividerLocation(300);

    JPanel card3 = new JPanel(new BorderLayout());
    JPanel card4 = new JPanel(new BorderLayout());
    JPanel card5 = new JPanel(new BorderLayout());
    JPanel card6 = new JPanel(new BorderLayout());
    JPanel card7 = new JPanel(new BorderLayout());
    JPanel card8 = new JPanel(new BorderLayout());

//      card3.add(jSplitPane3);
    card3.add(jSplitPane4,BorderLayout.SOUTH);

    tabbedPane.addTab("Main", jSplitPane2);