0

I've looked on here for resources, but I just can't figure out how to add a JScrollPane. I keep on trying to add the whole frame, but I of course can't do it that way. What simple step am I missing? After the information is given and begin is pressed, my program screen is too small so I need the Scroll Pane.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

// This is ran through App class. 
// This class uses Math Panel which uses ArithmeticQuestions. 
public class PictureandProblemsFrame extends JFrame {
    private JMenuBar menuBar;
    // private ChoicesPanel choicesPanel;
    private int userRandomNumber;
    private String picture1Address = "Picture1.jpg";
    private String picture2Address = "Picture2.jpg";
    private String picture3Address = "Picture3.jpg";
    private JLabel numbersLabel;
    private JLabel arithmeticLabel;
    private JLabel picturesLabel;
    private JLabel splitLabel;
    private JComboBox<String> numbersCombo;
    private JComboBox<String> arithmeticCombo;
    private JComboBox<String> picturesCombo;
    private JComboBox<String> splitCombo;
    private JButton beginButton;
    private int split;
    private int arithmetic;
    private int pictures;

    public PictureandProblemsFrame() {
        // Title of JFrame
        setTitle("Not Working: Cannot combine the picture and problems");
        setJMenuBar(createMenuBar());
        InformationPanel();
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane);
        setSize(900, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public JMenuBar createMenuBar() {
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");

        JMenu submenu = new JMenu("Submenu");
        submenu.setMnemonic(KeyEvent.VK_S);

        JMenuItem subMenuItem = new JMenuItem("An item in the submenu");
        subMenuItem.setMnemonic(KeyEvent.VK_T);
        submenu.add(subMenuItem);

        JMenuItem subMenuItem2 = new JMenuItem("Another item");
        subMenuItem2.setMnemonic(KeyEvent.VK_F);
        submenu.add(subMenuItem2);
        fileMenu.add(submenu);
        fileMenu.addSeparator();

        JMenuItem exitItem = new JMenuItem("Exit");
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);

        // Set accelerators and Mnemonics
        fileMenu.setMnemonic(KeyEvent.VK_F);
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));

        // Make program close upon exit selecton chosen
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int action = JOptionPane.showConfirmDialog(PictureandProblemsFrame.this,
                        "Do you really want to exit the application?", "Confirm Exit", JOptionPane.OK_CANCEL_OPTION);
                if (action == JOptionPane.OK_OPTION) {
                    System.exit(0);
                }
                // System.exit(0);
            }
        });
        return menuBar;
    }

    public void InformationPanel() {
        // Creates all Labels
        numbersLabel = new JLabel("Numbers");
        arithmeticLabel = new JLabel("Arithmetic");
        picturesLabel = new JLabel("Picture");
        splitLabel = new JLabel("Split");

        // Creates all drop down selections
        numbersCombo = new JComboBox<String>();
        arithmeticCombo = new JComboBox<String>();
        picturesCombo = new JComboBox<String>();
        splitCombo = new JComboBox<String>();

        // Creates begin button
        beginButton = new JButton("Begin");

        // Adds selections to drop down menu
        DefaultComboBoxModel<String> numbersModel = new DefaultComboBoxModel<String>();
        numbersModel.addElement("1");
        numbersModel.addElement("2");
        numbersModel.addElement("3");
        numbersModel.addElement("4");
        numbersModel.addElement("5");
        numbersModel.addElement("6");
        numbersModel.addElement("7");
        numbersModel.addElement("8");
        numbersModel.addElement("9");
        numbersModel.addElement("10");
        numbersModel.addElement("11");
        numbersModel.addElement("12");
        numbersCombo.setModel(numbersModel);

        // Adds selections to drop down menu
        DefaultComboBoxModel<String> arithmeticModel = new DefaultComboBoxModel<String>();
        arithmeticModel.addElement("Add/Sub");
        arithmeticModel.addElement("Multiply/Divide");
        arithmeticCombo.setModel(arithmeticModel);

        // Adds selections to drop down menu
        DefaultComboBoxModel<String> picturesModel = new DefaultComboBoxModel<String>();
        picturesModel.addElement("1");
        picturesModel.addElement("2");
        picturesModel.addElement("3");
        picturesCombo.setModel(picturesModel);

        // Adds selections to drop down menu
        DefaultComboBoxModel<String> splitModel = new DefaultComboBoxModel<String>();
        splitModel.addElement("4");
        splitModel.addElement("9");
        splitModel.addElement("16");
        splitCombo.setModel(splitModel);

        // FlowLayout so far makes this easiest.
        setLayout(new FlowLayout());
        add(numbersLabel);
        add(numbersCombo);
        add(arithmeticLabel);
        add(arithmeticCombo);
        add(picturesLabel);
        add(picturesCombo);
        add(splitLabel);
        add(splitCombo);
        add(beginButton);
}
}
Trey
  • 63
  • 1
  • 9
  • [Similar questions](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+add+jscrollpane+to+jframe) – Hovercraft Full Of Eels Oct 19 '16 at 19:29
  • Also your question and code shows that you haven't searched for or read the basic JScrollPane tutorial. It shows you exactly how to do it with a working example. For one -- you're adding nothing to the JScrollPane's viewport. Google JScrollPane tutorial -- first hit. – Hovercraft Full Of Eels Oct 19 '16 at 19:31
  • I have, but didn't want to include my messy code on here so I gave a simple outline of it to possibly make it easier for anyone willing to help. Thanks for all the help. – Trey Oct 19 '16 at 19:35
  • Start simpler. Create a bare bones program that has a JFrame and JScrollPane, and in fact start with the tutorial's code. It's all there -- really. Also, understand that this isn't a "help" site, but rather a "question and answer" site. The question of "how to use JScrollPane in a JFrame" has been asked and answered many times previously which is why this question was closed. – Hovercraft Full Of Eels Oct 19 '16 at 19:52

0 Answers0