Results 1 to 4 of 4
  1. #1
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default New Tabbed Program issues

    I'm creating my first tabbed program and could use some help from the community.

    I've only started on the GUI so it hasn't been too bad. I only have one question so far. How do I go about organizing my controls (text boxes, buttons, etc) on my form? I have done a little reading on the layout managers of JAVA but i'm not sure how to apply them to tabular structure.

    For instance, on my "General" tab it looks like:

    "The date is: <DATE> The time is: <TIME>"

    I would like it to be:

    "The date is: <DATE>
    The time is: <TIME>"

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JButton;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class FinalProject extends JFrame {
        
        public FinalProject() {
            
             //Title 
            setTitle("Pool and Hot Tub calculator");  
             //set size
            setSize(300,300);
            //Here we are creating the object
            JTabbedPane jtp = new JTabbedPane();
            //jtp.setLayout(new BoxLayout(jtp, BoxLayout.VERTICAL));
            //jtp.add(Time);
            //jtp.add(Date);
    
    
            //Creates Template
           getContentPane().add(jtp);
    
           //Creates Tabs
           JPanel Splash = new JPanel();
           JPanel Pools = new JPanel();
           JPanel HotTubs = new JPanel(); 
           JPanel Settings = new JPanel();
           JPanel Customers = new JPanel();
           JPanel Employees = new JPanel();
           JPanel TempCalc = new JPanel();
           JPanel LengthCalc = new JPanel();
           
           
           		//Splash tab controls
    		        //Creates label for date
    		       JLabel Date = new JLabel();
    		       Date.setText("The date is:");
    		       Splash.add(Date);
    		       
    		       //Creates label for time
    		       JLabel Time = new JLabel();
    		       Time.setText("The time is:");
    		       Splash.add(Time);
    		       
    		       
    		       
    		    //Pools tab controls
    		       //Creates Label and Textbox for length
    		       //Label
    		       JLabel lblPLength = new JLabel();
    		       lblPLength.setText("Enter the pool's length (ft): ");
    		       Pools.add(lblPLength);
    		       
    		       //Text field
    		       JTextField txtPLength = new JTextField();
    		       Pools.add(txtPLength);
    		       
    		       //Label
    		       JLabel lblPWidth = new JLabel();
    		       lblPWidth.setText("Enter the pool's width (ft): ");
    		       Pools.add(lblPWidth);
    		       
    		       //Text field
    		       JTextField txtPWidth = new JTextField();
    		       Pools.add(txtPWidth);
    		       
    		       //Label
    		       JLabel lblPDepth = new JLabel();
    		       lblPDepth.setText("Enter the pool's depth (ft): ");
    		       Pools.add(lblPDepth);
    		       
    		       //Text field
    		       JTextField txtPDepth = new JTextField();
    		       Pools.add(txtPDepth);
    		       
    		       //Volume calculation button
    		     JButton btnPVolume = new JButton("Calculate Volume");
    		      Pools.add(btnPVolume);
    		       
    		       //Label
    		       JLabel lblPVolume = new JLabel();
    		       lblPVolume.setText("Enter the pool's volume (ft^3): ");
    		       Pools.add(lblPVolume);
    		       
    		       //Text field
    		       JTextField txtPVolume = new JTextField();
    		       Pools.add(txtPVolume);
    		       
    		       
    		       
    		       //Hot Tubs tab controls
    
    		       		//radio button
    			       JRadioButton btnHRound = new JRadioButton();
    			       btnHRound.setSelected(true);
    			       
    			       JRadioButton btnHOval = new JRadioButton();
    			       
    			     //Group the radio buttons.
    			       ButtonGroup group = new ButtonGroup();
    			       group.add(btnHRound);
    			       group.add(btnHOval);
    		       
    		       
    			       //Label
    			       JLabel lblHLength = new JLabel();
    			       lblHLength.setText("Enter the tub's lenth (ft): ");
    			       HotTubs.add(lblHLength);
    			       
    			       //Text field
    			       JTextField txtHLength = new JTextField();
    			       HotTubs.add(txtHLength);
    			       
    			       //Label
    			       JLabel lblHDepth = new JLabel();
    			       lblHDepth.setText("Enter the tub's depth (ft): ");
    			       HotTubs.add(lblHDepth);
    			       
    			       //Text field
    			       JTextField txtHDepth = new JTextField();
    			       HotTubs.add(txtHDepth);
    			       
    			       //Volume calculation button
    				     JButton btnHVolume = new JButton("Calculate Volume");
    				      HotTubs.add(btnHVolume);
    				      
    				    //Label
    				       JLabel lblHWidth = new JLabel();
    				       lblHWidth.setText("The tub's volume is (ft^3):  ");
    				       HotTubs.add(lblHWidth);
    
    				       
    				       
    				  //Temp Calc tab controls
    				       
    				       //Label
    				       JLabel lblTTemp = new JLabel();
    				       lblTTemp.setText("Enter Temperature:  ");
    				       TempCalc.add(lblTTemp);
    				       
    				       //Combo Box
    				       String[] strTTemp = { "C", "F"};
    				     JComboBox cmbTTemp = new JComboBox(strTTemp);
    				     cmbTTemp.setSelectedIndex(0);
    				     TempCalc.add(cmbTTemp);
    				    // cmbTTemp.addActionListener(this);
    				       
    				       //Label
    				       JLabel lblTResult = new JLabel();
    				       lblTResult.setText("Result: ");
    				       TempCalc.add(lblHDepth);
    				      
    				       
    				       //Text field
    				       JTextField txtTResult = new JTextField();
    				       TempCalc.add(txtTResult);
    				       
    				       //Label beside the result (Will be set to F or C)
    				       JLabel lblTTempType = new JLabel();
    				      // lblTResult.setText("Result: ");
    				       TempCalc.add(lblTTempType);
    				       
    				       //Temp Conversion button
    					     JButton btnTConvert = new JButton("Convert");
    					     TempCalc.add(btnTConvert);
    					      
    					    //Label - Shows status 
    					       JLabel lblTStatus = new JLabel();
    					       lblHWidth.setText(" ");
    					       TempCalc.add(lblTStatus);
    					       
    					       
    			       // Length Calc tab controls
    					       
    					       //label
    					       JLabel lblLMM = new JLabel();
    					       lblLMM.setText("Millimeters");
    					       LengthCalc.add(lblLMM);
    					       
    					       //Text field
    					       JTextField txtLMM = new JTextField();
    					       LengthCalc.add(txtLMM);
    					       
    					       //Label
    					       JLabel lblLM = new JLabel();
    					       lblLM.setText("Meters");
    					       LengthCalc.add(lblLM);
    					       
    					       //Text field
    					       JTextField txtLM = new JTextField();
    					       LengthCalc.add(txtLM);
    					       
    					       //Label
    					       JLabel lblLYrds = new JLabel();
    					       lblLYrds.setText("Yards");
    					       LengthCalc.add(lblLYrds);
    					       
    					       //Text field
    					       JTextField txtLYrds = new JTextField();
    					       LengthCalc.add(txtLYrds);
    					       
    					       //Label
    					       JLabel lblLFt = new JLabel();
    					       lblLFt.setText("Feet");
    					       LengthCalc.add(lblLFt);
    					       
    					       //Text field
    					       JTextField txtLFt = new JTextField();
    					       LengthCalc.add(txtLFt);
    					       
    					       //Label
    					       JLabel lblLIn = new JLabel();
    					       lblLIn.setText("Inches");
    					       LengthCalc.add(lblLIn);
    					       
    					       //Text field
    					       JTextField txtLIn = new JTextField();
    					       LengthCalc.add(txtLIn);
    					       
    		// -------------- End of Controls		       
    				       
           //This adds the first and second tab to our tabbed pane object and names it
           jtp.addTab("General", Splash);
           jtp.addTab("Pools", Pools);
           jtp.addTab("Hot Tubs", HotTubs);
           jtp.addTab("Settings", Settings);
           jtp.addTab("Customers", Customers);
           jtp.addTab("Employees", Employees);
           jtp.addTab("Temp Calc", TempCalc);
           jtp.addTab("Length Calc", LengthCalc);
    
            //This creates a new button called "Press" and adds it to the second tab
           //JButton test = new JButton("Press");
          // Pools.add(test);
    
            //This is an Action Listener which reacts to clicking on 
            //the test button called "Press"
           // ButtonHandler phandler = new ButtonHandler();
           // test.addActionListener(phandler);
           // setVisible(true); //otherwise you won't "see" it 
        }
        
        //This is the internal class that defines what the above Action Listener
        //will do when the test button is pressed.
        class ButtonHandler implements ActionListener{
               public void actionPerformed(ActionEvent e){
                       JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
               }
        }
    
        //example usage
         public static void main (String []args){
            FinalProject tab = new FinalProject();
            tab.setVisible(true);
            tab.setSize(350, 450);
        }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your options for a tabular output include using GridBagLayout vs. a combination of simpler layouts vs. alternative layout such as MiGLayout. An example of use of GridBagLayout:

    Java Code:
    import java.awt.*;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.*;
    
    public class AppExample2Gui {
       public static final String[] FIELD_NAMES = {"Name", "Address", "Phone", "Date of Birth"};
       public static final String[] COLOR_TEXTS = {"Red", "Blue", "Green", "Yellow"};
       private static final int GAP = 8;
       private static final int FIELD_SIZE = 15;
       private JPanel mainPanel = new JPanel();
       // to extract JTextField based on JLabel String as key
       private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
       private JButton enterBtn = new JButton("Enter");
       private JButton clearBtn = new JButton("Clear");
       private JButton exitBtn = new JButton("Exit");
    
       public AppExample2Gui() {
          JPanel midPanel = new JPanel(new GridBagLayout());
          GridBagConstraints gpc = createGpc(0, 0);
    
          int rowIndex = 0;
          for (rowIndex = 0; rowIndex < FIELD_NAMES.length; rowIndex++) {
             gpc = createGpc(0, rowIndex);
             midPanel.add(new JLabel(FIELD_NAMES[rowIndex]), gpc);
             JTextField field = new JTextField(FIELD_SIZE);
             gpc = createGpc(1, rowIndex);
             midPanel.add(field, gpc);
             fieldMap.put(FIELD_NAMES[rowIndex], field);
          }
    
          JPanel colorPanel = new JPanel(new GridLayout(1, 0, 5, 0));
          for (String colorText : COLOR_TEXTS) {
             JCheckBox cBox = new JCheckBox(colorText);
             cBox.setActionCommand(colorText);
             colorPanel.add(cBox);
          }
          gpc = createGpc(0, rowIndex);
          midPanel.add(new JLabel("Colors:"), gpc);
          gpc = createGpc(1, rowIndex);
          midPanel.add(colorPanel, gpc);
    
          rowIndex++;
    
          JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
          btnPanel.add(enterBtn);
          btnPanel.add(clearBtn);
          btnPanel.add(exitBtn);
          gpc = createGpc(0, rowIndex);
          midPanel.add(new JLabel(" "), gpc);
          gpc = createGpc(1, rowIndex);
          midPanel.add(btnPanel, gpc);
    
          mainPanel.setLayout(new BorderLayout(GAP, GAP));
          mainPanel.add(midPanel, BorderLayout.NORTH);
       }
    
       private GridBagConstraints createGpc(int x, int y) {
          GridBagConstraints gpc = new GridBagConstraints();
          gpc.gridx = x;
          gpc.gridy = y;
          gpc.gridheight = 1;
          gpc.gridwidth = 1;
          gpc.weighty = 0.0;
    
          if (x == 0) {
             gpc.weightx = 0.01;
             gpc.anchor = GridBagConstraints.WEST;
             gpc.fill = GridBagConstraints.BOTH;
             gpc.insets = new Insets(GAP, GAP, GAP, GAP);
          } else {
             gpc.weightx = 1.0;
             gpc.anchor = GridBagConstraints.EAST;
             gpc.fill = GridBagConstraints.HORIZONTAL;
             gpc.insets = new Insets(GAP, 2 * GAP, GAP, GAP);
          }
    
          return gpc;
       }
    
       public JPanel getMainPanel() {
          return mainPanel;
       }
       
       private static void createAndShowUI() {
          JFrame frame = new JFrame("AppExample2");
          frame.getContentPane().add(new AppExample2Gui().getMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    For more on this, please see the Oracle Swing tutorials GridBagLayout section.

  3. #3
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Thanks for the example but i'm not fully comprehending it.

    How does the variable rowIndex come into play? It looks like it helps in constructing the GUI but i'm not sure what it's doing.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    In my example, row index is simply the row of the tabular structure (JLabel followed by JTextField) in the gui that I'm creating. It goes from 0, the first JLabel/JTextField pair to 3 in my example above since there are 4 label/field pairs in the example. The reason it's there is that the GridBagConstraints will want to know the x and y location of each component that uses them. Again, read the GridBagLayout tutorial for the details and then play with it some yourself.
    Last edited by Fubarable; 08-16-2011 at 06:31 PM.

Similar Threads

  1. Tabbed pane
    By sonal12 in forum AWT / Swing
    Replies: 13
    Last Post: 09-15-2010, 12:22 AM
  2. tabbed Pane .
    By programmer_007 in forum AWT / Swing
    Replies: 2
    Last Post: 09-03-2009, 08:52 PM
  3. JTree and Tabbed Pane
    By paluee in forum AWT / Swing
    Replies: 1
    Last Post: 03-30-2009, 06:03 AM
  4. Tabbed pane using struts 2.x......?
    By prabhurangan in forum Web Frameworks
    Replies: 1
    Last Post: 07-19-2008, 06:48 AM
  5. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •