-1

I know how to code Java but I'm having a lot of trouble with this. I've made a menubar but I want to put a split pane underneath it. The menubar is fine but the split pane is giving me a lot of errors and I don't know how to fix it.

Any help would be much appreciated.

    package getcodinggui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

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


public class GetCodingGUI {
    JTextArea output;
    JScrollPane scrollPane;

    public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu;

        menuBar = new JMenuBar();

        menu = new JMenu("Home");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "File Menu Items");
        menuBar.add(menu);

        menu = new JMenu("About");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("Contact Us");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("FAQ");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("Log In");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        return menuBar;
    }


    public Container createContentPane() {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);


        //Create a scrolled text area.
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);

        //Add the text area to the content pane.
        contentPane.add(scrollPane, BorderLayout.CENTER);

        return contentPane;
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        public static class MyJFrameWin extends JFrame{ 

        JSplitPane jSplitPane, jSplitPane2;
        JPanel jPanel1, jPanel2a, jPanel2b;

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

            jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
                    jPanel2a, jPanel2b);
            jSplitPane2.setOneTouchExpandable(true);
            jSplitPane2.setDividerLocation(100);

            jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
                    jPanel1, jSplitPane2);
            jSplitPane.setOneTouchExpandable(true);
            jSplitPane.setDividerLocation(150);

            getContentPane().add(jSplitPane);
        }
}


        //Create and set up the content pane.
        GetCodingGUI demo = new GetCodingGUI();
        frame.setJMenuBar(demo.createMenuBar());
        frame.setContentPane(demo.createContentPane());

        //Display the window.
        frame.setSize(1280, 720);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(runJSplitPaneLater);
}

        javax.swing.SwingUtilities.invokeLater(new Runnable, runJSplitPaneLater() 

{
        static Runnable runJSplitPaneLater = new Runnable(){
            @Override
        public void run() {
            MyJFrameWin myJFrameWin = new MyJFrameWin();
            myJFrameWin.setVisible(true);
                createAndShowGUI();
            }  
        });


    }
}
  • 1
    "lots of errors" - please include the errors in your post – randers Feb 14 '16 at 11:32
  • `"lots of errors"` -- this suggests that your style of coding needs to be fixed. Don't type a large amount of code and then test it. Instead if you can't use a modern IDE such as NetBeans or Eclipse (which warns you of compilation issues almost immediately), then it is up to you to compile your code **early and often**, and most importantly **to not add any new code until current compilation issues are fixed**. Else you'll end up with a rat's nest of hard to fix errors. – Hovercraft Full Of Eels Feb 14 '16 at 15:56
  • Yes, I know I shoudl've mentioned the errors sorry. I coded it in NetBeans but i didn't understand what was going on... –  Feb 14 '16 at 22:28

1 Answers1

1

I had to clean up 20 compile errors.

Here's the GUI I created.

Split Pane Example

Here are the changes I made.

  1. I rearranged all of your code. Code is much easier to understand when it reads from top to bottom.

  2. Since the SwingUtilities invokeLater method requires a Runnable, I made your GUI view class implement Runnable.

  3. I put your content pane in one of the JSplitPanes. I just guessed which pane.

  4. I put the outer JSplitPane into the JFrame.

  5. I fixed your menu alt keys.

  6. I returned a JPanel from your createContentPane method.

  7. I formatted your code.

  8. I reduced the size of your JFrame so it would fit on my screen.

Here's the code:

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class GetCodingGUI implements Runnable {
    private JTextArea output;
    private JScrollPane scrollPane;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GetCodingGUI());
    }

    @Override
    public void run() {
        // Create and set up the window.
        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JSplitPane jSplitPane, jSplitPane2;
        JPanel jPanel1, jPanel2a, jPanel2b;

        jPanel1 = new JPanel();
        jPanel2a = new JPanel();
        jPanel2b = createContentPane();

        jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jPanel2a,
                jPanel2b);
        jSplitPane2.setOneTouchExpandable(true);
        jSplitPane2.setDividerLocation(100);

        jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel1,
                jSplitPane2);
        jSplitPane.setOneTouchExpandable(true);
        jSplitPane.setDividerLocation(150);

        frame.add(jSplitPane);
        frame.setJMenuBar(createMenuBar());

        // Display the window.
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu;

        menuBar = new JMenuBar();

        menu = new JMenu("Home");
        menu.setMnemonic(KeyEvent.VK_H);
        menu.getAccessibleContext().setAccessibleDescription("File Menu Items");
        menuBar.add(menu);

        menu = new JMenu("About");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("Contact Us");
        menu.setMnemonic(KeyEvent.VK_C);
        menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("FAQ");
        menu.setMnemonic(KeyEvent.VK_F);
        menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("Log In");
        menu.setMnemonic(KeyEvent.VK_L);
        menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
        menuBar.add(menu);

        return menuBar;
    }

    public JPanel createContentPane() {
        // Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);

        // Create a scrolled text area.
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);

        // Add the text area to the content pane.
        contentPane.add(scrollPane, BorderLayout.CENTER);

        return contentPane;
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111