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

Posted

技术标签:

【中文标题】单击提交后如何在带有java bean值的JTextField上设置文本【英文标题】:how do you set text on JTextField that carries the java bean value after clicking submit 【发布时间】:2011-07-09 01:51:44 【问题描述】:

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

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

【问题讨论】:

请发布您的代码,不清楚您在问什么。 【参考方案1】:

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

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;


【讨论】:

以上是关于单击提交后如何在带有java bean值的JTextField上设置文本的主要内容,如果未能解决你的问题,请参考以下文章

如何仅提交带有下拉选择值的表单?

R 更新 textInput 值的闪亮延迟

单击按钮时如何在android中获取带有ListView值的复选框,EditText?

使用取消按钮单击提交按钮后阻止表单提交[重复]

在jsp中提交页面后填充popup

如何在支持 bean 中处理漂亮的“查询参数”?