?
Solved

Setting JEditorPane central to my screen

Posted on 2004-08-30
17
Medium Priority
?
420 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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