• Post Reply Bookmark Topic Watch Topic
  • New Topic

clicking a button to run a class  RSS feed

 
Luke Smtih
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is is possible to make a button using the JButton method, that when the button is clicked it runs another class?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "runs another class" mean?

Perhaps you're wanting to learn the basics of event handling. For that, I'd suggest moseying on over to the Swing / JFC / AWT forum. You also might like to read up on the subject, starting in chapter 57 of Bradley Kjell's Introduction to Computer Science using Java.
 
Luke Smtih
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i was a bit vague.. ok all i want to know is if it is possible to have a button in one java program that when clicked will run another java program that i have made.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Luke Smtih:
sorry i was a bit vague.. ok all i want to know is if it is possible to have a button in one java program that when clicked will run another java program that i have made.


It's vague again. Do u mean to run your class in a new JVM, when a button was clicked? Or you just need to run a new class, popping up with a new window?

If you can specify the thing that you want in details, it would be better for us to be able to explain you clearly...
 
Luke Smtih
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha ok third time lucky haha

ok i have a program which just has a text input box at the top which allows the user to enter an SQL statement to search through other tables in the database or add tables etc etc. I want to put a button on it which, when clicked, will run another java file that i have which is for caculating the difference between two dates.

im not to suer what u mean by JVM is that java virtual machine? all i want it a to click a button which will run the other program in a different window.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps you are familiar with the method "public static void main(String[]args)" You've typed it a million times, but all that stuff has meaining! Main is a static method which takes an array of Strings as the arguments. One way to "run" a class from another one is to just call that main!

DateCalculator.main(new String[]);

try that out...

-Joe
 
Luke Smtih
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for the advice... i am getting two errors when i compile now, im not suer if i have put the "DateDiffGUIApplet.main(new String[]);" in the right spot or i need further code for it to run that file...

ERRORS
--------------------Configuration: <Default>--------------------
DisplayQueryResults.java:15: <identifier> expected
DateDiffGUIApplet.main(new String[]);
^
DisplayQueryResults.java:15: cannot resolve symbol
symbol : class main
location: class DateDiffGUIApplet
DateDiffGUIApplet.main(new String[]);
^
2 errors






[CODE]
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class DisplayQueryResults extends JFrame {
private ResultSetTableModel tableModel;
private JTextArea queryArea;

DateDiffGUIApplet.main(new String[]);



public DisplayQueryResults()
{

super("Displaying Query results");

String url = "jdbc.odbc:customers";

String query = "SELECT * FROM custDetails, videoHired";

try{
tableModel = new ResultSetTableModel(query);

queryArea = new JTextArea(query,3,100);
queryArea.setWrapStyleWord(true);
queryArea.setLineWrap(true);

JScrollPane scrollPane = new JScrollPane(queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

JButton submitButton = new JButton("Submit Query");
JButton linkButton = new JButton("link");
Box box = Box.createHorizontalBox();
box.add(scrollPane);
box.add(submitButton);

JTable resultTable = new JTable(tableModel);

Container c = getContentPane();
c.add(box, BorderLayout.NORTH);
c.add(new JScrollPane(resultTable),
BorderLayout.CENTER);

submitButton.addActionListener(
new ActionListener(){


public void actionPerformed(ActionEvent e)
{
try{
tableModel.setQuery(queryArea.getText());
}

catch (SQLException sqlException){
JOptionPane.showMessageDialog(null,
sqlException.toString(),
"Database error",
JOptionPane.ERROR_MESSAGE);
}
}
}

);



setSize(500,250);
setVisible(true);
}



catch (ClassNotFoundException classNotFound){
JOptionPane.showMessageDialog(null,
"Cloudscape driver not found","Driver not found",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}

catch (SQLException sqlException){
JOptionPane.showMessageDialog(null,
sqlException.toString(),
"Database error",JOptionPane.ERROR_MESSAGE);

System.exit(1);
}
}

public static void main(String args[])
{
DisplayQueryResults app = new DisplayQueryResults();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

[CODE]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your call to main is in a completely random spot. You want to make that call in response to something, so put it in the appropriate spot in a method somewhere.
 
Luke Smtih
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any suggestions on where???
 
shawn McFly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd guess you'd put it in an ActionListener for that particular button.... no?

Shawn

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!