473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Tabbed pane problem

23
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:

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2.    import javax.swing.JOptionPane.*;
  3.    import javax.swing.JTabbedPane.*;
  4.    import java.awt.*;
  5.    import java.awt.event.*;
  6.  
  7.     public class WeightConverter extends JPanel{
  8.  
  9.           //main method will instantiate and display GUI
  10.  
  11.        public static void main(String[] args){
  12.  
  13.          JFrame frame = new JFrame("Weight Converter");
  14.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16.          JTabbedPane pane = new JTabbedPane();
  17.          ConvertToOuncesPane panel_1 = new ConvertToOuncesPane();   
  18.  
  19.          pane.addTab("Convert", panel_1);
  20.  
  21.          frame.getContentPane().add(pane);
  22.          frame.pack();
  23.          frame.setVisible(true);
  24.       }
  25.       //ConvertToOuncesPane class will create the pane for converting to ounces. 
  26.        static class ConvertToOuncesPane{
  27.  
  28.           //Set up new component identifiers
  29.          JLabel tonsLabel, stonesLabel, poundsLabel, ouncesLabel, resultLabel;
  30.          JTextField tonsField, stonesField, poundsField, ouncesField;
  31.          JButton btnConvert, btnClear, btnExit;
  32.          String resultString = "Total Number of Ounces: ";
  33.  
  34.           /*****************************************************************************\
  35.           |
  36.           |this method will be the primary constructor method for the class
  37.           |
  38.           \*****************************************************************************/
  39.           public ConvertToOuncesPane(){
  40.  
  41.              //set up the labels for the input boxes
  42.             tonsLabel = new JLabel("Enter number of tons:");
  43.             stonesLabel = new JLabel("Enter number of stones:");
  44.             poundsLabel = new JLabel("Enter number of pounds:");
  45.             ouncesLabel = new JLabel("Enter number of pounds:");
  46.             resultLabel = new JLabel(resultString + "---");
  47.  
  48.              //set up the input boxes
  49.             tonsField = new JTextField(10);
  50.             stonesField = new JTextField(10);
  51.             poundsField = new JTextField(10);
  52.             ouncesField = new JTextField(10);
  53.  
  54.              //set up the convert button and add action listener
  55.             btnConvert = new JButton("Convert");
  56.             btnConvert.addActionListener (new ConvertToListener());
  57.  
  58.              //set up the clear button and add action listener
  59.             btnClear = new JButton("Clear");
  60.             btnClear.addActionListener (new ClearToListener());
  61.  
  62.              //set up the exit button and add action listener
  63.             btnExit = new JButton("Exit");
  64.             btnExit.addActionListener (new ExitToListener());
  65.  
  66.              //add components to the pane
  67.             JPanel panel = new JPanel();
  68.             panel.add (tonsLabel);
  69.             panel.add (tonsField);
  70.             panel.add (stonesLabel);
  71.             panel.add (stonesField);
  72.             panel.add (poundsLabel);
  73.             panel.add (poundsField);
  74.             panel.add (ouncesLabel);
  75.             panel.add (ouncesField);
  76.             panel.add (resultLabel);
  77.             panel.add (btnConvert);
  78.             panel.add (btnClear);
  79.             panel.add (btnExit);
  80.  
  81.              //set default values for textFields
  82.             tonsField.setText("0");
  83.             stonesField.setText("0");
  84.             poundsField.setText("0");
  85.             ouncesField.setText("0");
  86.  
  87.              //set up the look of the pane
  88.             panel.setBackground(Color.white);
  89.             panel.setPreferredSize(new Dimension(200, 280));
  90.  
  91.  
  92.  
  93.          }
  94.  
  95.         /**********************************************************************\
  96.         |
  97.         |the ConvertToListener class is dedicated to the convert button 
  98.         |on the Convert To Ounces Pane
  99.         |
  100.         \**********************************************************************/ 
  101.           private class ConvertToListener implements ActionListener{
  102.  
  103.              public void actionPerformed(ActionEvent event){
  104.  
  105.                 //set up variables for the ConvertListener class
  106.                int tons = 0, stones = 0, pounds = 0, ounces = 0, totalOunces = 0;
  107.                final int CONVERT_TONS = 32000, CONVERT_STONES = 224, CONVERT_POUNDS = 16;
  108.                String errorMessage = "You must enter a number.";
  109.                boolean testField;
  110.  
  111.                 //collect, test, and parse integers from each textField
  112.                    //test tonsField
  113.                testField = testInt(tonsField.getText());
  114.  
  115.                if (testField == true)
  116.                   tons = Integer.parseInt(tonsField.getText());
  117.                else
  118.                   JOptionPane.showMessageDialog(null, errorMessage);
  119.  
  120.                     //test stonesField
  121.                testField = testInt(stonesField.getText());
  122.  
  123.                if (testField == true)
  124.                   stones = Integer.parseInt(stonesField.getText());
  125.                else
  126.                   JOptionPane.showMessageDialog(null, errorMessage);
  127.  
  128.                     //test poundsField
  129.                testField = testInt(poundsField.getText());
  130.  
  131.                if (testField == true)
  132.                   pounds = Integer.parseInt(poundsField.getText());
  133.                else
  134.                   JOptionPane.showMessageDialog(null, errorMessage);
  135.  
  136.                     //test ouncesField
  137.                testField = testInt(ouncesField.getText());
  138.  
  139.                if (testField == true)
  140.                   ounces = Integer.parseInt(ouncesField.getText());
  141.                else
  142.                   JOptionPane.showMessageDialog(null, errorMessage);
  143.  
  144.                 //perform the conversion calculation
  145.                totalOunces = tons * CONVERT_TONS + stones * CONVERT_STONES + pounds * CONVERT_POUNDS + ounces;
  146.  
  147.                 //display the result
  148.                resultLabel.setText(resultString + totalOunces);    
  149.             }
  150.  
  151.              //set up a try-catch in the event a letter is entered into a field
  152.              boolean testInt(String test){
  153.                try{
  154.                   Integer.parseInt(test);
  155.                }
  156.                    catch(NumberFormatException exception){
  157.                      return false;
  158.                   }
  159.                return true;
  160.             }
  161.  
  162.  
  163.  
  164.  
  165.          }
  166.          /***************************************************************************\
  167.           |
  168.           |the ClearToListener class is dedicated to the Clear button on 
  169.           |the Convert To Ounces Pane
  170.           |
  171.           \***************************************************************************/
  172.           private class ClearToListener implements ActionListener{
  173.  
  174.               /****************************************************************\
  175.               |
  176.               |this method will restore the default zeros to all text fields
  177.               |
  178.               \****************************************************************/
  179.              public void actionPerformed(ActionEvent event){
  180.                  //restore default values to textFields
  181.                tonsField.setText("0");
  182.                stonesField.setText("0");
  183.                poundsField.setText("0");
  184.                ouncesField.setText("0");
  185.  
  186.             }
  187.          }
  188.          /*******************************************************************************\
  189.           |
  190.           |the ExitToListener class is dedicated to the Exit button on the
  191.           |Convert To Ounces Pane
  192.           |
  193.           \*******************************************************************************/
  194.  
  195.           private class ExitToListener implements ActionListener{
  196.              /***********************************************************\
  197.              |
  198.              |this method will end the program when the user 
  199.              |clicks the exit button
  200.              |
  201.              \***********************************************************/    
  202.              public void actionPerformed(ActionEvent event){
  203.                System.exit(0);
  204.             }
  205.          }
  206.  
  207.       }
  208.  
  209.  
  210.  
  211.  
  212.    }
  213.  
Sep 6 '08 #1
7 2210
Laharl
849 Expert 512MB
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.
Sep 6 '08 #2
HxRLxY
23
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.
Sep 6 '08 #3
Laharl
849 Expert 512MB
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.
Sep 6 '08 #4
HxRLxY
23
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("Convert", 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?
Sep 6 '08 #5
Laharl
849 Expert 512MB
Your ConvertToOuncesPane 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).
Sep 6 '08 #6
HxRLxY
23
Well, I finally figured it out. I was missing an extends JPanel modifier. Thanks for the help.
Sep 6 '08 #7
JosAH
11,448 Expert 8TB
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
Sep 6 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Don Leckie | last post by:
Hi, I'm upgrading our software from JDK 1.3.1_03 to JDK 1.4.2_07 and I'm having trouble with ComponentOrientation.RIGHT_TO_LEFT working correctly in a JScrollPane. One of my windows has a...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
0
by: dbuchanan | last post by:
Hello, The actions pane locks up after selecting a listbox. I have tried all kinds of things and have narrowed it down to this. If I click in the listbox the action pane locks up. Neither a...
0
NeoPa
by: NeoPa | last post by:
Originally posted by Missinglinq: The first thing to remember is that Tabbed Pages are all part of a single form; think of it as a really long form turned on its side and folded on itself. Because...
0
by: padhuwork | last post by:
Hi, I have an issue. In my Vb6 Project, I have added a Tabbed Dialog Control (SSTab). At runtime, I would like to controls to my Tab panes. How do I do it? I have 2 Tab panes. Below is my...
3
by: Sjef Janssen | last post by:
I try to get a tabbed display work both in IE (6 and 7) and Firefox. The last does give a problem. What's going wrong here? I'm totally puzzled. <!doctype html PUBLIC "-//W3C//DTD XHTML 1.0...
11
by: AndyM | last post by:
Hi, I have a curious problem that is causing me large amounts of grief and is steadily turning me grey. Hopefully you guys can help. I have a Master table that contains a CustomerID (as well as...
0
by: =?Utf-8?B?YmFtbm9sYQ==?= | last post by:
When using Windows Live Hotmail and the "bottom" reading pane option chosen, the messages list shows up but no message, not even when I (single) click on the message. The only way to open and...
0
Sl1ver
by: Sl1ver | last post by:
Does anyone know how to cutomize the look of a standard tabbed pane or know of any other free tabbed pane .dll's i can use?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.