0

I make my first baby steps in Java and Swing GUI. My plan is to make a little GUI with an JTextArea to fill in some text and two tabbed JPanels. But the problem is, when I fill in some text in the JTextArea and click the tab "Includes" (or the other tab "Functions"), it resizes the tabmenu (It should have a fixed 50:50 view).

Here are some pictures for a better understanding:

enter image description here enter image description here enter image description here

Here is the Java code:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

public class Editor extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        Editor editor = new Editor();
    }

    private JTextArea guiEdit = new JTextArea();
    private JScrollPane utilScrollbar = new JScrollPane(guiEdit);
    private JTabbedPane guiOverviewTabPanel = new JTabbedPane();
    private JPanel guiOverviewTabIncludes = new JPanel();
    private JPanel guiOverviewTabFunctions = new JPanel();

    public Editor() {
        // Set layout
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gridbag);

        // Add the textfield
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.WEST;
        add(utilScrollbar, c);

        // Add tabs to the JTabbedPane
        guiOverviewTabPanel.add("Includes", guiOverviewTabIncludes);
        guiOverviewTabPanel.add("Functions", guiOverviewTabFunctions);

        // Add the JTabbedPane
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 1;
        c.gridy = 0;
        c.gridheight = 1;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.EAST;
        add(guiOverviewTabPanel, c);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        this.setMinimumSize(new Dimension(800, 400));

        setVisible(true);
    }
}

Does anyone know a way/solution to fix this perspective. The real ratio ist 5:2 (5 parts JTextArea and 2 parts JTabbedPane - this is just for a better understanding, so I can't use a GridLayout

swaechter
  • 1,357
  • 3
  • 22
  • 46
  • 1
    I don't think your baby is able to do that. – Roman C Nov 08 '12 at 17:37
  • Is there an other (better) layout manager ? I tested it with a BorderLayout and packed the JTabbedPane to EAST, but the two tabs weren't in a horizontal line. And I can't use a GridLayout, because I don't have a 1:1 perspective (I have a 5:2) – swaechter Nov 08 '12 at 17:41
  • 1
    The real solution would go for baby `JSplitPane`. – Roman C Nov 08 '12 at 17:55
  • Hi. Thank you. This is a very nice solution (I can set a minimum width for each component and the user can change the width of the JTextArea. Thank you – swaechter Nov 08 '12 at 18:11

1 Answers1

1

A JSplitPane is meant for this kind of GUI display.

I modified your code to use a JSplitPane.

import java.awt.Dimension;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

public class Editor extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new Editor();
    }

    private JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    private JTextArea guiEdit = new JTextArea();
    private JScrollPane utilScrollbar = new JScrollPane(guiEdit);
    private JTabbedPane guiOverviewTabPanel = new JTabbedPane();
    private JPanel guiOverviewTabIncludes = new JPanel();
    private JPanel guiOverviewTabFunctions = new JPanel();

    public Editor() {
        HierarchyListener hierarchyListener = new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent event) {
                long flags = event.getChangeFlags();
                if ((flags & HierarchyEvent.SHOWING_CHANGED) == 
                        HierarchyEvent.SHOWING_CHANGED) {
                    splitPane.setDividerLocation(0.50D);
                }
            }

        };

//      guiEdit.setLineWrap(true);
//      guiEdit.setWrapStyleWord(true);

        splitPane.setLeftComponent(utilScrollbar);

        // Add tabs to the JTabbedPane
        guiOverviewTabPanel.add("Includes", guiOverviewTabIncludes);
        guiOverviewTabPanel.add("Functions", guiOverviewTabFunctions);

        splitPane.setRightComponent(guiOverviewTabPanel);

        splitPane.addHierarchyListener(hierarchyListener);
        add(splitPane);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        this.setMinimumSize(new Dimension(800, 400));

        setVisible(true);
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111