|
Sorry my bad with Copy/Paste
Here is the code once again.
public class Billing extends Frame implements ActionListener,Printable
{
JButton printbtn = new JButton("Print");
JFrame frame = new JFrame();
PrinterJob job = PrinterJob.getPrinterJob();
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
if (pageIndex > 0)
{
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
graphics.drawString("Hello world!", 100, 100);
return PAGE_EXISTS;
}
public Billing()
{
JPanel panel = new JPanel();
panel.setLayout(new MigLayout("debug,fillx,filly", "[]rel:push[]", "[]1[]"));
panel.add(printbtn);
frame.add(panel);
printbtn.addActionListener(this);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - 760)/2);
int y=(int)((dimension.getHeight() - 550)/2);
frame.setLocation(x, y);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
new Billing();
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(printbtn))
{
System.out.println("You have clicked on Print Button!");
PrinterJob pj = PrinterJob.getPrinterJob();
job.setPrintable(this);
if (pj.printDialog() == true)
{
try
{
pj.print();
}
catch (PrinterException exc)
{
exc.printStackTrace();
}
}
System.out.println("Print Job Ends Here!!!!XXXXX!!!!");
}
}
}
}
|
|
|
|
|
So two copies of your code but with no detail about your problem. Please explain exactly what you are trying to do, what results you expect, and what results you see, including the full text of any error messages.
Veni, vidi, abiit domum
|
|
|
|
|
I'm able to print Hello world with the help of my printer machine.
But with that code I don't get any error messages,but I'm able to print those two lines in the Console
1.You have clicked on Print Button!
2.Print Job Ends Here!!!!XXXXX!!!!
But not Hello world from this code
graphics.drawString("Hello world!", 100, 100);
What is wrong with this code?
|
|
|
|
|
|
I have got the solution
I have created two Printer Job
1.
PrinterJob job = PrinterJob.getPrinterJob();
2.
PrinterJob pj = PrinterJob.getPrinterJob();
And If I did this in my code
job.setPrintable(this);
I should not be doing this
if (pj.printDialog() == true)
pj.print();
Instead of "pj" I have to use "job".
that was the problem
I was not using the right job hehe.
|
|
|
|
|
how can i replace the two digits inside the number?
O/P:
536247982
After replacing
537246982
|
|
|
|
|
Write out, on paper, the two numbers and then the steps required to achieve the result. Then convert those steps in native language to Java code. It is really not that difficult. If you need some ideas then look at the Java Tutorial[^], particularly the section on numbers and strings.
Veni, vidi, abiit domum
|
|
|
|
|
How can i draw a curve in java? thank you.
|
|
|
|
|
|
Thanks alot.
|
|
|
|
|
Hi
I am facing this problem in my code and I'm unable to solve the problem. I can view data in utf-8 text file and there seems no problem. But what is in the RAF file, I cannot find. Thanks.
static void writeDataFile(String urText){
String strDataFilePath = "D:\\Documents\\JCreator Pro\\Font Images\\Images.dat";
String token[] = urText.split("\t");
RandomAccessFile file = null;
try {
file = new RandomAccessFile(strDataFilePath, "rw");
for (int i = 0; i < token.length; i++){
token[i] = token[i].replaceAll("\n", "");
file.writeInt(i);
int remain = i % 2;
if ( remain == 0){
file.writeUTF(token[i]);
}
else{
file.writeUTF(token[i]);
}
}
}
catch (IOException ex) {
System.out.println (ex);
}
finally{
try {
file.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
|
|
|
|
You need to look at the content of the data you are reading when the excption occurs, and the above code does not appear to be reading anything.
Veni, vidi, abiit domum
|
|
|
|
|
Thanks for reply. After sending the post I was able to settle the issue and that was just in the previous calling method.
|
|
|
|
|
I want a key Listener on a tabbed pane (Tab) so when I click on a tab I can do something.
I tried
public class Mainframe extends JFrame implements ActionListener,KeyListener
{
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addKeyListener(this);
tabbedPane.addTab("Tab 1 ",panel1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
tabbedPane.addTab("New Tab", panel2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_1)
JOptionPane.showMessageDialog(null, "You have selected ","Tab Clicked",JOptionPane.PLAIN_MESSAGE);
}
@Override
public void keyReleased(KeyEvent e) {
}
}
It does nothing.
|
|
|
|
|
|
Change Listener works fine .
|
|
|
|
|
do that example application using struts 1.x
|
|
|
|
|
That's called cascading comboboxes. Your favorite search engine can help you with this search term.
|
|
|
|
|
|
// For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.
Click here for .jar file.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQL {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://<SERVERNAME>","<uid>", "<pass>");
System.out.println("DATABASE NAME IS:"
+ connection.getMetaData().getDatabaseProductName());
Statement statement = connection.createStatement();
ResultSet resultSet = statement
.executeQuery("select * from dbo.try");
while (resultSet.next()) {
System.out.println(resultSet.getString("Name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
|
|
|
|
And what is your question?
Veni, vidi, abiit domum
|
|
|
|
|
excuse me sir, It's only article for information.
|
|
|
|
|
Very poor info.
Lots of glitch :
1. What happen if Connection return as null.
2. What happen if ResultSet return as null.
3. Finally if all run good, then where is the code to close the resources.
Regards
Shubhashish
|
|
|
|
|
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.ResultSet ;
import java.sql.Statement ;
import java.sql.SQLException;
class JdbcTestMssql {
public static void main (String args[]) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (ClassNotFoundException e) {
System.err.println (e) ;
System.exit (-1) ;
}
try {
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://192.168.100.68:1433;databaseName=master;user=ezadmin;password=ezadmin;");
String query = "SELECT * From sys.databases" ;
Statement statement = connection.createStatement () ;
ResultSet rs = statement.executeQuery (query) ;
while ( rs.next () )
System.out.println ("MS-SQL Query result: " + rs.getString ("name")) ;
connection.close () ;
}
catch (java.sql.SQLException e) {
System.err.println (e) ;
System.exit (-1) ;
}
}
}
|
|
|
|
|