Solved

Java Focus & Print Questions

Posted on 2003-11-23
31
1,029 Views
Last Modified: 2010-03-31
Hello Friends;

I'm having some few troubles about my program. I'm creating my very own Java IDE >__<
I'm very a happy about it but in my persuit to finish it, there are some troubles that I had encountered.

Anyways I'm gonna realese it for free, if I can finish it :) I hope so . . . So pls. guys give me
some support. I'm gonna put the names of every person who can and who will help me with
this project on the Java IDE which I'm creating. Also I'm gonna include my home Experts -
Exchange.

Here is my 3 Questions :

1.) I have a JInternalFrame which will pop-out if the use created a new project. My problem is
     I dont know how to set it to focus, silly question :)
 
     JInternalFrame f1 = new JInternalFrame();

     // I've tried :
     f1.requestFocus();
     // Also :
     f1.requestFocusInWindow();

     But does not work. If I try those in buttons or a JTextPane it works but even if it works
     the JInternalFrame is not highlighted or focused, how can I solved this ?

2.) I have a print method that takes a lot of memory. If I triggered the print button, it displays
     the print dailog but my main program which is the IDE becomes distorted. If I try to move
     the print dailog on my main program all I can see is a gray screen. Is there any way that I
     could repaint my IDE so that when I print it will not be distorted.

     here's my method :

     // Printing Method

     private void print()
     {
     int exit = JOptionPane.showConfirmDialog
     (null,"Are you sure you want to print ?",
     "Timeport . . .",JOptionPane.YES_NO_OPTION);
 
     if(exit==JOptionPane.YES_OPTION)
     {
     printPage(t1.getText());
     }
     }

     public void printPage(String textToPrint) {

     PrinterJob job = PrinterJob.getPrinterJob();
     PageFormat landscape = job.defaultPage();

     landscape.setOrientation(PageFormat.LANDSCAPE);
     Book bk = new Book();
     bk.append(new PaintCover(), job.defaultPage());

     job.setPageable(bk);

     if (job.printDialog()) {
     try {
     job.print();
     }catch (Exception exc){
     System.out.println(exc);
     }
     }
     }

     class PaintCover implements Printable{
     Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 12);

     public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
     g.setFont(fnt);
     g.setColor(Color.black);
     g.drawString(t1.getText(), 100, 200);
     return Printable.PAGE_EXISTS;
     }
     }
     
3.) After printing I tried to close my JInternalFrame but when I tried to click on the print
     button, it still prints the contents on my previous project. So my conclusion is that it
     remains on the buffer until I tried to print another project. How can I remove the
     print action on the buffer afte printing ?. In jdbc I use this close(); but when I tried
     it, it wont work.

Everyhelp will be appriciated, I know 3 questions is not allowed but I'll give more points
so pls. help :)

Your Friend :
JAVATM
0
Comment
Question by:Javatm
  • 14
  • 13
  • 4
31 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9805848
2. Try doing the printing in a separate dedicated thread. This may have an impact on the other two problems, so try that first.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9805873
>> Try doing the printing in a separate dedicated thread.
   
      The printing process is a separete actionlistener and will only run when I triggered
      the print button.

      Here is a suedo code :

      create.action {
      create();
      }
 
      save.action {
      save();
      }

      open.action {
      open();
      }
 
      print.action {
      print();
      }
   
      I made every method for creating, saving, opening & printing. I just call them one
      by one depending on the need of the user.
0
 
LVL 92

Expert Comment

by:objects
ID: 9806881
> My problem is I dont know how to set it to focus

try:
f1.toFront();
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 9806891
2) Swing is single threaded, if you perform any lengthy process in this thread then the gui will be blocked until that process is complete.

3) post your print code
0
 
LVL 15

Author Comment

by:Javatm
ID: 9808220
I tried f1.toFront(); but still did not focus the JInternalFrame ehm I doing something
wrong ?

Here is my create method if the user creates new project :

  private void create()
  {
  try {
  String s1 = JOptionPane.showInputDialog(null,"Enter the Projects Name ?",
  "Timeport . . .",JOptionPane.QUESTION_MESSAGE);

  if(s1.equals("")) {
  JOptionPane.showMessageDialog(null,"No Value not Allowed !",
  "Timeport . . .",JOptionPane.WARNING_MESSAGE);
  }
  else {
  t1 = new JTextPane();
  JInternalFrame f1 = new JInternalFrame(""+s1,false,true,false,true);
  Container c = f1.getContentPane();
  c.add( new JScrollPane(t1) );

  f1.setSize(470, 270);
  f1.toFront();
  f1.setVisible(true);
  x1.add(f1);
 
  t1.requestFocusInWindow();
 
  }
  }
  catch(Exception e) {
  JOptionPane.showMessageDialog(null,"No Project Created !",
  "Timeport . . .",JOptionPane.INFORMATION_MESSAGE);
  }
  }

>>  if you perform any lengthy process in this thread then the gui will be blocked until that  
       process is complete.

       Thats what I figured out :) anyways my print code is the same as the codes on my
       Question number 2 which is my print method.

       I just call the method on my actionlistener like :

       printbutton.action {
       print();
       }

Thank you . . .
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9808400
try:

frame.setSelected(true);
0
 
LVL 92

Expert Comment

by:objects
ID: 9808404
can u post print() method?
0
 
LVL 15

Author Comment

by:Javatm
ID: 9809167
My print method is the same as above :)
     
// Printing Method

     private void print()
     {
     int exit = JOptionPane.showConfirmDialog
     (null,"Are you sure you want to print ?",
     "Timeport . . .",JOptionPane.YES_NO_OPTION);
 
     if(exit==JOptionPane.YES_OPTION)
     {
     printPage(t1.getText());
     }
     }

     public void printPage(String textToPrint) {

     PrinterJob job = PrinterJob.getPrinterJob();
     PageFormat landscape = job.defaultPage();

     landscape.setOrientation(PageFormat.LANDSCAPE);
     Book bk = new Book();
     bk.append(new PaintCover(), job.defaultPage());

     job.setPageable(bk);

     if (job.printDialog()) {
     try {
     job.print();
     }catch (Exception exc){
     System.out.println(exc);
     }
     }
     }

     class PaintCover implements Printable{
     Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 12);

     public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
     g.setFont(fnt);
     g.setColor(Color.black);
     g.drawString(t1.getText(), 100, 200);
     return Printable.PAGE_EXISTS;
     }
     }
0
 
LVL 15

Author Comment

by:Javatm
ID: 9809185
>> frame.setSelected(true);
 
      I tried it and it worked, thanks :)
      It gave a focus on my JInternalFrame, now my problem is just the
      printing process.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9809221
The problem I have in my print method is that :

1.) If the user clicks the print button it displays the print dailog which if I
     move around will distort my main GUI. What I see when I move the
     print dailog is just a gray screen.

     I tried repaint(); but did not worked.

2.) The last problem is that the print remains on the buffer. I want it to be
     erased after printing. Even if I close the new project and then I
     click on the print button it still prints thats why I want it to be erased
     after printing.

Dude, My last problem is the one which I wanna solved. You can try to work
on the 2 remaining problems but the last is the most important.
 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9809949
Have you moved the printing into its own dedicated thread as i mentioned?
0
 
LVL 15

Author Comment

by:Javatm
ID: 9813913
>> Have you moved the printing into its own dedicated thread as i mentioned?
 
      I dont really know what you mean ? can you elaborate it more. As I said my print method
      only prints when I triggered my print button, so its on its own. So I guess its on it own
      separate thread.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 9813980
public void actionPerformed(Event e) {
      if (e.getSource() == yourPrintButton) {
            Printer printer = new Printer();
            Thread printThread = new Thread(printer);
            printThread.start();
      }
}            


// inner class            
class Printer implements Runnable {
           public void run() {
            print()
           }    
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9814119
> public void printPage(String textToPrint) {

You pass the text to printPage but you never actually use it, and instead get text direct from component. Is this intended?
0
 
LVL 15

Author Comment

by:Javatm
ID: 9815532
Objects;

>> You pass the text to printPage but you never actually use it

      How did you know that ?

      What I did was to get the text on my Text Area then
      transfer it to printPage, is it wrong ?

      if(exit==JOptionPane.YES_OPTION)
      {
      printPage(t1.getText());
      }
      }

      public void printPage(String textToPrint) {
      }

CEHJ;

      I'm gonna try your other solution, hope it works :)

Thank you . . .
JAVATM

0
 
LVL 92

Expert Comment

by:objects
ID: 9815555
> public void printPage(String textToPrint) {

But textToPrint var is never used that I can see.
The string you draw is retrieved from the text field.

g.drawString(t1.getText(), 100, 200);
0
 
LVL 15

Author Comment

by:Javatm
ID: 9815738
>> The string you draw is retrieved from the text field.

      Okey I'm gonna try it like :

      public void printPage() {
      }

     How about cleaning the buffer after printing how ehm I gonna
     do that ?

     Or is there an exit or close function after printing ? so that ones
     it is finish printing, it automatically clean's the buffer.



0
 
LVL 92

Expert Comment

by:objects
ID: 9815754
What buffer exactly are you referring to?
0
 
LVL 92

Expert Comment

by:objects
ID: 9815764
> Okey I'm gonna try it like :
> public void printPage() {

What difference will that make?
0
 
LVL 92

Expert Comment

by:objects
ID: 9815780
You sure its not just the first job printing multiple copies of the same page cause you never return NO_SUCH_PAGE.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9815887
>> What difference will that make?

     Can you re-arrange my printing method cause the last time I tried fixing it, it did not print.

>> What buffer exactly are you referring to?

      Here is the scenario : If the user clicks on the print button it prints. When the user close the
      textarea and clicks the print button it still prints the previous printing job unless I tried to print
      another.

      What I wanna do is to print the page ones and then close it. So that if the user close the
      textarea and clicks the print button it will display a message like : no page to print.

Hope it add more info . . .
0
 
LVL 92

Expert Comment

by:objects
ID: 9815899
try:

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
     if (pageIndex>0) return NO_SUCH_PAGE;
     g.setFont(fnt);
     g.setColor(Color.black);
     g.drawString(t1.getText(), 100, 200);
     return Printable.PAGE_EXISTS;
}
0
 
LVL 15

Author Comment

by:Javatm
ID: 9815949
Okey thanks a lot guys I'm gonna try it later cause I'm still at work ;)
No Java alllowed until I go home, anyways as I can see your 24 hrs a day.
Very much amazing ;)

I'm gonna bring back the result 2-3 hrs from now.
Friend to Keep :
JAVATM

0
 
LVL 92

Expert Comment

by:objects
ID: 9816428
I'd suggest turning off double buffering while you're printing to improve performance:

RepaintManager manager = RepaintManager.currentManager(comp);
manager.setDoubleBufferingEnabled(false);
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 9816438
I don't think you actually need to (or perhaps even should be) do your print from a seperate thread. The problem you are seeing looks like it's actually a bug:
http://developer.java.sun.com/developer/bugParade/bugs/4507585.html
Though performing it from a seperate thread is mentioned as a workaround, but this could possibly cause it's own problems.
0
 
LVL 92

Expert Comment

by:objects
ID: 9816448
Bugs supposed to be fixed in tiger.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9816896
Ok I think its a bug, anyways friends I'm gonna try to do it in a separate thread
but before I do that, I wanna ask something !.

You said a while ago :

--- Okey I'm gonna try it like :
--- public void printPage() {
>> What difference will that make?

Now My question is how do I print the contents of my textarea without casting it
to my :

     public void printPage(String textToPrint) {  
 
I tried sevaral things but failed the reason I use printPage is for me
to transfer the contents of my textarea so that I can print it. Can you
re-arrange my codes thanks. This will be my final question and if
answered I'm gonna close this question.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9817083
Friends;

Thank you for the help, I already solved the problem :

1.) For my question no#1 the answer is :
     f1.setSelected(true);

2.) For my question no#2 the answer is :
     Running it using " seperate thread ".

3.) For my question no#3 the answer is :
     I throwed an error message when there are
     no page to print.

Objects & CEHJ both of you guys did solved my problems.
Thank you very much. As I promised I'm gonna include your names
on my Java IDE after I finished it. Thank you very much & God Bless :)

Friend : Javatm
0
 
LVL 15

Author Comment

by:Javatm
ID: 9817121
I splitted the 500 pts for the 2 of you guys.

250 - Objects &
250 - CEHJ

Thank you again
Javatm
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9817401
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9820399
> Running it using " seperate thread ".

According to the reports this may not actually totally fix it but give it a try.
Though it may potentially introduce other Swing threading problems.


:)
http://www.objects.com.au
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 107
Opening PDF on button click and fill new document 2 47
by zero exception 10 53
java imports not found 4 30
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question