tags:

views:

71

answers:

2
+2  Q: 

Java tabs in GUI

Hello, I am hoping someone can spoon feed me this solution. It is part of my major lab for the class, and it really isn't giving me too much since I just don understand how to make a GUI with tabs. I can make a regular program with some sort of GUI, but I've been searching and reading and can't put 2 and 2 because of the whole class part and declaring the private variables. So what I am asking is if someone can make me a main GUI with 5 tabs and put my code into 1 tab so I can learn and put the rest of my codes into the other 4 tabs. So I hope you don't think I want you to give me code when I have the code, I just don't really get the tabs, and we haven't gone over it in class. Here is the code with its own gui, I hope what I type makes sense. I learn code by seeing, and this will help me a bunch.

  package landscape;
import javax.swing.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.text.DecimalFormat;

public class Landscape extends JFrame {

    private JFrame mainFrame;
    private JButton calculateButton;
    private JButton exitButton;
    private JTextField lengthField;
    private JLabel lengthLabel;
    private JTextField widthField;
    private JLabel widthLabel;
    private JTextField depthField;
    private JLabel depthLabel;
    private JTextField volumeField;
    private JLabel volumeLabel;
    private JRadioButton builtIn;
    private JRadioButton above;
    private JTabbedPane panel;

    public Landscape()
    {

        JTabbedPane tabs=new JTabbedPane();
        tabs.addTab("Pool", panel);//add a tab for the panel with the title "title"
        //you can add more tabs in the same fashion - obviously you can change the title
        //tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab
        mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane
        mainFrame.setSize(265,200);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
        mainFrame.setResizable(false);
        JPanel panel = new JPanel();//FlowLayout is default

        //Pool
            panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel
        panel.add(builtIn);
        panel.add(above);
        panel.add(lengthLabel);
        panel.add(lengthField);
        panel.add(widthLabel);
        panel.add(widthField);
        panel.add(depthLabel);
        panel.add(depthField);
        panel.add(volumeLabel);
        panel.add(volumeField);
        panel.add(calculateButton);
        panel.add(exitButton);      
        //creating components
        calculateButton = new JButton ("Calculate");
        exitButton = new JButton ("Exit");
        lengthField = new JTextField (5);
        lengthLabel = new JLabel ("Enter the length of your pool: ");
        widthField = new JTextField (5);
        widthLabel = new JLabel ("Enter the width of your pool: ");
        depthField = new JTextField (5);
        depthLabel = new JLabel ("Enter the depth of your pool: ");
        volumeField = new JTextField (5);
        volumeLabel = new JLabel ("Volume of the pool: ");
        //radio button
        ButtonGroup buttonGroup = new ButtonGroup();
        builtIn = new JRadioButton ("Built in");
        buttonGroup.add(builtIn);
        above = new JRadioButton ("Above");
        buttonGroup.add(above);

        exitButton.setMnemonic('x');
        calculateButton.setMnemonic('C');


        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) 
            { System.exit(0); }
        });

        // create the handlers
        calculateButtonHandler chandler = new calculateButtonHandler(); //instantiate new object
        calculateButton.addActionListener(chandler); // add event listener

        ExitButtonHandler ehandler = new ExitButtonHandler(); 
        exitButton.addActionListener(ehandler); 

        FocusHandler fhandler = new FocusHandler();
        lengthField.addFocusListener(fhandler);
        widthField.addFocusListener(fhandler);
        depthField.addFocusListener(fhandler);

    }

    class calculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            DecimalFormat num = new DecimalFormat(", ###.##");
            double width;
            double length;
            double depth;
            double volume;
            double volume2;
            double height;
            String instring;

            instring = lengthField.getText();
            if (instring.equals(""))
            {
                instring = "0";
                lengthField.setText("0");
            }
            length = Double.parseDouble(instring);

            instring = widthField.getText();
            if (instring.equals(""))
            {
                instring = "0";
                widthField.setText("0");
            }
            width = Double.parseDouble(instring);

            instring = depthField.getText();
            if (instring.equals(""))
            {
                instring = "0";
                depthField.setText("0");
            }
            depth = Double.parseDouble(instring);

            volume = width * length * depth;
            volumeField.setText(num.format(volume));

            volume2 = width * length * depth;
            }

        }

    class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    class FocusHandler implements FocusListener
    {
        public void focusGained(FocusEvent e)
        {
            if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField)
            {
                volumeField.setText("");
            }
            else if (e.getSource() == volumeField)
            {
                volumeField.setNextFocusableComponent(calculateButton);
                calculateButton.grabFocus();
            }
        }

        public void focusLost1(FocusEvent e)
        {
            if(e.getSource() == widthField)
            {
                widthField.setNextFocusableComponent(calculateButton);
            }
        }

        public void focusLost(FocusEvent e)
        {
            if(e.getSource() == depthField)
            {
                depthField.setNextFocusableComponent(calculateButton);
            }
        }
    }

    public static void main(String args[])
    {
        Landscape app = new Landscape();
    }
}
+1  A: 

The tutorial and its demo are pretty straight forward examples.

JamesG
I've been using that and cutting my code and putting it in there but it isn't working correctly. My problem is with the public class () and all of the variables, where they go. I don't believe I can have more then 1 public class, so it boggles me.
David
You can not have more than one public class per class file. You can have as many class files as you like*. In a class file, you can have as many non public classes as you like*. (* Unless your teacher says otherwise).
emory
+1  A: 

Instead of getting the content pane and adding to it, just create a new JPanel and add your stuff to that. Then, add the panel to a new JTabbedPane with whatever title you want, and set the content pane of the JFrame to be the tabbed pane.

Here is a simple example of what you would do:

JPanel panel=new JPanel();//FlowLayout is default
panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel
panel.add(builtIn);
panel.add(above);
//...you get the picture; add all the stuff you already do, just use panel instead of c
JTabbedPane tabs=new JTabbedPane();
tabs.addTab("title", panel);//add a tab for the panel with the title "title"
//you can add more tabs in the same fashion - obviously you can change the title
tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab
mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane

You can leave the rest of your code how it is and it should work fine.

qmega
This helps a lot, thanks, but with the panel.add will this be just in the tab? Because I need a like 4 different tabs with different operations in it. Obviously I need the first tab to contain code to calculate the volume of the pool, and my next tab will be code for a hot tub.Also, I understand how to add a tab, but how do I get my pool code to show just in the tab that is called "title"?
David
Anything in panel will be displayed in the tab called "title", and only in that tab. To put stuff in other tabs, just create another JPanel, put stuff in the new panel, and then add another tab with that panel in it. Hope that helps.
qmega
Hey man, I edited my code around to what, *I believe*, you were suggesting, but I get this error message when I try to compile it. Exception in thread "main" java.lang.NullPointerException at landscape.Landscape.<init>(Landscape.java:33) at landscape.Landscape.main(Landscape.java:189)I edited my original code to what I'm working with now. For some reason, I feel I butchered this.
David
You are getting the NullPointerException because you haven't instantiated mainFrame. You still have to have `mainFrame=new JFrame("whatever title");`. Just put that back in the beginning of your constructor and it should work. Also, the "creating components" part should be put before you add anything to the panel; otherwise you'll get more NullPointerExceptions.
qmega
Ah! Silly me. I screwed up the order when pasting my code in. Can't add anything to the panel without having it defined yet. Thanks so much for the help, I should be able to breeze through this.
David