File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes when select combo value how change value in other text fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "when select combo value how change value in other text fields" Watch "when select combo value how change value in other text fields" New topic
Author

when select combo value how change value in other text fields

sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
dear programmers.....
once again i fount trouble in my programme
now i have trouble in get record from database and put one field in combo box.... i did it .. but sir when i use next , or last, or first etc button on my frame como value how comobo value change.. i 'm confuse sir
here is its code
[code]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

public class SupplyMaterial extends JInternalFrame implements ActionListener
{
Connection con; Statement stmt; PreparedStatement prepstmt; ResultSet rs;
JPanel northPanel,southPanel;
JTextField tfartisan, tfmaterial, tfqty, tficode;
JButton MaterialInsert, MaterialSave, MaterialFirst,MaterialLast,MaterialNext,MaterialPrevious,MaterialOk,MaterialCancel;
GridBagLayout gb; GridBagConstraints gbc;
Container cont;
JComboBox box;
Vector v;
public SupplyMaterial(String title)
{
super(title, true,true,true);
v= new Vector();
box= new JComboBox(v);
JLabel artisan=new JLabel("Artisan ID");
JLabel material=new JLabel("Material ID");
JLabel qty=new JLabel("Quantity");
JLabel icode=new JLabel("Item ID");

tfartisan=new JTextField(20);
tfmaterial=new JTextField(20);
tfqty=new JTextField(20);
tficode=new JTextField(20);
MaterialInsert=new JButton("Insert");
MaterialSave=new JButton("Save");
MaterialFirst= new JButton("First");
MaterialLast=new JButton("Last");
MaterialNext=new JButton(">>");
MaterialPrevious= new JButton("<<");
MaterialOk=new JButton("Ok");
MaterialCancel=new JButton("Cancel");
gb= new GridBagLayout();
gbc= new GridBagConstraints();
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(artisan,0,0,1,2);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(box,0,1,1,3);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(material,1,0,1,2);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(tfmaterial,1,1,1,3);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(qty,2,0,1,2);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(tfqty,2,1,1,3);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(icode,3,0,1,2);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(tficode,3,1,1,3);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialFirst,4,0,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialLast,4,1,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialNext,4,2,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialPrevious,4,3,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialInsert,5,0,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialSave,5,1,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialOk,5,2,1,1);
gbc.fill= GridBagConstraints.HORIZONTAL;
addComponents(MaterialCancel,5,3,1,1);

MaterialFirst.addActionListener(this);
MaterialLast.addActionListener(this);
MaterialNext.addActionListener(this);
MaterialPrevious.addActionListener(this);
MaterialInsert.addActionListener(this);
MaterialSave.addActionListener(this);
MaterialCancel.addActionListener(this); MaterialOk.addActionListener(this);
connect();
nextRec();
setSize(300,200);
show();
}
public void addComponents(Component c, int row, int col, int nrow, int ncol)
{
gbc.gridx=col;
gbc.gridy= row;
gbc.gridwidth= ncol;
gbc.gridheight=nrow;
gb.setConstraints(c,gbc);
cont= getContentPane();
cont.setLayout(gb);
cont.add(c);
}
void connect()
{
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbc dbc:artisan", "sa","aptech");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("Select * from SUPPLY_MATERIAL");
while(rs.next())
{
//rs.getString("Artisan_ID"));
v.addElement(rs.getString("Artisan_ID"));
}
prepstmt=con.prepareStatement("Insert into SUPPLY_MATERIAL values (?,?,?,?) ");
}
catch(Exception conec)
{
System.out.println("Connection Error"+conec.getMessage());
}
}
public void firstRec()
{

try
{
if(rs.first())
{
v.get(rs.getString("Artisan_ID"));
//tfartisan.setText(rs.getString("Artisan_ID"));
tfmaterial.setText(rs.getString("Item_ID"));
tficode.setText(rs.getString("Product_ID"));
tfqty.setText(Integer.toString(rs.getInt("Quantity")));
}
}
catch(Exception ex)
{
System.out.println("Error"+ex.getMessage());
}
}
public void lastRec()
{

try
{
if(rs.last())
{
tfartisan.setText(rs.getString("Artisan_ID"));
tfmaterial.setText(rs.getString("Item_ID"));
tficode.setText(rs.getString("Product_ID"));
tfqty.setText(Integer.toString(rs.getInt("Quantity")));
}
}
catch(Exception ex1)
{
System.out.println("Error Last Button "+ex1.getMessage());
}
}
public void nextRec()
{

try
{
if(rs.next())
{
tfartisan.setText(rs.getString("Artisan_ID"));
tfmaterial.setText(rs.getString("Item_ID"));
tficode.setText(rs.getString("Product_ID"));
tfqty.setText(Integer.toString(rs.getInt("Quantity")));
}
else
{
rs.first();
}
}
catch(Exception ex3)
{
System.out.println("Error NExt Record "+ex3.getMessage());
}
}
public void prevRec()
{

try
{
if(rs.previous())
{
tfartisan.setText(rs.getString("Artisan_ID"));
tfmaterial.setText(rs.getString("Item_ID"));
tficode.setText(rs.getString("Product_ID"));
tfqty.setText(Integer.toString(rs.getInt("Quantity")));
}
else
{
rs.first();
}
}
catch(Exception ex4)
{
System.out.println("Error Previous Record "+ex4.getMessage());
}
}
public void insertRec()
{

tfartisan.setText("");
tfmaterial.setText("");
tfqty.setText("");
tficode.setText("");
MaterialSave.setEnabled(true);
}
public void saveRec()
{
try{
prepstmt.setString(1,tfartisan.getText());
prepstmt.setString(2,tfmaterial.getText());
prepstmt.setString(3,tficode.getText());
prepstmt.setInt(4,Integer.parseInt(tfqty.getText()));
prepstmt.executeUpdate();

rs=stmt.executeQuery("Select * from SUPPLY_MATERIAL");
}
catch(Exception prep)
{
System.out.println("Error on Prepared Statment"+prep.getMessage());
}

}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==MaterialFirst)
{
firstRec();
MaterialPrevious.setEnabled(true);
}
if(ae.getSource()==MaterialLast)
{
lastRec();
MaterialPrevious.setEnabled(true);
}
if(ae.getSource()==MaterialNext)
{
nextRec();
MaterialPrevious.setEnabled(true);
}
if(ae.getSource()==MaterialPrevious)
{
prevRec();
}
if(ae.getSource()==MaterialInsert)
{
insertRec();
}
if(ae.getSource()==MaterialSave)
{
saveRec();
MaterialSave.setEnabled(false);
}
if((ae.getSource()==MaterialCancel) || (ae.getSource()==MaterialOk))
{
dispose();
}
}
}
sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
Kindly Plzzzz Help mee n solve my problem
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

First, you should fill in the Vector v with all the possible values that the combo box could hold. You would call the "setSelectedItem( newSelection );" method on your combo box whenever a new record was chosen.

The above method should be fine for most scenarios, but if you need to create different lists of possible values on the fly, you'll need to create a new DefaultComboBoxModel each time using the list of possible choices for the new record and then set it on your combo box using the "setModel( model );" method.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
sirr i know that it will done through getSelectedIndex or getSelectedIndex
but sir problem is this that what type of condition i can give it here.. how can i set its index and change value of other textfields
thank u for replying
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you want to change the value in other text fields when the user selects a different value in the combo box, you can add either an ActionListener or an ItemListener to the combo box. An ActionListener receives an event whenever a new value is selected. An ItemListener receives an event for both the de-selection of an old item and the selection of the new item.
sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
yes sirrr u r right..
but u see as i have made methods for every button so may i make another method for comobo box and apply on it ActionListener by
gerSource()=combo.getSelectedIndex()
but sir its logic is not clear in my mind .. i'm confuse about it, that how can i complete my work.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You are going to add the following code to the constructor and actionPerformed() method:



Then you can implement the comboValueChanged() method to do whatever you need to do -
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when select combo value how change value in other text fields