如何在单击提交后在JTextField上设置带有java bean值的文本

时间:2011-03-04 19:37:22

标签: java swing jtextfield

当从一个类中单击JButton(submit)时,另一个类应该替换JTextField的文本。新文本值是从bean收集的。新值出现在执行system.out.println的控制台上,但为什么不替换JTextField中的值。当程序最初编译时,JTextField初始化为当前库存读取的内容,但是一旦用户提交新订单,JTextField库存应该显示新的库存值。 (减去订购的书籍数量和当前库存)

inventoryTextField.setText(String.valueOf(currentInventory));

1 个答案:

答案 0 :(得分:0)

这是我一直在使用的代码:

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;
}
}