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: -
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);
-
}
-
}
-
-
}
-
-
-
-
-
}
-
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.
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.
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.
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?
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).
Well, I finally figured it out. I was missing an extends JPanel modifier. Thanks for the help.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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).
|
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...
|
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?
|
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...
|
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
| |
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>
|
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...
|
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...
|
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?
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |