Solved

Setting JEditorPane central to my screen

Posted on 2004-08-30
17
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

688 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