473,662 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tabbed pane problem

23 New Member
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 2222
Laharl
849 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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 New Member
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?
Sep 6 '08 #5
Laharl
849 Recognized Expert Contributor
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).
Sep 6 '08 #6
HxRLxY
23 New Member
Well, I finally figured it out. I was missing an extends JPanel modifier. Thanks for the help.
Sep 6 '08 #7
JosAH
11,448 Recognized Expert MVP
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
1895
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 JSplitPane. There is a JScrollPane in each side. There's a JTable in each scroll pane. The left scroll pane has the vertical JScrollBar on the left side. The right scroll pane has the veritcal JScrollBar on the right side (default).
19
2970
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 provide an open TD with a DIV in it for the content of this formlet. (The DIV is for DHTML to hide and show the content) I've created a web page showing step by step the two problems I'm encountering. This problem is much easier to see than it...
0
1227
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 combo box nor a button will work after clicking in the list box. There are no events associated with the list box. This is a mystery to me. What could be causing this behavior? What could be the remedy?
0
7507
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 it is all one form, references to contols on any page are done in the same manner as if they were all on one single screen. Create a form in Design View. Goto the toolbox and click on the Tabbed Control icon; it actually looks like several...
0
1218
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 code, Option Explicit
3
3078
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 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w4.org/1999/xhtml" xml:lang="nl" lang="nl"> <head> <title>Tabbed display</title> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> <style>
11
2614
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 customer name etc), this is used as the Primary key to all the tables relating to that customer. I've been using 1 to Many relationships for most of the tables (e.g A customer can have many addresses) with Referential Integrity and cascading turned...
0
1492
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 read the actual message in the "bottom" reading pane format is to double click the message and read it in full view. This all happened when I accidentally fiddled with the divider between the messages list and the reading pane (I believe this is...
0
1554
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
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.