
hello sir, now i'm create two textfield for mark1&mark2 from db.how to add these two numbers in another one text field.how to write a coding... if u have another one idea pls tell me........

Hi Friend,
Try the following code:
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class SumOfNumbers{
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("First Number: ");
JLabel label2=new JLabel("Second Number: ");
JLabel label3=new JLabel("Result: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
final JTextField text3=new JTextField(20);
JButton button=new JButton("Calculate");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from student where id=1");
int mar1=0,mar2=0;
if(rs.next()){
mar1=rs.getInt("marks1");
mar2=rs.getInt("marks2");
}
text1.setText(Integer.toString(mar1));
text2.setText(Integer.toString(mar2));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int num1=Integer.parseInt(text1.getText());
int num2=Integer.parseInt(text2.getText());
int result=num1+num2;
text3.setText(Integer.toString(result));
}
});
}
catch(Exception e){
}
JPanel p=new JPanel(new GridLayout(4,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(label3);
p.add(text3);
p.add(button);
f.add(p);
f.setVisible(true);
f.pack();
}
}
Thanks

adding of two numbers in designing of frame package swings;
import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane;
public class Swings extends JFrame implements ActionListener {
JButton jb1;
JTextField jt3;
JButton jb2;
JLabel jl1;
JTextField jt1;
JLabel jl2;
JTextField jt2;
float c;
Swings() {
jb1 = new JButton("Sum");
jt3 = new JTextField(10);
jb2 = new JButton("reset");
jl1 = new JLabel("First number");
jt1 = new JTextField(10);
jl2 = new JLabel("Second number number");
jt2 = new JTextField(10);
Container con = getContentPane();
JPanel jp = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp.add(jl1);
jp.add(jt1);
jp.add(jl2);
jp.add(jt2);
jp.add(jb1);
jp.add(jt3);
jp.add(jb2);
con.add(jp);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public static void main(String[] args) throws IOException {
Swings sw = new Swings();
sw.setVisible(true);
sw.setSize(250, 250);
sw.setResizable(true);
sw.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
if ((jt1 != null && !"".equals(jt1)) && (jt2 != null && !"".equals(jt2))) {
try {
float a = Integer.parseInt(jt1.getText());
float b = Integer.parseInt(jt1.getText());
c = a + b;
jt3.setText(Float.toString(c));
} catch (NumberFormatException ae) {
System.out.println("This is not a number");
System.out.println(ae.getMessage());
}
}
} else {
jt1.setText("");
jt2.setText("");
jt3.setText("");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.