0

I wish to center my scrollpane inside a Jframe but i cant seem to do it. I used the GUI builder in netbeans but i have edited the Alignment part to CENTER or Trailer, doesnt seem to do anything. I tried using the JScrollPane.setAlignment(CENTER) inside initcomponents but no luck there either.

layout.setHorizontalGroup(

        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)                              


                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(
                                                                        myScrollPane,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        760,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2999509
  • 97
  • 2
  • 4
  • 13
  • Hint: component.setAlignmentX(JComponent.CENTER_ALIGNMENT); use X,Y grid and use for the scroll panel in constructor the axis as a param. In your case X_AXIS. Links: http://stackoverflow.com/questions/6638753/java-how-to-center-vertical-column-of-components http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#alignment – Bogdan M. Apr 09 '14 at 09:20
  • oh where do i put this? under the .addcomponent part i guess? – user2999509 Apr 09 '14 at 10:15
  • check the links. >.> i just gave you all resources for you :\ – Bogdan M. Apr 09 '14 at 10:20

1 Answers1

1

Don't use an IDE to create your GUI. Learn how to create the GUI and write the code yourself. That way you learn Java and not the IDE.

The easiest way to center a component is to use a GridBagLayout:

frame.setLayout( new GridBagLayout() );
frame.add(scrollPane, new GridBagConstraints());
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I cannot agree more with you on this, hence my frustration. We are supposed to do an assignment on a GUI within a few weeks, it was tough mastering basic logic and I used the IDE to start up the GUI first. – user2999509 Apr 10 '14 at 16:33