Tabbed pane problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HxRLxY
    New Member
    • Sep 2008
    • 23

    #1

    Tabbed pane problem

    I posted a different question (Help with non-static/static problem) which was answered. I changed my inner class to a static nested class, but now I cannot create an object using that class and add it to the JTabbedPane. I get a 'cannot find symbol' error, and it points to the pane.addTab method. The program works fine when I remove the nested class and make it a separate class, then instantiate it. Any help would be greatly appreciated.

    Here is my source code:

    Code:
    import javax.swing.*;
       import javax.swing.JOptionPane.*;
       import javax.swing.JTabbedPane.*;
       import java.awt.*;
       import java.awt.event.*;
    
        public class WeightConverter extends JPanel{
        
         	 //main method will instantiate and display GUI
       
           public static void main(String[] args){
          
             JFrame frame = new JFrame("Weight Converter");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          	
             JTabbedPane pane = new JTabbedPane();
             ConvertToOuncesPane panel_1 = new ConvertToOuncesPane();   
             
             pane.addTab("Convert", panel_1);
          	
             frame.getContentPane().add(pane);
             frame.pack();
             frame.setVisible(true);
          }
          //ConvertToOuncesPane class will create the pane for converting to ounces. 
           static class ConvertToOuncesPane{
          	
          	//Set up new component identifiers
             JLabel tonsLabel, stonesLabel, poundsLabel, ouncesLabel, resultLabel;
             JTextField tonsField, stonesField, poundsField, ouncesField;
             JButton btnConvert, btnClear, btnExit;
             String resultString = "Total Number of Ounces: ";
          	
          	/*****************************************************************************\
          	|
          	|this method will be the primary constructor method for the class
          	|
          	\*****************************************************************************/
              public ConvertToOuncesPane(){
             
             	//set up the labels for the input boxes
                tonsLabel = new JLabel("Enter number of tons:");
                stonesLabel = new JLabel("Enter number of stones:");
                poundsLabel = new JLabel("Enter number of pounds:");
                ouncesLabel = new JLabel("Enter number of pounds:");
                resultLabel = new JLabel(resultString + "---");
                
             	//set up the input boxes
                tonsField = new JTextField(10);
                stonesField = new JTextField(10);
                poundsField = new JTextField(10);
                ouncesField = new JTextField(10);
             	
             	//set up the convert button and add action listener
                btnConvert = new JButton("Convert");
                btnConvert.addActionListener (new ConvertToListener());
             	
             	//set up the clear button and add action listener
                btnClear = new JButton("Clear");
                btnClear.addActionListener (new ClearToListener());
             	
             	//set up the exit button and add action listener
                btnExit = new JButton("Exit");
                btnExit.addActionListener (new ExitToListener());
                
             	//add components to the pane
                JPanel panel = new JPanel();
                panel.add (tonsLabel);
                panel.add (tonsField);
                panel.add (stonesLabel);
                panel.add (stonesField);
                panel.add (poundsLabel);
                panel.add (poundsField);
                panel.add (ouncesLabel);
                panel.add (ouncesField);
                panel.add (resultLabel);
                panel.add (btnConvert);
                panel.add (btnClear);
                panel.add (btnExit);
                
             	//set default values for textFields
                tonsField.setText("0");
                stonesField.setText("0");
                poundsField.setText("0");
                ouncesField.setText("0");
             	
             	//set up the look of the pane
                panel.setBackground(Color.white);
                panel.setPreferredSize(new Dimension(200, 280));
             	  
             	
             
             }
          
            /**********************************************************************\
            |
            |the ConvertToListener class is dedicated to the convert button 
            |on the Convert To Ounces Pane
            |
            \**********************************************************************/ 
              private class ConvertToListener implements ActionListener{
             
                 public void actionPerformed(ActionEvent event){
                
                	//set up variables for the ConvertListener class
                   int tons = 0, stones = 0, pounds = 0, ounces = 0, totalOunces = 0;
                   final int CONVERT_TONS = 32000, CONVERT_STONES = 224, CONVERT_POUNDS = 16;
                   String errorMessage = "You must enter a number.";
                   boolean testField;
                
                	//collect, test, and parse integers from each textField
                   	//test tonsField
                   testField = testInt(tonsField.getText());
                	
                   if (testField == true)
                      tons = Integer.parseInt(tonsField.getText());
                   else
                      JOptionPane.showMessageDialog(null, errorMessage);
                		
                		//test stonesField
                   testField = testInt(stonesField.getText());
                	
                   if (testField == true)
                      stones = Integer.parseInt(stonesField.getText());
                   else
                      JOptionPane.showMessageDialog(null, errorMessage);
                		
                		//test poundsField
                   testField = testInt(poundsField.getText());
                	
                   if (testField == true)
                      pounds = Integer.parseInt(poundsField.getText());
                   else
                      JOptionPane.showMessageDialog(null, errorMessage);
                		
                		//test ouncesField
                   testField = testInt(ouncesField.getText());
                	
                   if (testField == true)
                      ounces = Integer.parseInt(ouncesField.getText());
                   else
                      JOptionPane.showMessageDialog(null, errorMessage);
                		
                	//perform the conversion calculation
                   totalOunces = tons * CONVERT_TONS + stones * CONVERT_STONES + pounds * CONVERT_POUNDS + ounces;
                	
                	//display the result
                   resultLabel.setText(resultString + totalOunces);	
                }
             	
             	//set up a try-catch in the event a letter is entered into a field
                 boolean testInt(String test){
                   try{
                      Integer.parseInt(test);
                   }
                       catch(NumberFormatException exception){
                         return false;
                      }
                   return true;
                }
             	
             	
             	
             
             }
             /***************************************************************************\
          	|
          	|the ClearToListener class is dedicated to the Clear button on 
          	|the Convert To Ounces Pane
          	|
          	\***************************************************************************/
              private class ClearToListener implements ActionListener{
             	 
             	 /****************************************************************\
             	 |
             	 |this method will restore the default zeros to all text fields
             	 |
             	 \****************************************************************/
                 public void actionPerformed(ActionEvent event){
                 	//restore default values to textFields
                   tonsField.setText("0");
                   stonesField.setText("0");
                   poundsField.setText("0");
                   ouncesField.setText("0");
                	
                }
             }
             /*******************************************************************************\
          	|
          	|the ExitToListener class is dedicated to the Exit button on the
          	|Convert To Ounces Pane
          	|
          	\*******************************************************************************/
          	
              private class ExitToListener implements ActionListener{
             	/***********************************************************\
             	|
             	|this method will end the program when the user 
             	|clicks the exit button
             	|
             	\***********************************************************/	
                 public void actionPerformed(ActionEvent event){
                   System.exit(0);
                }
             }
          	
          }
          	
          
          
          
       }
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Originally posted by HxRLxY
    The program works fine when I remove the nested class and make it a separate class, then instantiate it.
    Sounds to me like you've solved your own problem.

    Comment

    • HxRLxY
      New Member
      • Sep 2008
      • 23

      #3
      Originally posted by Laharl
      Sounds to me like you've solved your own problem.
      The only problem with that solution is that the program is a lab project and must be submitted as one file. So I can't write a separate class file, I have to have them all nested.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Rather than placing the second class in a separate file, keep it in the same file. A file may have multiple classes, but only one public class. Thus, simply omit the public keyword from the definition of the second class.

        Comment

        • HxRLxY
          New Member
          • Sep 2008
          • 23

          #5
          Originally posted by Laharl
          Rather than placing the second class in a separate file, keep it in the same file. A file may have multiple classes, but only one public class. Thus, simply omit the public keyword from the definition of the second class.
          I removed the second class from the primary class as you suggested. But I still get the cannot find symbol error at pane.addTab("Co nvert", panel_1) . I can add the tab if I use a JComponent (e.g. JButton), but I cant add the panel_1 object. Any ideas?

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Your ConvertToOunces Pane class is not a JComponent, so it can't be added as a tab. You need to extend JComponent and implement any necessary methods (I don't think there are any, but if there are the compiler will tell you when you don't).

            Comment

            • HxRLxY
              New Member
              • Sep 2008
              • 23

              #7
              Well, I finally figured it out. I was missing an extends JPanel modifier. Thanks for the help.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by HxRLxY
                Well, I finally figured it out. I was missing an extends JPanel modifier. Thanks for the help.
                That's what I wrote already in your other thread. You should
                read the answers given to you.

                kind regards,

                Jos

                Comment

                Working...