aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes change text on jtextfield from one class after clicking submit from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "change text on jtextfield from one class after clicking submit from another class" Watch "change text on jtextfield from one class after clicking submit from another class" New topic
Author

change text on jtextfield from one class after clicking submit from another class

Patty Zapata
Greenhorn

Joined: Mar 03, 2011
Posts: 7
when clicking a jbutton (submit) from one class another class is supposed to replace the text of a jtextfield. The new text value is collected from a bean. The new value appears on the console doing system.out.println but why doesn't it replace the value in the jtextfield. When the program is initially compiled the jtextfield is initialize to what the current inventory read but once the user submits a new order the jtextfield inventory is supposed to show the new inventory value. (subtracting the number of books ordered and the current inventory)

inventoryTextField.setText(String.valueOf(currentInventory));
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
The classic mistake doing this is to call the update routine from outside the GUI thread, so that it doesn't have any effect. But it is really impossible to tell without more information, and probably some code.

rc
Patty Zapata
Greenhorn

Joined: Mar 03, 2011
Posts: 7
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
info.setBillingStreet(billingStreetAddressTextField.getText()); //This is to test the Edit menu item from the BookOrder class
System.out.print(billingStreetAddressTextField.getText());
info.setQuantityPurchase(quantityPurchaseTextField.getText());
info.setCurrentInventory(Integer.parseInt(quantityPurchaseTextField.getText()));
int n = info.getCurrentInventory(); //UPDATED VALUE
inventory.createInventoryDetailsPanel(n);//PASSING THE UPDATED VALUE TO THE INVENTORY CLASS
}
});




import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class Inventory extends JPanel {

private JTabbedPane pane = null;
final JTextField inventoryTextField = new JTextField();

OrderInfo info2 = new OrderInfo();
JPanel mainPanel = new JPanel();
JPanel panel2;
JPanel panel22;
int currentInventory = 5000;
int n;

public Inventory() {
JPanel componentsPanel = new JPanel();
componentsPanel.setLayout(new BoxLayout(componentsPanel,
BoxLayout.PAGE_AXIS));
componentsPanel.add(createInventoryPanel());
add(mainPanel);
mainPanel.setVisible(true);
mainPanel.add(componentsPanel);
add(mainPanel);
}

public JTabbedPane createInventoryPanel() {
pane = new JTabbedPane();
pane.addTab("Inventory Details", createInventoryDetailsPanel(n));
pane.addTab("Inventory Reordering", createInventoryOrderingPanel());
return pane;
}

JPanel createInventoryDetailsPanel(int n) {
panel22 = new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel1.add(new JLabel("Item", JLabel.CENTER));
panel1.add(new JLabel(" Java How To Program : Deitel and Deitel "));
panel2.add(new JLabel("Quantity", JLabel.CENTER));
panel2.add(inventoryTextField);
System.out.print("Current Inventory is " + n);
if (n != 0) {//THIS IS WHERE I'M HAVING PROBLEMS TRYING TO PASS THE BEAN from the New Order class
//SO THE NEW VALUE WILL SHOW IN THE TEXT FIELD FOR INVENTORY DETAILS.
//IT ALWAYS SHOWS 5000 NO MATTER WHAT (INITIALIZED ABOVE)
inventoryTextField.setText(String.valueOf(n));
} else {
inventoryTextField.setText(String.valueOf(currentInventory));
}
panel22.add(panel1);
panel22.add(panel2);
return panel22;
}

private JPanel createInventoryOrderingPanel() {
JPanel panel = new JPanel();
JTextField txt1 = new JTextField(10);
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel1.add(new JLabel("Item", JLabel.CENTER));
panel1.add(new JLabel(" Java How To Program : Deitel and Deitel "));
panel2.add(new JLabel("Quantity", JLabel.CENTER));
panel2.add(txt1);
panel.add(panel1);
panel.add(panel2);
return panel;
}

}
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I can't say I know what is going on entirely, but I can tell you a couple of things to check on.

It appears that you are calling a method to set a value in a panel, and having it create the entire panel as well. I find that unusual; normally I create the panel, and then call some method that updates a value on the panel later. This is particularly useful when the value is going to be updated multiple times, and there is no need to go through all the Swing code of creating the panel each of those times.

But you have JPanel createInventoryDetailsPanel(int n). Your note says you want the new value to show up in the panel, but you do not say what call is not getting that done. Is it the one from the actionPerformed method? Or the one from the createInventoryPanel() method?

The first one should be all right -- actionPerformed is called (normally) from the UI thread, and when it sets the value in the text field, it should take effect immediately. I would create one string and use it in both the println and the setText, but it looks all right the way it is.

I don't know whether the other call is in the UI thread or not -- the Inventory constructor calls create InventoryPanel, which calls createInventoryDetailsPanel, but I don't know where the Inventory constructor is called from.

Do you know whether the Inventory constructor is called from the UI thread? Is actionPerformed getting executed?

rc
Patty Zapata
Greenhorn

Joined: Mar 03, 2011
Posts: 7
Mr. Cook:

Thank you for your prompt reply. To answer your question, I'm using a CardLayout to render the NewOrder and Inventory classes. The BookOrder class is calling the Inventory constructor. I'm also calling the Inventory constructor to call the createInventoryDetailsPanel(int n) from the NewOrder class. I don't know how else to call this method to update the new inventory value. This is my entire code:

/*
* This class renders the menu bar. Each menu item has a listener to load the new and inventory pages
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.Serializable;
import javax.swing.*;

public class BookOrder implements ActionListener, Serializable {
CardLayout cardLayout = new CardLayout();
JPanel panelHoldsCards = new JPanel();
JPanel newOrder = new NewOrder();
JPanel inventory = new Inventory();
JMenuItem newMenuItem = new JMenuItem("New");
JMenuItem editMenuItem = new JMenuItem("Edit");
JMenuItem inventoryMenuItem = new JMenuItem("Inventory");

OrderInfo info = new OrderInfo();

BookOrder() {
JFrame frame = new JFrame("Order Entry");
Container contentPane = frame.getContentPane();
JPanel panel = new JPanel();
contentPane.add(panelHoldsCards);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Order");

fileMenu.add(newMenuItem);
fileMenu.add(editMenuItem);
fileMenu.add(inventoryMenuItem);

menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
panelHoldsCards.setLayout(cardLayout);
panelHoldsCards.add(panel, "Nothing");
newMenuItem.addActionListener(this);
newMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == newMenuItem) {
clearTextFields(newOrder);
clearComboBoxes(newOrder);
clearCheckBoxes(newOrder);
clearLists(newOrder);
clearRadioButtons(newOrder);
}
}
});

editMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
editTextFields(newOrder);
}

});
inventoryMenuItem.addActionListener(this);

frame.pack();
frame.setSize(800, 1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void editTextFields(Container container) {

for (Component c : container.getComponents()) {
if (c instanceof JTextField) {
((JTextField) c).setText(info.getBillingStreet()); //this doesn't persist

} else if (c instanceof Container) {
editTextFields((Container) c);
}
}
}


public void clearTextFields(Container container) {

for (Component c : container.getComponents()) {
if (c instanceof JTextField) {
((JTextField) c).setText("");
} else if (c instanceof Container) {
clearTextFields((Container) c);
}
}
}

public void clearComboBoxes(Container container) {
for (Component co : container.getComponents()) {
if (co instanceof JComboBox) {
((JComboBox) co).setSelectedIndex(0);
} else if (co instanceof Container) {
clearComboBoxes((Container) co);
}
}
}

public void clearCheckBoxes(Container container) {
for (Component com : container.getComponents()) {
if (com instanceof JCheckBox) {
((JCheckBox) com).setSelected(false);
} else if (com instanceof Container) {
clearCheckBoxes((Container) com);
}
}
}

public void clearLists(Container container) {
for (Component comp : container.getComponents()) {
if (comp instanceof JList) {
((JList) comp).setSelectedIndex(0);
} else if (comp instanceof Container) {
clearLists((Container) comp);
}
}
}

public void clearRadioButtons(Container container) {
// ButtonModel a = order.group.getSelection();
for (Component comp : container.getComponents()) {
if (comp instanceof JRadioButton) {
((JRadioButton) comp).setSelected(false);
} else if (comp instanceof Container) {
clearRadioButtons((Container) comp);
}
}
}

public static void main(String[] args) throws FileNotFoundException {
BookOrder bookOrder = new BookOrder();
}

public void addNewPanel(String actionCommand) {
panelHoldsCards.add(newOrder, "New");
cardLayout.show(panelHoldsCards, "New");
}

public void addEditPanel(String actionCommand) {
panelHoldsCards.add(newOrder, "Edit");
cardLayout.show(panelHoldsCards, "Edit");
}

public void addInventoryPanel(String actionCommand) {
panelHoldsCards.add(inventory, "Inventory");
cardLayout.show(panelHoldsCards, "Inventory");
}

public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if (ae.getSource() instanceof JMenuItem) {
String nothing = "Nothing";
if ("Nothhing".equals(actionCommand))
cardLayout.first(panelHoldsCards);
else if ("New".equals(actionCommand))
addNewPanel("New");
else if ("Edit".equals(actionCommand))
addEditPanel("Edit");
else if ("Inventory".equals(actionCommand))
addInventoryPanel("Inventory");
}
}
}



/*
* This class loads the Order and Edit Page. The constructor initializes all components.
* The submit button triggers the bean to set values entered by the user//THIS DOESN'T WORK
*/

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.Serializable;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class NewOrder extends JPanel implements ItemListener, ActionListener, Serializable {

public JTextField billingStreetAddressTextField;
public JTextField billingApartmentAddressTextField;
public JTextField billingCityAddressTextField;
public JComboBox billingStateAddressComboBox;
public String states[] = { "Alabama", "Alaska", "American Samoa",
"Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Florida", "Georgia", "Guam",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota",
"Northern Marianas Islands", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
"Virginia", "Virgin Islands", "Washington", "West Virginia",
"Wisconsin", "Wyoming" };

ActionListener stateListener = new StateSelected();

public int stateBillingAddressIndex;
public int stateShippingAddressIndex;

public JTextField billingCountryAddressTextField;
public JTextField shippingStreetAddressTextField;
public JTextField shippingApartmentAddressTextField;
public JTextField shippingCityAddressTextField;
public JComboBox shippingStateAddressComboBox;
public JTextField shippingCountryAddressTextField;

public JList booksList;
ArrayList<String> list = new ArrayList<String>();
public final String books[] = {
"Who needs an IT book when there is internet", "Expensive IT book",
"Outdated IT book", "Heavy IT Book" };
String displayBooks;

public JTextField quantityPurchaseTextField;
public JFormattedTextField orderTotalTextField;

public JCheckBox addressCheckBox;

public JRadioButton[] paymentMethod;

public JPasswordField creditCardTextField;
public JPasswordField payPalTextField;
public JLabel creditCardLabel;
public JLabel payPalLabel;

public JButton submit;

ButtonGroup group = new ButtonGroup();


OrderInfo info = new OrderInfo();

public NewOrder() {

JPanel holdPanel = new JPanel();
JPanel componentsPanel = new JPanel();

componentsPanel.setLayout(new BoxLayout(componentsPanel,
BoxLayout.PAGE_AXIS));

JLabel billingLabel = new JLabel("Billing Address ");
billingLabel.setFont(new Font("TimesRoman", Font.BOLD, 20));
billingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(billingLabel);

JLabel streetBillingLabel = new JLabel(
"Street Address ");
streetBillingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(streetBillingLabel);

billingStreetAddressTextField = new JTextField(20);
componentsPanel.add(billingStreetAddressTextField);

JLabel apartmentBillingLabel = new JLabel(
"Apartment Number ");
apartmentBillingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(apartmentBillingLabel);

billingApartmentAddressTextField = new JTextField(20);
componentsPanel.add(billingApartmentAddressTextField);

JLabel cityBillingLabel = new JLabel(
"City ");
cityBillingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(cityBillingLabel);

billingCityAddressTextField = new JTextField(20);
componentsPanel.add(billingCityAddressTextField);

JLabel stateBillingLabel = new JLabel(
"State ");
stateBillingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(stateBillingLabel);

billingStateAddressComboBox = new JComboBox(states);
componentsPanel.add(billingStateAddressComboBox);
billingStateAddressComboBox.addActionListener(stateListener);

JLabel countryBillingLabel = new JLabel(
"Country ");
countryBillingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(countryBillingLabel);

billingCountryAddressTextField = new JTextField(20);
componentsPanel.add(billingCountryAddressTextField);

JLabel shippingLabel = new JLabel("Shipping Address");
shippingLabel.setFont(new Font("TimesRoman", Font.BOLD, 20));
shippingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(shippingLabel);

addressCheckBox = new JCheckBox("Same as Billing Address?");
addressCheckBox.addItemListener(this);
componentsPanel.add(addressCheckBox);

JLabel shippingStreetLabel = new JLabel(
"Street Address ");
shippingStreetLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(shippingStreetLabel);

shippingStreetAddressTextField = new JTextField(20);
componentsPanel.add(shippingStreetAddressTextField);

JLabel apartmentShippingLabel = new JLabel(
"Apartment Number ");
apartmentShippingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(apartmentShippingLabel);

shippingApartmentAddressTextField = new JTextField(20);
componentsPanel.add(shippingApartmentAddressTextField);

JLabel cityShippingLabel = new JLabel(
"City ");
cityShippingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(cityShippingLabel);
shippingCityAddressTextField = new JTextField(20);
componentsPanel.add(shippingCityAddressTextField);

JLabel stateShippingLabel = new JLabel(
"State ");
stateShippingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(stateShippingLabel);
shippingStateAddressComboBox = new JComboBox(states);
componentsPanel.add(shippingStateAddressComboBox);

JLabel countryShippingLabel = new JLabel(
"Country ");
countryShippingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(countryShippingLabel);
shippingCountryAddressTextField = new JTextField(20);
componentsPanel.add(shippingCountryAddressTextField);

JLabel booksToPurchaseLabel = new JLabel("Select Book to purchase");
booksToPurchaseLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
componentsPanel.add(booksToPurchaseLabel);
booksList = new JList(books);
componentsPanel.add(booksList);
booksList
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
booksList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
Object[] booksSelectedArray = booksList.getSelectedValues();

for (int i = 0; i < booksSelectedArray.length; i++) {
displayBooks = (String) booksSelectedArray[i];
}
list.add(displayBooks);
}
}
});

JLabel qtyBooksLabel = new JLabel(
"How many books would you like to purchase today? ");
componentsPanel.add(qtyBooksLabel);
quantityPurchaseTextField = new JTextField(20);
componentsPanel.add(quantityPurchaseTextField);

JLabel totalLabel = new JLabel("Order Total");
componentsPanel.add(totalLabel);

orderTotalTextField = new JFormattedTextField();
componentsPanel.add(orderTotalTextField);
orderTotalTextField.setEditable(false);

quantityPurchaseTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

try {
Double orderTotal = (Double
.parseDouble(quantityPurchaseTextField.getText()) * 100);
NumberFormat formattedOrderTotal = NumberFormat
.getInstance();
orderTotalTextField.setText(""
+ formattedOrderTotal.format(orderTotal));

} catch (NumberFormatException nfe) {

}
}
});

quantityPurchaseTextField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent event) {

try {
Double orderTotal = (Double
.parseDouble(quantityPurchaseTextField.getText()) * 100);
NumberFormat formattedOrderTotal = NumberFormat
.getInstance();
orderTotalTextField.setText(""
+ formattedOrderTotal.format(orderTotal));

} catch (NumberFormatException nfe) {
}
}

public void focusLost(FocusEvent event) {
focusGained(event);
}
});
// JPanel radioPanel = new JPanel();
final JRadioButton[] paymentMethod = new JRadioButton[3];
paymentMethod[0] = new JRadioButton("Credit Card");
paymentMethod[1] = new JRadioButton("PayPal");
paymentMethod[2] = new JRadioButton("Invoice on Delivery");

paymentMethod[0].setActionCommand("Credit Card");
paymentMethod[1].setActionCommand("PayPal");
paymentMethod[2].setActionCommand("Invoice on Delivery");

paymentMethod[0].addActionListener(this);
paymentMethod[1].addActionListener(this);
paymentMethod[2].addActionListener(this);

for (int i = 0; i < paymentMethod.length; i++) {
group.add(paymentMethod[i]);
componentsPanel.add(paymentMethod[i]);
}
// radioPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
// "Payment Method"));
creditCardLabel = new JLabel("Credit Card");
componentsPanel.add(creditCardLabel);
creditCardTextField = new JPasswordField();
componentsPanel.add(creditCardTextField);
creditCardTextField.setVisible(false);
creditCardLabel.setVisible(false);

payPalLabel = new JLabel("PayPal");
componentsPanel.add(payPalLabel);
payPalTextField = new JPasswordField();
componentsPanel.add(payPalTextField);
payPalTextField.setVisible(false);
payPalLabel.setVisible(false);

submit = new JButton("Submit");
componentsPanel.add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//info.setBillingStreet(billingStreetAddressTextField.getText()); //This is to test the Edit menu item from the BookOrder class
//System.out.print(billingStreetAddressTextField.getText());
info.setQuantityPurchase(quantityPurchaseTextField.getText());
info.setCurrentInventory(Integer.parseInt(quantityPurchaseTextField.getText()));
int n = info.getCurrentInventory(); //UPDATED VALUE
Inventory inventory = new Inventory();
inventory.createInventoryDetailsPanel(n);//PASSING THE UPDATED VALUE TO THE INVENTORY CLASS
}
});
holdPanel.add(componentsPanel);
add(holdPanel);
}
@Override
public void itemStateChanged(ItemEvent ie) {

String a = billingStreetAddressTextField.getText();
String b = billingApartmentAddressTextField.getText();
String c = billingCityAddressTextField.getText();
String d = (String) billingStateAddressComboBox.getSelectedItem();
String e = billingCountryAddressTextField.getText();

if (addressCheckBox.isSelected()) {
shippingStreetAddressTextField.setText(a);
shippingApartmentAddressTextField.setText(b);
shippingCityAddressTextField.setText(c);
shippingStateAddressComboBox.setSelectedItem(d);
shippingCountryAddressTextField.setText(e);
} else {
shippingStreetAddressTextField.setText("");
shippingApartmentAddressTextField.setText("");
shippingCityAddressTextField.setText("");
shippingStateAddressComboBox.setSelectedIndex(0);
shippingCountryAddressTextField.setText("");
}
}

public void actionPerformed(ActionEvent ae) {
ButtonModel bModel = group.getSelection();
if (bModel.getActionCommand() == "Credit Card") {
creditCardLabel.setVisible(true);
creditCardTextField.setVisible(true);
payPalLabel.setVisible(false);
payPalTextField.setVisible(false);
}
if (bModel.getActionCommand() == "PayPal") {
payPalLabel.setVisible(true);
payPalTextField.setVisible(true);
creditCardLabel.setVisible(false);
creditCardTextField.setVisible(false);
}
if (bModel.getActionCommand() == "Invoice on Delivery") {
payPalLabel.setVisible(false);
payPalTextField.setVisible(false);
creditCardLabel.setVisible(false);
creditCardTextField.setVisible(false);
}
}

public class StateSelected implements ActionListener {
public void actionPerformed(ActionEvent ae) {
stateBillingAddressIndex = (Integer) billingStateAddressComboBox
.getSelectedIndex();
stateShippingAddressIndex = (Integer) shippingStateAddressComboBox
.getSelectedIndex();
}
}
}


import java.io.FileNotFoundException;
import java.io.Serializable;
import javax.swing.ButtonModel;

public class OrderInfo implements Serializable {

public String billingStreet;
public String billingApartment;
public String billingCity;
public int billingState;
public String billingCountry;
public String shippingStreet;
public String shippingApartment;
public String shippingCity;
public int shippingState;
public String shippingCountry;
public int bookName;
public int quantityPurchase;
public int currentInventory =5000;
public String orderTotal;
public ButtonModel paymentMethod;
public char[] creditCard;
public char[] payPal;
public int intQuantityPurchase;

/**
* @param billingStreet
* @param billingApartment
* @param billingCity
* @param billingState
* @param billingCountry
* @param shippingStreet
* @param shippingApartment
* @param shippingCity
* @param shippingState
* @param shippingCountry
* @param bookName
* @param quantityPurchase
* @param orderTotal
* @param paymentMethod
* @param creditCard
* @param payPal
* @throws FileNotFoundException
*/
/*public OrderInfo(String billingStreet, String billingApartment,
String billingCity, int billingState, String billingCountry,
String shippingStreet, String shippingApartment,
String shippingCity, int shippingState, String shippingCountry,
int bookName, int quantityPurchase, String orderTotal,
ButtonModel paymentMethod, char[] creditCard, char[] payPal) {
super();
this.billingStreet = billingStreet;
this.billingApartment = billingApartment;
this.billingCity = billingCity;
this.billingState = billingState;
this.billingCountry = billingCountry;
this.shippingStreet = shippingStreet;
this.shippingApartment = shippingApartment;
this.shippingCity = shippingCity;
this.shippingState = shippingState;
this.shippingCountry = shippingCountry;
this.bookName = bookName;
this.quantityPurchase = quantityPurchase;
this.orderTotal = orderTotal;
this.paymentMethod = paymentMethod;
this.creditCard = creditCard;
this.payPal = payPal;
//this.intQuantityPurchase = intQuantityPurchase;
}
*/

public String getBillingStreet() {
return billingStreet;
}

public void setBillingStreet(String billingStreet) {
this.billingStreet = billingStreet;
//System.out.print("form order info " + billingStreet);
}

public String getBillingApartment() {
return billingApartment;
}

public void setBillingApartment(String billingApartment) {
this.billingApartment = billingApartment;
}

public String getBillingCity() {
return billingCity;
}

public void setBillingCity(String billingCity) {
this.billingCity = billingCity;
}

public int getBillingState() {
return billingState;
}

public void setBillingState(int billingState) {
this.billingState = billingState;
}

public String getBillingCountry() {
return billingCountry;
}

public void setBillingCountry(String billingCountry) {
this.billingCountry = billingCountry;
}

public String getShippingStreet() {
return shippingStreet;
}

public void setShippingStreet(String shippingStreet) {
this.shippingStreet = shippingStreet;
}

public String getShippingApartment() {
return shippingApartment;
}

public void setShippingApartment(String shippingApartment) {
this.shippingApartment = shippingApartment;
}

public String getShippingCity() {
return shippingCity;
}

public void setShippingCity(String shippingCity) {
this.shippingCity = shippingCity;
}

public int getShippingState() {
return shippingState;
}

public void setShippingState(int shippingState) {
this.shippingState = shippingState;
}

public String getShippingCountry() {
return shippingCountry;
}

public void setShippingCountry(String shippingCountry) {
this.shippingCountry = shippingCountry;
}

public int getBookName() {
return bookName;
}

public void setBookName(int bookName) {
this.bookName = bookName;
}

public int getQuantityPurchase() {
return intQuantityPurchase;
}

public void setQuantityPurchase(String quantityPurchase) {
try {
intQuantityPurchase = Integer.parseInt(quantityPurchase.trim());
} catch (NumberFormatException e) {
e.getMessage();
}
}

public int getCurrentInventory() {
return currentInventory;
}

public void setCurrentInventory(int intQuantityPurchase) {
this.currentInventory = currentInventory - intQuantityPurchase;
}

public String getOrderTotal() {
return orderTotal;
}

public void setOrderTotal(String orderTotal) {
this.orderTotal = orderTotal;
}

public ButtonModel getPaymentMethod() {
return paymentMethod;
}

public void setPaymentMethod(ButtonModel paymentMethod) {
this.paymentMethod = paymentMethod;
}

public char[] getCreditCard() {
return creditCard;
}

public void setCreditCard(char[] creditCard) {
if (creditCard != null) {
this.creditCard = creditCard;
} else {
creditCard[0] = 0;
}
}

public char[] getPayPal() {
return payPal;
}

public void setPayPal(char[] payPal) {
if (payPal != null) {
this.payPal = payPal;
} else {
payPal[0] = 0;
}
}
}



import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class Inventory extends JPanel {

private JTabbedPane pane = null;
final JTextField inventoryTextField = new JTextField();

OrderInfo info2 = new OrderInfo();
JPanel mainPanel = new JPanel();
JPanel panel2;
JPanel panel22;
int currentInventory = 5000;
int n;

public Inventory() {
JPanel componentsPanel = new JPanel();
componentsPanel.setLayout(new BoxLayout(componentsPanel,
BoxLayout.PAGE_AXIS));
componentsPanel.add(createInventoryPanel());
add(mainPanel);
mainPanel.setVisible(true);
mainPanel.add(componentsPanel);
add(mainPanel);
System.out.print(getRootPane());
}

public JTabbedPane createInventoryPanel() {
pane = new JTabbedPane();
pane.addTab("Inventory Details", createInventoryDetailsPanel(n));
pane.addTab("Inventory Reordering", createInventoryOrderingPanel());
return pane;
}

JPanel createInventoryDetailsPanel(int n) {
panel22 = new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel1.add(new JLabel("Item", JLabel.CENTER));
panel1.add(new JLabel(" Java How To Program : Deitel and Deitel "));
panel2.add(new JLabel("Quantity", JLabel.CENTER));
panel2.add(inventoryTextField);

if (n != 0) {//THIS IS WHERE I'M HAVING PROBLEMS TRYING TO PASS THE BEAN from the New Order class
//SO THE NEW VALUE WILL SHOW IN THE TEXT FIELD FOR INVENTORY DETAILS.
//IT ALWAYS SHOWS 5000 NO MATTER WHAT (INITIALIZED ABOVE)

inventoryTextField.setText(String.valueOf(n));//this will not change. It always shows 5000

System.out.print("Current Inventory is now " + n); //This will change. The console gives me the right result.
} else {
inventoryTextField.setText(String.valueOf(currentInventory));
}
panel22.add(panel1);
panel22.add(panel2);
return panel22;
}

private JPanel createInventoryOrderingPanel() {
JPanel panel = new JPanel();
JTextField txt1 = new JTextField(10);
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel1.add(new JLabel("Item", JLabel.CENTER));
panel1.add(new JLabel(" Java How To Program : Deitel and Deitel "));
panel2.add(new JLabel("Quantity", JLabel.CENTER));
panel2.add(txt1);
panel.add(panel1);
panel.add(panel2);
return panel;
}

}




Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I have not quite figured out what you intended to do, but I think I have figured out at least part of what the problem is, and can't spend any more time on it right now.

Just a tip: if you will use "System.out.println" in place of "System.out.print", it will be easier to read the output from multiple trace statements. The former is the same, except it puts a new line between itself and the next output.

In the if statement where you've indicated you are having trouble ("if (n != 0)"), put a println statement after both of the setText statements. Use something like this:

System.out.println("setting " + n + "on inventoryTextField:" + inventoryTextField.hashCode());

Replace "n" with "currentInventory" in the else portion of the if.

What I've found is that the two statements are executed on different inventoryTextField instances, as indicated by having different hashCodes. I'm not sure how you managed to have two of them, I'm having trouble following the logic. But I think the reason (or at least part of the reason) you are not updating the value with the first part of the 'if' is that it is being executed on a different text field, one that probably isn't on the UI, or is not visible on the UI.

I would try reorganizing things so that you have a set of code that you execute one time that creates the entire UI -- order panel, inventory panel, etc. Your code creates the panels each time you are going to display the inventory panel, and that's confusing and may be slow, and is unnecessary.

Also, if you post any code again, please put it within code tags.

rc
Patty Zapata
Greenhorn

Joined: Mar 03, 2011
Posts: 7
Mr. Cook: I will answer the rest of the questions shortly but first how do I post code using code tags? What are code tags? I'm new to Java and JavaRanch.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

Click here -> UseCodeTags.

Your second example is too long though. Please post an SSCCE instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Patty Zapata
Greenhorn

Joined: Mar 03, 2011
Posts: 7
Mr. Cook:

As you requested I included the following lines of code:


I get these on the console:

setting 0on inventoryTextField:926681939
setting 0on inventoryTextField:1860295362
from inventoryTextField.getText 4998
setting 4998on inventoryTextField:1860295362
Patty Zapata
Greenhorn

Joined: Mar 03, 2011
Posts: 7
Thank you for the code tags suggestion: Here is the code again: Sorry for the clutter.


Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Patty Zapata wrote:Mr. Cook:

As you requested I included the following lines of code:


I get these on the console:

setting 0on inventoryTextField:926681939
setting 0on inventoryTextField:1860295362
from inventoryTextField.getText 4998
setting 4998on inventoryTextField:1860295362


So the first hashCode is different than the other two; we don't know which one is displayed in the panel, but the fact that setting it to a value isn't working suggests that you created one text field, put it on the panel, and are now updating a different one.

I think you need to reorganize your code. Have the program go through all the UI creation stuff -- panels, setting layout, adding components, etc., so that all of that is done before you display your frame. Don't execute any of that code again, directly or indirectly - you have created the UI, just leave it there.

Then to change a value, call a method to set the value of the component that is already there. You could, for instance, have a method on Inventory to set the text field; make the text field an instance variable so that you can refer to it in an instance method and be using the same reference as the one you created.

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: change text on jtextfield from one class after clicking submit from another class