Solved

Setting JEditorPane central to my screen

Posted on 2004-08-30
17
410 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 82
object oriented programming comparison 5 69
Desingning Refactoring existing code 2 24
going to wrong jsp page 2 18
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 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