1

I'm working on a GUI, and I'm trying to format to look more appealing for the user, however; I've run into a few roadblocks, one being I can't figure out how I would properly center JButtons in an existing JPanel, I've managed to arrange them into rows, I would like to know some more about how I can really format these.(If you're interested in helping me in my other problem look here, How to set output stream to TextArea)

This is what I have currently.

If you click the link it will show you what I currently have in one of my tabs, I would like for the JButtons in the picture to be centered on the X-Axis instead of to the left as they are as a default setting I suppose.

So here's my code, it arranges vertically, I need all of the rows to be centered though.

package guipanel;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Dan
 */
public class GUIPanel extends JFrame {
    /**
     * Creates new form GUIPanel
     */
    public GUIPanel() {
        initComponents();
    }
    private void setOutputStream(boolean catchErrors) throws IOException {
        System.setOut(new PrintStream(new FilterOutputStream(new ByteArrayOutputStream()))); // catches System.out messages
        setVisible(true);
        requestFocus();
        if (catchErrors) {
               System.setErr(new PrintStream(new FilterOutputStream(new ByteArrayOutputStream())));
        }
    }
    private void setPosition(JPanel jPanel) {
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = gbc.CENTER;
        layout.setConstraints(jPanel, gbc);
        jPanel.setLayout(layout);
    }
    private void addTabs(JTabbedPane jTabbedPane1) throws IOException {
        jTabbedPane1.add("Main", jPanel);
        jPanel.add(textArea1);
        jTabbedPane1.add("Commands", jPanel1);
        jTabbedPane1.add("Rules", jPanel2);
        jTabbedPane1.add("Links", jPanel3);
        jPanel3.add(website);
        jPanel3.add(jSeparator1);
        jPanel3.add(youtube);
        jPanel3.add(jSeparator2);
        jPanel3.add(webclient);
        jPanel3.add(jSeparator4);
        jPanel3.add(forums);
        jTabbedPane1.add("Information", jPanel4);
        jTabbedPane1.add("Shutdown", jPanel5);
        jPanel5.add(Shutdown);
        jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.PAGE_AXIS));
        setPosition(jPanel5);
        setOutputStream(true);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        textArea1 = new java.awt.TextArea();
        jLabel1 = new javax.swing.JLabel();
        website = new javax.swing.JButton();
        youtube = new javax.swing.JButton();
        forums = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        webclient = new javax.swing.JButton();
        Shutdown = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        textArea1.setPreferredSize(new java.awt.Dimension(432, 300));
        textArea1.getAccessibleContext().setAccessibleParent(jTabbedPane1);

        jLabel1.setText("Evolution-X™ Programmed by Musicmasta.");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        website.setText("Website");
        website.setMaximumSize(new java.awt.Dimension(133, 37));
        website.setPreferredSize(new java.awt.Dimension(133, 37));
        website.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        website.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                websiteActionPerformed(evt);
            }
        });

        youtube.setText("Youtube");
        youtube.setMaximumSize(new java.awt.Dimension(133, 37));
        youtube.setPreferredSize(new java.awt.Dimension(133, 37));
        youtube.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                youtubeActionPerformed(evt);
            }
        });

        forums.setText("Forums");
        forums.setMaximumSize(new java.awt.Dimension(133, 37));
        forums.setPreferredSize(new java.awt.Dimension(133, 37));
        forums.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        forums.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                forumsActionPerformed(evt);
            }
        });
        forums.getAccessibleContext().setAccessibleName("Forums");

        webclient.setText("Webclient");
        webclient.setMaximumSize(new java.awt.Dimension(133, 37));
        webclient.setName(""); // NOI18N
        webclient.setPreferredSize(new java.awt.Dimension(133, 37));
        webclient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                webclientActionPerformed(evt);
            }
        });
        webclient.getAccessibleContext().setAccessibleName("");

        Shutdown.setText("Shutdown");
        Shutdown.setMaximumSize(new java.awt.Dimension(221, 81));
        Shutdown.setPreferredSize(new java.awt.Dimension(221, 81));
        Shutdown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ShutdownActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Evolution-X 639");
        setBounds(new java.awt.Rectangle(0, 0, 400, 450));
        setPreferredSize(new java.awt.Dimension(450, 420));
        getContentPane().setLayout(new java.awt.FlowLayout());

        try {
            addTabs(jTabbedPane1);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        jTabbedPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTabbedPane1.setFocusable(false);
        jTabbedPane1.setOpaque(true);
        getContentPane().add(jTabbedPane1);
        jTabbedPane1.getAccessibleContext().setAccessibleName("");

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        System.exit(0);
    }                                      

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    private void youtubeActionPerformed(java.awt.event.ActionEvent evt) {
        Desktop desktop = Desktop.getDesktop();
        try {
            youtubelink = new URI("http://youtube.com/user/theevolutionx10");
            try {
                desktop.browse(youtubelink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void websiteActionPerformed(java.awt.event.ActionEvent evt) {
        Desktop desktop = Desktop.getDesktop();
        try {
            websitelink = new URI("http://evolutionx.x10.mx/");
            try {
                desktop.browse(websitelink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void forumsActionPerformed(java.awt.event.ActionEvent evt) {

        Desktop desktop = Desktop.getDesktop();
        try {
            forumlink = new URI("http://www.evolutionx.x10.mx/forum/index.php");
            try {
                desktop.browse(forumlink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void webclientActionPerformed(java.awt.event.ActionEvent evt) {
        Desktop desktop = Desktop.getDesktop();
        try {
            webclientlink = new URI("http://evolutionx.x10.mx/play.html");
            try {
                desktop.browse(webclientlink);
            } catch (IOException ex) {
                Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (URISyntaxException ex) {
            Logger.getLogger(GUIPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void ShutdownActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPanel().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton Shutdown;
    private javax.swing.JButton forums;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private java.awt.TextArea textArea1;
    private javax.swing.JButton webclient;
    private javax.swing.JButton website;
    private javax.swing.JButton youtube;
    // End of variables declaration
    private JPanel jPanel = new JPanel();
    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();
    private JPanel jPanel3 = new JPanel();
    private JPanel jPanel4 = new JPanel();
    private JPanel jPanel5 = new JPanel();
    private URI webclientlink, youtubelink, forumlink, websitelink;

}
4
  • And what was wrong with answer I gave for your last question Oct 18, 2012 at 23:55
  • Could you explain a little more what do you need? I see four buttons on Links tab but I'm not sure what do you want. Oct 18, 2012 at 23:56
  • @MadProgrammer, It wasn't printing anything to the textArea. Yeah, sure thing.
    – Dan
    Oct 18, 2012 at 23:58
  • @Dan, Really, works fine for me. Post an example in the previous question so I can evaluate it further Oct 18, 2012 at 23:59

1 Answer 1

3

You only have to add this lines before add the buttons to the tab:

website.setAlignmentX(Component.CENTER_ALIGNMENT);
youtube.setAlignmentX(Component.CENTER_ALIGNMENT);
webclient.setAlignmentX(Component.CENTER_ALIGNMENT);
forums.setAlignmentX(Component.CENTER_ALIGNMENT);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.