Pergunta

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)
Foi útil?

Solução

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());
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top