File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes IllegalArgumentException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "IllegalArgumentException" Watch "IllegalArgumentException" New topic
Author

IllegalArgumentException

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Hello, I am fairly new to Java, I'm still in school learning it. I'm trying to run a program that is giving me this runtime error. I don't understand what it is telling me. I have been working on this program for 2 days and it was working fine until last night when I got this exception. HELP!
Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class StockApplication
{
public static void main(String[] args)
{
StockFrame sframe = new StockFrame();
sframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sframe.show();
}
}
/**
A frame that contains a panel with buttons and textboxes
**/
class StockFrame extends JFrame
{
public static final int WIDTH = 700; //set frame at 400 pixels wide
public static final int HEIGHT = 500; //set frame at 200 pixels tall
StockPanel sPanel; //panel to contain button and textbox
TopPanel tPanel;
GuiPanel gPanel;
fourthPanel xPanel;
fifthPanel yPanel;
sixthPanel zPanel;
ErrorPanel ePanel;
public JTextField Shares; //textbox to contain share amount
public JTextField DateField; //textbox to contain date
public JTextField Dollars; //textbox to contain dollars
public JTextField TotalPrice; //textbox to contain total price
public JLabel ErrorMsg;

public StockFrame()
{
setTitle("Stock Portfolio");
setSize(WIDTH, HEIGHT);
// create and add panels to frame
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
tPanel = new TopPanel();
sPanel = new StockPanel();
gPanel = new GuiPanel();
xPanel = new fourthPanel();
yPanel = new fifthPanel();
zPanel = new sixthPanel();
ePanel = new ErrorPanel();
contentPane.add(tPanel, FlowLayout.CENTER);
contentPane.add(sPanel, FlowLayout.LEFT);
contentPane.add(gPanel, FlowLayout.LEFT);
contentPane.add(xPanel, FlowLayout.LEFT);
contentPane.add(yPanel, FlowLayout.LEFT);
contentPane.add(zPanel, FlowLayout.LEFT);
contentPane.add(ePanel, FlowLayout.CENTER);
}
private class TopPanel extends JPanel
{
//Declare Clear, Accept, and Exit buttons
public JButton ClearButton;
public JButton AcceptButton;
public JButton ExitButton;
//no args constructor for TopPanel
public TopPanel()
{
//create and add buttons, textboxes, and labels
ClearButton = new JButton("Clear");
add(ClearButton);
AcceptButton = new JButton("Accept");
add(AcceptButton);
ExitButton = new JButton("Exit");
add(ExitButton);
ClearButton.addActionListener(new ButtonListener());
AcceptButton.addActionListener(new ButtonListener());
ExitButton.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//closes the window when the exit button is pressed
if (e.getActionCommand() == "Exit")
System.exit(0);
//clears # of shares, date, dollars and total share price
if(e.getActionCommand() == "Clear")
{
Shares.setText(null);
Dollars.setText(null);
DateField.setText(null);
TotalPrice.setText(null);
}
//calculates the Total Price by multiplying # of shares by dollars
//or displays an error message if one of these is missing
int shr = Integer.valueOf(Shares.getText()).intValue();
float dlr = Float.valueOf(Dollars.getText()).intValue();
float ttlprice;
if (e.getActionCommand() == "Accept")
{
if (Shares.getText() == null)
ErrorMsg.setText("Share are missing.");
else if (Dollars.getText() == null)
ErrorMsg.setText("Dollars are missing.");
else if (DateField.getText() == null)
ErrorMsg.setText("Date is missing.");
else
{
ttlprice = shr * dlr;
TotalPrice.setText(Float.toString(ttlprice));
}
}
}
}
}

private class StockPanel extends JPanel
{
public JComboBox StockName; //Combo box to allow the user to select the stock name
public JTextField Symbol; //corresponding Exchange symbol will appear in this box
//no args constructor
public StockPanel()
{
//create and add buttons, textboxes, and labels
add(new JLabel("Stock Name"));
StockName = new JComboBox();
StockName.setEditable(true);
StockName.addItem("American Electric Power Company, Inc");
StockName.addItem("Archer Daniels Midland Company");
StockName.addItem("AT&T");
StockName.addItem("ChevronTexaco Corporation");
StockName.addItem("Cisco Systems");
StockName.addItem("Intel Corporation");
StockName.addItem("General Electric Company");
StockName.addItem("Global Crossing Ltd");
StockName.addItem("K Mart Corporation");
StockName.addItem("Microsoft Corporation");
StockName.addItem("Palm Inc");
StockName.addItem("Providian Financial Corporation");
StockName.addItem("Qwest Communications International");
add(StockName);
add(new JLabel("Symbol"));
Symbol = new JTextField(10);
Symbol.setEditable(false);
add(Symbol);

StockName.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//set the symbol according to the stock name
if (StockName.getSelectedItem() == "American Electric Power Company")
Symbol.setText("AEP");
else if (StockName.getSelectedItem() == "Archer Daniels Midland Company")
Symbol.setText("ADM");
else if (StockName.getSelectedItem() == "AT&T")
Symbol.setText("T");
else if (StockName.getSelectedItem() == "ChevronTexaco Corporation")
Symbol.setText("CVX");
else if (StockName.getSelectedItem() == "Cisco Systems Inc")
Symbol.setText("CSCO");
else if (StockName.getSelectedItem() == "Intel Corporation")
Symbol.setText("INTC");
else if (StockName.getSelectedItem() == "General Electric Company")
Symbol.setText("GE");
else if (StockName.getSelectedItem() == "Global Crossing Ltd")
Symbol.setText("GX");
else if (StockName.getSelectedItem() == "K Mart Corporation")
Symbol.setText("KM");
else if (StockName.getSelectedItem() == "Microsoft Corporation")
Symbol.setText("MSFT");
else if (StockName.getSelectedItem() == "Palm Inc")
Symbol.setText("PALM");
else if (StockName.getSelectedItem() == "Providian Financial Corporation")
Symbol.setText("PVN");
else if (StockName.getSelectedItem() == "Qwest Communications International Inc")
Symbol.setText("Q");
}
});
}
}
private class GuiPanel extends JPanel
{
public JButton Bought; //not used right now
public JButton Sold; //not used right now
public JComboBox Exchange; //combo box for the user to select the exchange
//no args constructor
public GuiPanel()
{
add(new JLabel("Exchange"));
Exchange = new JComboBox();
Exchange.setEditable(true);
Exchange.addItem("American");
Exchange.addItem("London");
Exchange.addItem("NASDAQ");
Exchange.addItem("New York");
Exchange.addItem("Tokyo");
Exchange.addItem("Toronto");
add(Exchange);
Bought = new JButton("Bought");
add(Bought);
Sold = new JButton("Sold");
add(Sold);

}
}
private class fourthPanel extends JPanel
{
//no args constructor
public fourthPanel()
{
add(new JLabel("Share"));
Shares = new JTextField(10);
add(Shares);
add(new JLabel("Date"));
DateField = new JTextField(10);
add(DateField);
}
}

private class fifthPanel extends JPanel
{
public JTextField Commission; //textbox to contain commission
//no args constructor
public fifthPanel()
{
add(new JLabel("Dollars"));
Dollars = new JTextField(10);
add(Dollars);
add(new JLabel("Commission"));
Commission = new JTextField(10);
add(Commission);
add(new JLabel("TotalPrice"));
TotalPrice = new JTextField(10);
add(TotalPrice);
add(new JLabel("Brokerage"));
}
}
private class sixthPanel extends JPanel
{
public JComboBox Brokerage; //combo box for the user to select the brokerage
public JTextField Phone; //textbox to contain phone number
//no args constructor
public sixthPanel()
{
Brokerage = new JComboBox();
Brokerage.setEditable(true);
Brokerage.addItem("Johann Bach");
Brokerage.addItem("William Tell");
Brokerage.addItem("Paine-Weber");
Brokerage.addItem("Kathryn Swynford");
Brokerage.addItem("Schwab");
add(Brokerage);
add(new JLabel("Phone"));
Phone = new JTextField(10);
add(Phone);
add(new JLabel("Candy Bortniker"));
Brokerage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//set the phone number according to the brokerage
if (Brokerage.getSelectedItem() == "Johann Bach")
Phone.setText("816-111-5555");
else if (Brokerage.getSelectedItem() == "William Tell")
Phone.setText("916-222-5555");
else if (Brokerage.getSelectedItem() == "Paine-Weber")
Phone.setText("816-333-5555");
else if (Brokerage.getSelectedItem() == "Kathryn Synford")
Phone.setText("913-444-5555");
else if (Brokerage.getSelectedItem() == "Schwab")
Phone.setText("913-555-4343");
}
});
}
}
private class ErrorPanel extends JPanel
{
public JLabel ErrorLabel; //label to hold error messages
//no args constructor
public ErrorPanel()
{
ErrorMsg = new JLabel("ErrorMsg");
add(ErrorMsg);
}
}
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The problem is in all these lines -


That isn't how you use FlowLayout...

If you remove all the FlowLayout. arguments everything works fine...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Candy,
Welcome to JavaRanch. Let me guess, the code you added was an attempt to position the components.
Your problem is here:

Unfortunately FlowLayout is a one-trick pony and generally a poor choice for overall GUI layout. You would be better off sticking with the default BorderLayout for JFrame for this app and strategically place your components in the NORTH SOUTH, EAST, WEST and CENTER locations. For greatest control over layout you can use a GridBagLayout, which strikes fear in the hearts of most novice Java developers due to its relative complexity.
One further critique, if I may, you have a lot of code like:

Using the == operator on Strings (or any other object for that matter) is a bug waiting to happen. It's important to realize that what you are doing is comparing not the content (or state) of the Strings here but of their references. In your case all is probably well since you are using String literals throughout. String literals go into a String pool, so identical String values have the same reference. You should always use the equals(Object o) for object state comparisons. So the above code would then be:

Hope this helps,
Michael Morris
[ March 17, 2003: Message edited by: Michael Morris ]
[ March 17, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Thank you so much for your help. I've been trying BorderLayout, which is what my instructor suggested, but the panels overlap each other. What is on the west overlaps what is on the East. If 2 panels are on the west you only see the last one. Should I widen the frame, shorten the length of what is on the panels, or is there a way to get it to drop down a line?
I changed my if statements to what you suggested. I'll take any suggestions you have.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Candy,
My suggestion is to take a little time to understand the behavior of each of the major layout managers: FlowLayout, BorderLayout and GridLayout. With some combination of those three you can pretty much get any component positioning you want. Once you have that down you can then tackle the omnipotent GridBagLayout. With it you have near total control over component position but it is fairly complex.
What I have always done when designing GUI layout is take an 11x17 dry erase board (you can also use paper and pencil) and roughly sketch the component positions that I want. Then I determine what combination of layouts will accomplish it.

I've been trying BorderLayout, which is what my instructor suggested, but the panels overlap each other. What is on the west overlaps what is on the East.

What have you put in the CENTER? The EAST and WEST components in a BorderLayout work by taking just enough room horizontally to fit and all the verical space minus the width of the NORTH and SOUTH components. If you don't have a CENTER component then the EAST and WEST shall meet (contrary to the old adage (n'er the twain...). The NORTH and SOUTH components work by taking just enough room to verically fit and all the horizontal space. The greed CENTER takes whatever is left over horizontally and vertically.

If 2 panels are on the west you only see the last one. Should I widen the frame, shorten the length of what is on the panels, or is there a way to get it to drop down a line?

What you are probably doing is something like:

The first statement does nothing because the second statement replaces the WEST component with panel2. Can you see where behavior like that might come in handy? (Hint: dynamic component interchange)
Now if you do something like this:

Then you should see both panels in the WEST.
Hope this helps,
Michael Morris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalArgumentException