简体   繁体   English

选择JComboBox时在JTextField中显示值

[英]Show value in JTextField When Selecting JComboBox

Hello Friends I am Working on a Project But problem occurs in JComboBox when i select dealer name from JComboBox its credit amount will display on JTextField . 朋友你好,我正在一个项目上,但是当我从JComboBox选择dealer name时, JComboBox会出现问题,其credit amount将显示在JTextField But the problem is only one Dealer Name is Showing in JComboBox . 但是问题是JComboBox仅显示一个经销商名称。 Please Help !. 请帮忙 !。 Here is my Source Code: 这是我的源代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Demo2 {

    private JFrame frmDemo;
    private JTextField textField;
    private static Connection con;
    private String query,CAmt;
    private PreparedStatement PStat;
    private ResultSet res;
    private JComboBox comboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Demo2 window = new Demo2();
                    window.frmDemo.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Demo2() {
        initialize();
        Database();
        Dealer();
    }

    //********* Database  ************//
     /* CREATE TABLE Dealer (
            DealerName   VARCHAR (45),
            CreditAmount DOUBLE (10, 4) 
        );*/
    //************************************

    public static Connection Database() {
        try
        {
            Class.forName("org.sqlite.JDBC");
            con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Azaz\\workspace\\SalesDesk\\Database\\SalesDesk.db");
            return con;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public void Dealer()
    {
        try
        {
            query="Select DealerName from Dealer";
            PStat=con.prepareStatement(query);
            res=PStat.executeQuery();
            while(res.next())
            {
                String dealer=res.getString("DealerName");
                comboBox.addItem(dealer);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                PStat.close();
                res.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmDemo = new JFrame();
        frmDemo.setTitle("Demo");
        frmDemo.setBounds(100, 100, 450, 300);
        frmDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblNewLabel = new JLabel("Dealer");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));

        comboBox = new JComboBox();
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try
                {
                    query="Select CreditAmount from Dealer Where DealerName=? ";
                    PStat=con.prepareStatement(query);
                    PStat.setString(1, comboBox.getSelectedItem().toString());
                    res=PStat.executeQuery();
                    while(res.next())
                    {
                        double cdt=res.getDouble("CreditAmount");
                        CAmt=Double.toString(cdt);
                        textField.setText(CAmt);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    try
                    {
                        PStat.close();
                        res.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        });

        textField = new JTextField();
        textField.setColumns(10);

        JLabel lblCreditAmount = new JLabel("Credit Amount");
        lblCreditAmount.setFont(new Font("Tahoma", Font.BOLD, 11));
        GroupLayout groupLayout = new GroupLayout(frmDemo.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(44)
                            .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lblCreditAmount)))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE))
                    .addGap(21))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(52)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel)
                        .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))
                    .addGap(37)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCreditAmount))
                    .addContainerGap(114, Short.MAX_VALUE))
        );
        frmDemo.getContentPane().setLayout(groupLayout);
    }
}

You need to concatenate the value to set them in the component, you can recover the current value to add the each value like 您需要连接值以在组件中进行设置,可以恢复当前值以添加每个值,例如

textfield.setText(textfield.getText() + "\n" +CAmt)

But it is better to use a StringBuilder to concatenate the values then set it in the textfield as a String . 但是最好使用StringBuilder连接这些值,然后在textfield中将其设置为String

StringBuilder sb = new StringBuilder();
while(res.next())
{
    double cdt=res.getDouble("CreditAmount");
    CAmt=Double.toString(cdt);
    sb.append(CAmt).append("\n");
}
textField.setText(sb.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM