• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1037
  • Last Modified:

Java Focus & Print Questions

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
Javatm
Asked:
Javatm
  • 14
  • 13
  • 4
3 Solutions
 
CEHJCommented:
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
 
JavatmAuthor Commented:
>> 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
 
objectsCommented:
> My problem is I dont know how to set it to focus

try:
f1.toFront();
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
try:

frame.setSelected(true);
0
 
objectsCommented:
can u post print() method?
0
 
JavatmAuthor Commented:
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
 
JavatmAuthor Commented:
>> 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
 
JavatmAuthor Commented:
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
 
CEHJCommented:
Have you moved the printing into its own dedicated thread as i mentioned?
0
 
JavatmAuthor Commented:
>> 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
 
CEHJCommented:
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
 
objectsCommented:
> 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
 
JavatmAuthor Commented:
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
 
objectsCommented:
> 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
 
JavatmAuthor Commented:
>> 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
 
objectsCommented:
What buffer exactly are you referring to?
0
 
objectsCommented:
> Okey I'm gonna try it like :
> public void printPage() {

What difference will that make?
0
 
objectsCommented:
You sure its not just the first job printing multiple copies of the same page cause you never return NO_SUCH_PAGE.
0
 
JavatmAuthor Commented:
>> 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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
I'd suggest turning off double buffering while you're printing to improve performance:

RepaintManager manager = RepaintManager.currentManager(comp);
manager.setDoubleBufferingEnabled(false);
0
 
objectsCommented:
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
 
objectsCommented:
Bugs supposed to be fixed in tiger.
0
 
JavatmAuthor Commented:
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
 
JavatmAuthor Commented:
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
 
JavatmAuthor Commented:
I splitted the 500 pts for the 2 of you guys.

250 - Objects &
250 - CEHJ

Thank you again
Javatm
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
> 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 14
  • 13
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now