|
How can I use another class actionPerformed in another main class?
I want to use this class actionperformed
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.Container;
import javax.swing.JOptionPane;
import java.awt.Frame;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class SpringSample extends Frame implements ActionListener
{
JLabel F_namelbl = new JLabel("Username: ");
JLabel L_namelbl = new JLabel("Password: ");
JTextField tf_Fname = new JTextField(15);
JPasswordField tf_Lname = new JPasswordField(15);
JLabel Lbl_HW = new JLabel("< Login Area >");
JButton Btn = new JButton("Login");
JButton btn_cancel = new JButton("Cancel");
private static final long serialVersionUID = 1L;
SpringSample()
{
JFrame frame = new JFrame("Login Area!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JPanel p = new JPanel(new SpringLayout());
SpringLayout layout = (new SpringLayout());
contentPane.setLayout(layout);
JPanel p1=new JPanel();
frame.add(p1);
contentPane.add(F_namelbl);
contentPane.add(L_namelbl);
contentPane.add(tf_Fname);
contentPane.add(tf_Lname);
contentPane.add(Lbl_HW);
contentPane.add(Btn);
contentPane.add(btn_cancel);
layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, F_namelbl, 30, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, F_namelbl, 25, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, L_namelbl, 30, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, L_namelbl, 50, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.NORTH, tf_Fname, 25, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, tf_Fname, 15, SpringLayout.EAST, F_namelbl);
layout.putConstraint(SpringLayout.NORTH, tf_Lname, 50, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, tf_Lname, 15, SpringLayout.EAST, F_namelbl);
layout.putConstraint(SpringLayout.NORTH, Btn, 80, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, Btn, 110, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, btn_cancel, 80, SpringLayout.NORTH, p1);
layout.putConstraint(SpringLayout.WEST, btn_cancel, 200, SpringLayout.WEST, p1);
btn_cancel.addActionListener(this);
Btn.addActionListener(this);
frame.pack();
frame.setSize(350, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[])
{
new SpringSample();
Connectionstring();
try {
insertRecordIntoDbUserTable();
selectfromdb();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
static Connection Connectionstring()
{
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
private static void insertRecordIntoDbUserTable() throws SQLException
{
Connection con = null;
Statement statement = null;
String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
try
{
con = Connectionstring();
statement = con.createStatement();
System.out.println(insertTableSQL);
statement.executeUpdate(insertTableSQL);
JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
}
if (con != null)
{
con.close();
JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
}
}
}
private static void selectfromdb() throws SQLException
{
Statement stmt = Connectionstring().createStatement();
ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
while (rs.next())
{
String lastName = rs.getString("Username");
String Pass = rs.getString("Password");
System.out.println(lastName + "" + Pass + "\n");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn_cancel)
{
JOptionPane.showMessageDialog(null,"You have clicked on Cancel","button cancel",JOptionPane.WARNING_MESSAGE);
}
else if(e.getSource() == Btn)
{
JOptionPane.showMessageDialog(null,"You have clicked login","button login",JOptionPane.WARNING_MESSAGE);
}
}
}
I want to use in this class
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public abstract class ConnectionDB
{
public static void main(String[] args)
{
SpringSample sp = new SpringSample();
try {
insertRecordIntoDbUserTable();
selectfromdb();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void insertRecordIntoDbUserTable() throws SQLException
{
Connection con = null;
Statement statement = null;
String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('null2','tf_Lname')";
try
{
con = DBConnectionstring();
statement = con.createStatement();
System.out.println(insertTableSQL);
statement.executeUpdate(insertTableSQL);
JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
}
if (con != null)
{
con.close();
JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
}
}
}
private static void selectfromdb() throws SQLException
{
Statement stmt = DBConnectionstring().createStatement();
ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
while (rs.next())
{
String lastName = rs.getString("Username");
String Pass = rs.getString("Password");
System.out.println(lastName + "" + Pass + "\n");
}
}
static Connection DBConnectionstring()
{
Connection con = null;
String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
}
How can I do that?
|
|
|
|
|
Rather than dumping a lot of code, try explaining your problem in clear terms. If you need to call a method of a class then create an object of that class and call the method as normal. however, if that is not your question then please clarify.
Use the best guess
|
|
|
|
|
You can only register an event handler to an event source. So your first class is an event handler. And there fore you can use this to any of the event source which trigger that event. Something like this.
class Handler implements ActionPerformed{
public void actionPerformed(ActionEvent e) {}
}
}
class Source {
p s v main(){
JButton bt = new Jbutton("Ok");
bt.addActionListener(new Handler());
}
}
Regards
Shubhashish
|
|
|
|
|
Jvm has be written to have good speed in linux?
|
|
|
|
|
Please stop posting these questions. you have been told plenty of times that they are not valid for the programming forums.
Use the best guess
|
|
|
|
|
You are a funny person, why do I need to care what you said? This is a very good topic to discuss for anyone interest in Java, stop being funny alright?
|
|
|
|
|
crunchor wrote: Jvm has be written to have good speed in linux?
Of course it was, Do you really think that it would have been written to have poor performance?
|
|
|
|
|
|
Please stop posting these irrelevant questions here. If you have a problem with another website then complain to that site's adminstrators. Once again this post has nothing to do with programming or Java. You are getting seriously close to being removed from this site.
Use the best guess
|
|
|
|
|
coeproject is a .net focus forum, I ask for what is happening in somewhere about java is not a problem. No one really care about java in this forum anyway.
|
|
|
|
|
|
Richard. He's trolling. I suggest that we apply the official response to trolls.
|
|
|
|
|
I agree, let the voting commence.
Use the best guess
|
|
|
|
|
I think you forgot to read this:
Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
|
|
|
|
|
Presumably you are referring to the Oracle Java Forums although it actually applies to all of the Oracle forums.
The forums are of course a free service and Oracle is all about the money so one can lead with that.
But other than that someone, or several someones, made a decision to upgrade the forum software and that mess is the result.
crunchor wrote: It supposes to be the biggest java community to discuss rigtht?
Since Oracle took over the traffic has been down SIGNIFICANTLY. Forums that used to receive hundreds of posts daily now receive less than 20. My own uninformed opinion might be that google searches use to push the Sun forums to the top. For whatever reason.
crunchor wrote: They screw up then I don't know where to ask java questions.
There are other places but none with the diversity that the Sun forums had.
|
|
|
|
|
Is there any other framework in java to play music ? I have currently used JMF .But it can play only .mp3 and .mpg format.I can not play all the format like professional media player.I just want to build a player can play most of the format.Is there any way in java with enough documentations.Plz help me someone.
|
|
|
|
|
|
Thanks.Then what will be my technique with conversion.
|
|
|
|
|
|
Try searching for codec java libraries. Usually they need JMF to be used.
Like this one:
JFFMPEG[^]
|
|
|
|
|
static Connection Connectionstring()
{
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
String conUrl = "jdbc:sqlserver://localhost;databaseName=paytest;user=PC;password=;integratedSecurity=true";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
I have got this error
Exception in thread "main" java.lang.NoSuchFieldError: address
at java.net.InetAddress.init(Native Method)
at java.net.InetAddress.<clinit>(Unknown Source)
at java.net.InetSocketAddress.<init>(Unknown Source)
at com.microsoft.sqlserver.jdbc.SocketFinder.getDefaultSocket(IOBuffer.java:2430)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2094)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at SpringSample.Connectionstring(SpringSample.java:290)
at SpringSample.main(SpringSample.java:120)
How to connect ?
|
|
|
|
|
You should check the port where sqlserver is installed and specify the port number. Ex
jdbc:sqlserver://localhost:1433;databaseName=paytest;user=PC;password=;integratedSecurity=true
|
|
|
|
|
If I use this code in a different class(Just the connection code)it works fine with
jdbc:sqlserver: this code
but when I'm using it with GUI it does not work ?I hope connection string is not related to GUI I'm using ?
|
|
|
|
|
chdboy wrote: I hope connection string is not related to GUI I'm using ? How do you think it could be? It is much more likely that you have made a mistake in one application that is not in the other one. Try stepping through the code with your debugger to trace the exact sequence of events when you try to connect.
Use the best guess
|
|
|
|
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public abstract class ConnectionDB
{
public static void main(String[] args)
{
try {
insertRecordIntoDbUserTable();
selectfromdb();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void insertRecordIntoDbUserTable() throws SQLException
{
Connection con = null;
Statement statement = null;
String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
try
{
con = DBConnectionstring();
statement = con.createStatement();
System.out.println(insertTableSQL);
statement.executeUpdate(insertTableSQL);
JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
}
if (con != null)
{
con.close();
JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
}
}
}
private static void selectfromdb() throws SQLException
{
Statement stmt = DBConnectionstring().createStatement();
ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
while (rs.next())
{
String lastName = rs.getString("Username");
String Pass = rs.getString("Password");
System.out.println(lastName + "" + Pass + "\n");
}
}
static Connection DBConnectionstring()
{
Connection con = null;
String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
}
The only difference is that this is an Abstract class.
And this is the code I'm using now with copy for connection string to the above code .
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.Container;
import javax.swing.JOptionPane;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class SpringSample extends Frame implements ActionListener
{
JLabel F_namelbl = new JLabel("Username: ");
JLabel L_namelbl = new JLabel("Password: ");
JTextField tf_Fname = new JTextField(15);
JPasswordField tf_Lname = new JPasswordField(15);
JLabel Lbl_HW = new JLabel("< Login Area >");
JButton Btn = new JButton("Login");
JButton btn_cancel = new JButton("Cancel");
private static final long serialVersionUID = 1L;
SpringSample()
{
JFrame frame = new JFrame("Login Area!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JPanel p = new JPanel(new SpringLayout());
SpringLayout layout = (new SpringLayout());
contentPane.setLayout(layout);
JPanel p1=new JPanel();
frame.add(p1);
contentPane.add(F_namelbl);
contentPane.add(L_namelbl);
contentPane.add(tf_Fname);
contentPane.add(tf_Lname);
contentPane.add(Lbl_HW);
contentPane.add(Btn);
contentPane.add(btn_cancel);
layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, F_namelbl, 30, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, F_namelbl, 25, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, L_namelbl, 30, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, L_namelbl, 50, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.NORTH, tf_Fname, 25, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, tf_Fname, 15, SpringLayout.EAST, F_namelbl);
layout.putConstraint(SpringLayout.NORTH, tf_Lname, 50, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, tf_Lname, 15, SpringLayout.EAST, F_namelbl);
layout.putConstraint(SpringLayout.NORTH, Btn, 80, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, Btn, 110, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, btn_cancel, 80, SpringLayout.NORTH, p1);
layout.putConstraint(SpringLayout.WEST, btn_cancel, 200, SpringLayout.WEST, p1);
btn_cancel.addActionListener(this);
Btn.addActionListener(this);
frame.pack();
frame.setSize(350, 200);
frame.setVisible(true);
}
public static void main(String args[])
{
new SpringSample();
Connectionstring();
try {
insertRecordIntoDbUserTable();
selectfromdb();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
static Connection Connectionstring()
{
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
private static void insertRecordIntoDbUserTable() throws SQLException
{
Connection con = null;
Statement statement = null;
String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
try
{
con = Connectionstring();
statement = con.createStatement();
System.out.println(insertTableSQL);
statement.executeUpdate(insertTableSQL);
JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
}
if (con != null)
{
con.close();
JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
}
}
}
private static void selectfromdb() throws SQLException
{
Statement stmt = Connectionstring().createStatement();
ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
while (rs.next())
{
String lastName = rs.getString("Username");
String Pass = rs.getString("Password");
System.out.println(lastName + "" + Pass + "\n");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn_cancel)
{
JOptionPane.showMessageDialog(null,"You have clicked on Cancel","button cancel",JOptionPane.WARNING_MESSAGE);
}
else if(e.getSource() == Btn)
{
JOptionPane.showMessageDialog(null,"You have clicked login","button login",JOptionPane.WARNING_MESSAGE);
}
}
}
|
|
|
|
|