0
안녕하세요 친구를 선택 JComboBox
에 발생합니다. 그러나 문제는 단 하나의 딜러 이름이 JComboBox
에있는 것입니다. 도와주세요 !. 당신은표시 값 내가 프로젝트 그러나 문제에서 작업하고 JComboBox에
textfield.setText(textfield.getText() + "\n" +CAmt)
처럼 각 값을 추가하기 위해 현재의 값을 복구 할 수 있습니다 구성 요소에서 설정할 값을 연결할 필요하지만 더 나은입니다
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);
}
}
[mcve]를 더 제공 할 수 있습니까? 내가 본 것으로부터, 당신은'textField.setText (CAmt);'를 가지므로 마지막 값만 설정합니다. 더 원한다면 값을 연결하기를 원할 수 있습니다. – AxelH