Solved

Setting JEditorPane central to my screen

Posted on 2004-08-30
17
408 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
Comment Utility
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((d.getWidth() - f.getWidth()) / 2, d.getHeight() - f.getHeight()) / 2);
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
Easy way:
f.setLocationRelativeTo(null);
(I'd do that after defining it's size, by the way).
:D
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
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
Comment Utility
Good to hear that !
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
:)  
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'd help but:
A. What's a UML diagram?
and
B. I'm on a Mac ;)
0
 
LVL 8

Author Comment

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

16 Experts available now in Live!

Get 1:1 Help Now