Solved

Setting JEditorPane central to my screen

Posted on 2004-08-30
17
409 Views
Last Modified: 2010-05-18
I am using a JEidor pane to display some user instructions in my game and I need a solution of how to set it central for the screen size.

Currently I am using the following code from a menuitem and the JEditor defaults to the top left corner of the screen and I don't want to do that :)

userInstruct.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("Instructions button pressed");
                //Create new Jframe to view User instructions
                JFrame f = new JFrame();
                JEditorPane p = new JEditorPane("text/html", "");
                f.getContentPane().add(p); // was missing in the previous code snippet
                p.setEditable(false);
                URL instructions = getClass().getResource("instructions/index.html");
                try {
                    p.setPage(instructions);
                }
               
                catch (Exception ep) {
                   
                    System.out.println("Not Got index.html");
                }

                f.setSize(265, 540);
                f.setVisible(true);
               
               
            }                      
        });

Please help if you can

pjcrooks2000
0
Comment
Question by:pjcrooks2000
  • 8
  • 3
  • 3
  • +1
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11936583
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((d.getWidth() - f.getWidth()) / 2, d.getHeight() - f.getHeight()) / 2);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11936616
Sorry

            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            f.setLocation((int) (d.getWidth() - f.getWidth()) / 2, (int) (d.getHeight() - f.getHeight()) / 2);
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11936643
heheh

nealry is sends it off the screen me ole chap, well down toward the bottom anyway :)  Looks like thats the idea though.  Any thoughts on why its going part off screen, central but offscreen at the bottom ?
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11936734
This is the correct way if you dont know how to implement it :

userInstruct.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.out.println("Instructions button pressed");
                //Create new Jframe to view User instructions

                JFrame f = new JFrame();
                JEditorPane p = new JEditorPane("text/html", "");
                f.getContentPane().add(p); // was missing in the previous code snippet
                p.setEditable(false);
                URL instructions = getClass().getResource("instructions/index.html");
                try {
                p.setPage(instructions);
                }
               
                catch (Exception ep) {                  
                System.out.println("Not Got index.html");
                }

                Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
                f.setLocation(sd.width / 2 - 625 / 2, sd.height / 2 - 450 / 2);

                f.setSize(265, 540);
                f.setVisible(true);                            
            }                      
        });
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11936740
Sorry Off Balancem, this is the correct one :

userInstruct.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.out.println("Instructions button pressed");
                //Create new Jframe to view User instructions

                JFrame f = new JFrame();
                JEditorPane p = new JEditorPane("text/html", "");
                f.getContentPane().add(p); // was missing in the previous code snippet
                p.setEditable(false);
                URL instructions = getClass().getResource("instructions/index.html");
                try {
                p.setPage(instructions);
                }
               
                catch (Exception ep) {                  
                System.out.println("Not Got index.html");
                }

                Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
                f.setLocation(sd.width / 2 - 265 / 2, sd.height / 2 - 540 / 2);

                f.setSize(265, 540);
                f.setVisible(true);                            
            }                      
        });
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11936765
>>nealry is sends it off the screen me ole chap

You must place that code *after* setting the frame size
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11936808
Will get back on this while I am asking this question I am finding I need to Change the JEDitor pane for  a JLabel.

Will post back once I have modified it sooo sorry!
0
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 500 total points
ID: 11936890
Easy way:
f.setLocationRelativeTo(null);
(I'd do that after defining it's size, by the way).
:D
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11936992
Yep i had that for another one, I am just trying to get a mouse click event and a Frame to show up because I change it to a JLabel instead of a JEditorPane... won't be long I hope :)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11937035
Good to hear that !
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11937207
:)  
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11937263
Guys have a look at this one if you feeling challenged :)

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21111604.html#11936136

Its been there a while ---
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11937625
Well I am back and have backtracked to my original code!

I have tried out the options here and I have gone with CI-Ia0s's solution as it is very simple and works a treat!

I hate having to do this but I have to award full points for that effot from CI-Ia0s

Very sorry to all about that,  Oooh i hate having to do this.  I will try make it up to the others  and can I say thank you very very much for your help

pjcrooks2000

ps.  Please check out the question above too :)
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11937644
Thanks. I posted a response to your other question, by the way... (this time objects and webstorm get the points ;) ).
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11937654
Ok coolio, I have another question too about UML diagrams :)  go for it! I am interesting in increasing my reporting productivity.  
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11937679
I'd help but:
A. What's a UML diagram?
and
B. I'm on a Mac ;)
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11937733
Hmm well its a diagram that is used to display a relational diagram between my classes/methods etc..

UML stands for the Unified Modelling Lanuguage, a nice book is larmans http://www.amazon.com/exec/obidos/ASIN/0130925691/qid%3D1023878103/sr%3D1-1/ref%3Dsr%5F1%5F1/002-4975162-0905668

Its worth a gander, its mostly used for large projects where a team of developers and building seperate parts of a project.

Larmans book also looks into Use Cases, Domain models, Sequence diagrams, and loads of other gubbins to do with software Engineering/design

Hope that teaches you something :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
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:

932 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

12 Experts available now in Live!

Get 1:1 Help Now