Please Help - Need Help with Buttons for GUI for assignment. URGENT!!

Can someone please help me with the buttons on this program? I cannot figure out how to get them to work.

Thanks!!!

import java.awt.*;

import java.awt.event.*;

import java.text.NumberFormat;

import javax.swing.*;

import javax.swing.JButton;

publicclass InventoryTAHimplements ActionListener

{

Maker[] proMaker;

JTextField[] fields;

NumberFormat nf;

publicvoid actionPerformed(ActionEvent e)

{

int index = ((JComboBox)e.getSource()).getSelectedIndex();

populateFields(index);

}

publicstaticvoid main(String[] args)

{

try

{

UIManager.setLookAndFeel(

"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch (Exception e)

{

System.err.println(e.getClass().getName() +": " + e.getMessage());

}

InventoryTAH test =new InventoryTAH();

test.initMakers();

test.showGUI();

test.populateFields(0);

}

privatevoid initMakers(){

proMaker =new Maker[10];

proMaker[0] =new Maker( 1,"Pens",1.59,100,"Bic");

proMaker[1] =new Maker( 2,"Pencils", .65, 100,"Mead");

proMaker[2] =new Maker( 3,"Markers", 1.29, 100,"Sharpie");

proMaker[3] =new Maker( 4,"Paperclips", 1.19, 100,"Staples");

proMaker[4] =new Maker( 5,"Glue", .85, 100,"Elmer's");

proMaker[5] =new Maker( 6,"Tape", .50, 100,"3m");

proMaker[6] =new Maker( 7,"Paper", 1.85, 100,"Mead");

proMaker[7] =new Maker( 8,"Stapler", 2.21, 100,"Swingline");

proMaker[8] =new Maker( 9,"Folders", .50, 100,"Mead");

proMaker[9] =new Maker( 10,"Rulers", .27, 100,"Stanley");

}

int maxNum = 10;

int currentNum = 0;

int currentInv = 0;

Action firstAction =new AbstractAction("First")

{

publicvoid actionPerformed(ActionEvent evt)

{

currentInv = 0;

int populateFields;

}

};

JButton firstButton =new JButton(firstAction);

Action previousAction =new AbstractAction("Previous")

{

publicvoid actionPerformed(ActionEvent evt)

{

currentInv--;

if (currentInv < 0)

{

currentInv = maxNum - 1;

}

int populateFields;

}

};

JButton previousButton =new JButton(previousAction);

Action nextAction =new AbstractAction("Next")

{

publicvoid actionPerformed(ActionEvent evt)

{

currentInv++;

if (currentInv >= currentNum)

{

currentInv = 0;

}

int populateFields;

}

};

JButton nextButton =new JButton(nextAction);

Action lastAction =new AbstractAction("Last")

{

publicvoid actionPerformed(ActionEvent evt)

{

currentInv = currentNum - 1;

int populateFields;

}

};

JButton lastButton =new JButton(lastAction);

JPanel buttonPanel =new JPanel( );

privatevoid showGUI(){

JLabel l;

JButton button1;

JButton button2;

fields =new JTextField[8];

JFrame f =new JFrame("Inventory");

Container cp = f.getContentPane();

cp.setLayout(new GridBagLayout());

cp.setBackground(UIManager.getColor(Color.BLACK));

GridBagConstraints c =new GridBagConstraints();

c.gridx = 0;

c.gridy = GridBagConstraints.RELATIVE;

c.gridwidth = 1;

c.gridheight = 1;

c.insets =new Insets(2, 2, 2, 2);

c.anchor = GridBagConstraints.EAST;

cp.add(l =new JLabel("Item Number:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('a');

cp.add(l =new JLabel("Item Name:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('b');

cp.add(l =new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('c');

cp.add(l =new JLabel("Price per Unit: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('d');

cp.add(l =new JLabel("Total cost of Item: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('e');

cp.add(l =new JLabel("Total Value of Merchandise in Inventory: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('f');

cp.add(l =new JLabel("Manufacturer:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('g');

cp.add(l =new JLabel("Restocking Fee: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('h');

c.gridx = 1;

c.gridy = 0;

c.weightx = 1.0;

c.fill = GridBagConstraints.HORIZONTAL;

c.anchor = GridBagConstraints.CENTER;

cp.add(fields[0] =new JTextField(), c);

fields[0].setFocusAccelerator('a');

c.gridx = 1;

c.gridy = GridBagConstraints.RELATIVE;

cp.add(fields[1] =new JTextField(), c);

fields[1].setFocusAccelerator('b');

cp.add(fields[2] =new JTextField(), c);

fields[2].setFocusAccelerator('c');

cp.add(fields[3] =new JTextField(), c);

fields[3].setFocusAccelerator('d');

cp.add(fields[4] =new JTextField(), c);

fields[4].setFocusAccelerator('e');

cp.add(fields[5] =new JTextField(), c);

fields[5].setFocusAccelerator('f');

cp.add(fields[6] =new JTextField(), c);

fields[6].setFocusAccelerator('g');

cp.add(fields[7] =new JTextField(), c);

fields[7].setFocusAccelerator('h');

c.weightx = 0.0;

c.fill = GridBagConstraints.NONE;

cp.add(firstButton);

cp.add(previousButton);

cp.add(nextButton);

cp.add(lastButton);

JComboBox combo =new JComboBox();

for(int j = 0; j < proMaker.length; j++)

combo.addItem(proMaker[j].getName());

combo.addActionListener(this);

cp.add(combo);

cp.add(button1 =new JButton(""), c);

f.pack();

f.addWindowListener(new WindowAdapter()

{

publicvoid windowClosing(WindowEvent evt)

{

System.exit(0);

}

});

f.setVisible(true);

}

privatevoid populateFields(int index){

Maker maker = proMaker[index];

fields[0].setText(Long.toString(maker.getNumberCode()));

fields[1].setText(maker.getName());

fields[2].setText(Long.toString(maker.getUnits()));

fields[3].setText(Double.toString(maker.getPrice()));

fields[4].setText(Double.toString(maker.getSum()));

fields[5].setText(Double.toString(maker.totalAllInventory(proMaker)));

fields[6].setText(maker.getManufact());

fields[7].setText(Double.toString(maker.getSum()*.05));

}

class Maker{

int itemNumber;

String name;

int units;

double price;

String manufacturer;

public Maker(int n, String name,double price,int units, String manufac){

itemNumber = n;

this.name = name;

this.price = price;

this.units = units;

manufacturer = manufac;

}

publicint getNumberCode(){return itemNumber;}

public String getName(){return name;}

publicint getUnits(){return units;}

publicdouble getPrice(){return price;}

publicdouble getSum(){return units*price;}

public String getManufact(){return manufacturer;}

publicdouble totalAllInventory(Maker[] makers){

double total = 0;

for(int j = 0; j < makers.length; j++)

total += makers[j].getSum();

return total;

}

}}

[14262 byte] By [hurstta] at [2007-11-27 10:46:15]
# 1

add your actionlisteners to your buttons

JButton.addActionListener(..)

tjacobs01a at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 2

Oh it's urgent. No probs, I'll drop everything just to do your work so you can sit back and relax because you are more important than me or anyone else here who is asking for help.

floundera at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 3

URGENT!!!

Learn how to read the API if you want to learn.

If you read the JButton API you will find a link to the Swing tutorial on "How to Use Buttons" that will show you how to write an ActionListener for your button.

In the future Swing related questions should be posted in the Swing forum.

We don't want your whole program to debug when you have a problem. We only want a demo program that shows the problem. Its called a SSCCE.

see http://homepage1.nifty.com/algafield/sscce.html,

> I cannot figure out how to get them to work.

Did you add a System.out.println(...) to see if the code is being executed?

We can't solve your programming problem because we don't know what the button is supposed to do.

camickra at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 4

Do you know what stating "URGENT" does to the volunteers here? You're about to find out. It usually gets the pissed as hll (if American English is not your native tongue, that means very very mad).

It is like you are telling us that your problem is more important than any other poster's problem here. Most here will tell you that it's not.

It is as if you are telling us that we must drop everything that we are doing to attend to you because you are more important than us. Most will tell you that you're not.

It is as if you are telling us that because you are late in finishing your project, that your urgency becomes our urgency. Most here will tell you that it does not.

If you want prompt help, then skip the URGENT!! Rather ask a good question. That is, a question that is well thought out, that asks help on a specific problem, not some general vague problem such as "help me with the buttons on this program".Ask a question that shows that you have put real thought into asking the question.

Good luck

petes1234a at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 5

I am sorry to everyone. I didn't mean for anyone to drop what they are doing to help me. I do not believe that I am more important then the next poster, I just needed the help as soon as someone could help me.

hurstta at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 6

Describe your requirements and the current problems you have regarding them. What on earth your buttons should do?

hiwaa at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 7

> add your actionlisteners to your buttons

Oops, just realized the OP is creating Actions not ActionListeners.

The Swing tutorial also has example of how to write an use Actions.

camickra at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 8

// I have made some modifications. Please try this.

import java.awt.*;

import java.awt.event.*;

import java.text.NumberFormat;

import javax.swing.*;

import javax.swing.JButton;

public class InventoryTAH implements ActionListener

{

Maker[] proMaker;

JTextField[] fields;

NumberFormat nf;

int currentInv = 0;

public void actionPerformed(ActionEvent e)

{

currentInv= ((JComboBox)e.getSource()).getSelectedIndex();

populateFields(currentInv);

}

public static void main(String[] args)

{

try

{

UIManager.setLookAndFeel(

"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch (Exception e)

{

System.err.println(e.getClass().getName() + ": " + e.getMessage());

}

InventoryTAH test = new InventoryTAH();

test.initMakers();

test.showGUI();

test.populateFields(0);

}

private void initMakers() {

proMaker = new Maker[10];

proMaker[0] = new Maker( 1, "Pens",1.59,100,"Bic");

proMaker[1] = new Maker( 2, "Pencils", .65, 100,"Mead");

proMaker[2] = new Maker( 3, "Markers", 1.29, 100,"Sharpie");

proMaker[3] = new Maker( 4, "Paperclips", 1.19, 100,"Staples");

proMaker[4] = new Maker( 5, "Glue", .85, 100,"Elmer's");

proMaker[5] = new Maker( 6, "Tape", .50, 100,"3m");

proMaker[6] = new Maker( 7, "Paper", 1.85, 100,"Mead");

proMaker[7] = new Maker( 8, "Stapler", 2.21, 100,"Swingline");

proMaker[8] = new Maker( 9, "Folders", .50, 100,"Mead");

proMaker[9] = new Maker( 10, "Rulers", .27, 100,"Stanley");

}

int maxNum = 10;

int currentNum = 0;

Action firstAction = new AbstractAction("First")

{

public void actionPerformed(ActionEvent evt)

{

currentInv = 0;

populateFields(currentInv);

}

};

JButton firstButton = new JButton(firstAction);

Action previousAction = new AbstractAction("Previous")

{

public void actionPerformed(ActionEvent evt)

{

currentInv--;

if (currentInv < 0)

{

currentInv = maxNum - 1;

}

populateFields(currentInv);

}

};

JButton previousButton = new JButton(previousAction);

Action nextAction = new AbstractAction("Next")

{

public void actionPerformed(ActionEvent evt)

{

currentInv++;

if (currentInv >= maxNum)

{

currentInv = 0;

}

populateFields(currentInv);

}

};

JButton nextButton = new JButton(nextAction);

Action lastAction = new AbstractAction("Last")

{

public void actionPerformed(ActionEvent evt)

{

currentInv = maxNum-1;

populateFields(currentInv);

}

};

JButton lastButton = new JButton(lastAction);

JPanel buttonPanel = new JPanel( );

private void showGUI() {

JLabel l;

JButton button1;

JButton button2;

fields = new JTextField[8];

JFrame f = new JFrame("Inventory");

Container cp = f.getContentPane();

cp.setLayout(new GridBagLayout());

cp.setBackground(UIManager.getColor(Color.BLACK));

GridBagConstraints c = new GridBagConstraints();

c.gridx = 0;

c.gridy = GridBagConstraints.RELATIVE;

c.gridwidth = 1;

c.gridheight = 1;

c.insets = new Insets(2, 2, 2, 2);

c.anchor = GridBagConstraints.EAST;

cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('a');

cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('b');

cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('c');

cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('d');

cp.add(l = new JLabel("Total cost of Item: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('e');

cp.add(l = new JLabel("Total Value of Merchandise in Inventory: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('f');

cp.add(l = new JLabel("Manufacturer:", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('g');

cp.add(l = new JLabel("Restocking Fee: $", SwingConstants.CENTER), c);

l.setDisplayedMnemonic('h');

c.gridx = 1;

c.gridy = 0;

c.weightx = 1.0;

c.fill = GridBagConstraints.HORIZONTAL;

c.anchor = GridBagConstraints.CENTER;

cp.add(fields[0] = new JTextField(), c);

fields[0].setFocusAccelerator('a');

c.gridx = 1;

c.gridy = GridBagConstraints.RELATIVE;

cp.add(fields[1] = new JTextField(), c);

fields[1].setFocusAccelerator('b');

cp.add(fields[2] = new JTextField(), c);

fields[2].setFocusAccelerator('c');

cp.add(fields[3] = new JTextField(), c);

fields[3].setFocusAccelerator('d');

cp.add(fields[4] = new JTextField(), c);

fields[4].setFocusAccelerator('e');

cp.add(fields[5] = new JTextField(), c);

fields[5].setFocusAccelerator('f');

cp.add(fields[6] = new JTextField(), c);

fields[6].setFocusAccelerator('g');

cp.add(fields[7] = new JTextField(), c);

fields[7].setFocusAccelerator('h');

c.weightx = 0.0;

c.fill = GridBagConstraints.NONE;

cp.add(firstButton);

cp.add(previousButton);

cp.add(nextButton);

cp.add(lastButton);

JComboBox combo = new JComboBox();

for(int j = 0; j < proMaker.length; j++)

combo.addItem(proMaker[j].getName());

combo.addActionListener(this);

cp.add(combo);

cp.add(button1 = new JButton(""), c);

f.pack();

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent evt)

{

System.exit(0);

}

});

f.setVisible(true);

}

private void populateFields(int index) {

Maker maker = proMaker[index];

fields[0].setText(Long.toString(maker.getNumberCode()));

fields[1].setText(maker.getName());

fields[2].setText(Long.toString(maker.getUnits()));

fields[3].setText(Double.toString(maker.getPrice()));

fields[4].setText(Double.toString(maker.getSum()));

fields[5].setText(Double.toString(maker.totalAllInventory(proMaker)));

fields[6].setText(maker.getManufact());

fields[7].setText(Double.toString(maker.getSum()*.05));

}

class Maker {

int itemNumber;

String name;

int units;

double price;

String manufacturer;

public Maker(int n, String name, double price, int units, String manufac) {

itemNumber = n;

this.name = name;

this.price = price;

this.units = units;

manufacturer = manufac;

}

public int getNumberCode() { return itemNumber; }

public String getName() { return name; }

public int getUnits() { return units; }

public double getPrice() { return price; }

public double getSum() { return units*price; }

public String getManufact() { return manufacturer; }

public double totalAllInventory(Maker[] makers) {

double total = 0;

for(int j = 0; j < makers.length; j++)

total += makers[j].getSum();

return total;

}

}}

JL.Nayaka at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 9

Sorry guys/gals. Thanks for all that are willing to help me.

I need to add a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item

should display.

hurstta at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 10

> // I have made some modifications. Please try this.

Nayak, we are all impressed with your coding abilities and especially with your use of code tags.

Have you ever been to Yosemite National Park? It is one the most beautiful spots on earth. Well if you do go there, you'll see signs posted "do not feed the bears". And there are good reasons for this, not just that if you get close enough they could tear your limbs off. No, if you feed them, they don't learn to feed themselves. They learn to be lazy. They learn to beg for food.

Just like the bears at at Yosemite, you shouldn't spoon-feed the newbies. It teaches them to be lazy. It only motivates them not to think for themselves. They even forget how to think. So remember please, spoon-feeding != teaching. Also, they might just tear your limbs off.

Have a nice day.

petes1234a at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 11

Thank you sir. It seemed like a simple fix. The only thing now is I cannot set the fields to expand as the items are changed. When you open the program you can see i put a button on the bottom of the GUI so it will widen the fields. Can you help?

hurstta at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 12

> The only thing now is I cannot set the fields to expand as the items are changed

Thats not a good design. You just create the field at a reasonable size to begin with. You forms should not change in size just because you scroll from one item to the next.

camickra at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 13

My suggestion is that fields should not expend as item is changed. set the width for all fields to contain the item with maximum length.

JL.Nayaka at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...
# 14

Thank you everyone!

hurstta at 2007-7-28 20:17:53 > top of Java-index,Java Essentials,Java Programming...