Results 1 to 10 of 10
Thread: Centering inside a JFrame
- 09-23-2009, 03:09 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Centering inside a JFrame
I'm trying to make an app center when it comes up. I'm also trying to make the TextArea inside of the app centered. I've tried several things but have been unable to get any of them to work. I've looked up several things online to try and haven't been able to get any of them to successfully work.
I left some coding that I had commented out after I couldn't get it to work.Java Code:import java.awt.*; import javax.swing.*; class SimpleJavaGUI extends JFrame { public JTextArea theTextArea; public SimpleJavaGUI() { super("mainForSimpleGUI"); setLayout(new FlowLayout() ); theTextArea = new JTextArea(2, 20); theTextArea.setAlignmentX(CENTER_ALIGNMENT); theTextArea.setAlignmentY(CENTER_ALIGNMENT); theTextArea.setText(" Follow the white rabbit."); theTextArea.setToolTipText("This is a text area."); theTextArea.setEditable(false); theTextArea.setDisabledTextColor(Color.BLACK); theTextArea.setLineWrap(true); theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) ); add(theTextArea); } }
Java Code:import javax.swing.*; import java.awt.*; public class mainForSimpleGUI { public static void main(String[] args) { SimpleJavaGUI labelFrame = new SimpleJavaGUI(); //creates SimpleJavaGUI labelFrame.setLocationRelativeTo(null); labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); labelFrame.setSize(300, 300); labelFrame.setVisible(true); } } /* class SimpleJavaGUI extends JFrame { public JTextArea theTextArea; public SimpleJavaGUI() { super("mainForSimpleGUI"); setLayout(new FlowLayout() ); theTextArea = new JTextArea(2, 20); theTextArea.setLocation(30, 30); //theTextArea.setAlignmentX(CENTER_ALIGNMENT); //theTextArea.setAlignmentY(CENTER_ALIGNMENT); theTextArea.setText(" Follow the white rabbit."); theTextArea.setToolTipText("This is a text area."); theTextArea.setEditable(false); theTextArea.setDisabledTextColor(Color.BLACK); theTextArea.setLineWrap(true); theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) ); add(theTextArea); } } */
- 09-23-2009, 03:59 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Specify what the problem is.
What is the code doing vs what it is supposed to be doing?
- 09-23-2009, 04:10 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
The app is not centering on the screen when it appears, nor is the TextArea centering inside of the app.
-
To center the app, use setPreferredSize, not setSize, pack the app, and only then set the location relative to null:
One way to center a single component is to have the Container that holds it use a GridBagLayout.Java Code:public static void main(String[] args) { SimpleJavaGUI labelFrame = new SimpleJavaGUI(); //creates SimpleJavaGUI labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); labelFrame.setPreferredSize(new Dimension(300, 300)); labelFrame.pack(); labelFrame.setLocationRelativeTo(null); labelFrame.setVisible(true); }
- 09-23-2009, 04:58 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
What is the difference between setSize and setPreferredSize?
- 09-23-2009, 05:03 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
That is why they wrote the API specs for those methods.
All you have to do is to read them.
- 09-23-2009, 05:25 PM #7
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Centering the window
setLocationRelativeTo(null) worked for me, but it centered the upper lefthand corner.
Here is a method that has worked for me in the past:
blog.codebeach.com/2008/02/center-dialog-box-frame-or-window-in.html
Centering text in a label
java.sun.com/docs/books/tutorial/uiswing/components/label.html
Centering a component in a window
GridBagLayout can do the job. Usually it is easier to write your own LayoutManager instead of fighting with AWT or Swing.
Setting size
Preferred, minimum and maximum size are hints to the layout manager as to how big the component would like to be. setSize() is what the layout manager calls after it has decided how big it can allow the component to be.
- 09-23-2009, 05:33 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I'm the first to recommend reading the API, but its not always very helpfull as it isn't in this case.That is why they wrote the API specs for those methods.
You need to understand how layout managers work to understand the difference. The preferred size is a suggestion for the layout manager. Based on the suggestion the layout manager will use its rulse to set the size and location of the component. Read this section from the Swing tutorial for more information:What is the difference between setSize and setPreferredSize?
java.sun.com/docs/books/tutorial/uiswing/layout/using.html#sizealignment
- 09-23-2009, 05:37 PM #9
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Writing a layout manager is often straightforward.
For your example, replace "setLayout(new FlowLayout() );" with:
Java Code:setLayout(new LayoutManager() { public void addLayoutComponent(String name, Component comp) {} public void removeLayoutComponent(Component comp) {} public Dimension preferredLayoutSize(Container parent) { return theTextArea.getPreferredSize(); } public Dimension minimumLayoutSize(Container parent) { return theTextArea.getMinimumSize(); } public void layoutContainer(Container parent) { int pW = parent.getWidth(), pH = parent.getHeight(); int tW = theTextArea.getPreferredSize().width, tH = theTextArea.getPreferredSize().height; theTextArea.setBounds((pW-tW)/2, (pH-tH)/2, tW, tH); } });
- 09-23-2009, 07:23 PM #10
Member
- Join Date
- Sep 2009
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Launch native app inside a JFrame???
By benedums in forum Advanced JavaReplies: 6Last Post: 02-25-2011, 03:23 PM -
How to add JFrame inside JPanel
By niteshwar.bhardwaj in forum Java 2DReplies: 8Last Post: 12-13-2009, 08:41 PM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
Centering text of output
By dch414 in forum New To JavaReplies: 2Last Post: 10-02-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks