Solved

Java Focus & Print Questions

Posted on 2003-11-23
31
1,020 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
> My problem is I dont know how to set it to focus

try:
f1.toFront();
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
try:

frame.setSelected(true);
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
can u post print() method?
0
 
LVL 15

Author Comment

by:Javatm
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Have you moved the printing into its own dedicated thread as i mentioned?
0
 
LVL 15

Author Comment

by:Javatm
Comment Utility
>> 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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
>> 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
Comment Utility
What buffer exactly are you referring to?
0
 
LVL 92

Expert Comment

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

What difference will that make?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Bugs supposed to be fixed in tiger.
0
 
LVL 15

Author Comment

by:Javatm
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now