Results 1 to 9 of 9
Like Tree5Likes
  • 1 Post By Fubarable
  • 2 Post By Fubarable
  • 2 Post By Tolls

Thread: Java GUI

  1. #1
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Question Java GUI

    Hello,

    I've had trouble placing my JButtons and JTextArea exactly where I want them. So I was wondering, is there any good GUI Builders out there?

    I'm trying to make a button appear in the SouthEast corner and besides that to the west another button. But it's hard positioning them exactly where I want them.

    All the different layouts does not seem to do that (GridBagLayout might, but it's way to complicated) So either is there any good GUI Builders out there? Or what could I do?

    Thanks,
    kris5228

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java GUI

    You may wish to simply nest simple layouts to get your desired effect, for example a BorderLayout with another JPanel on the bottom that holds the buttons and uses a BoxLayout.

  3. #3
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: Java GUI

    Let me just get that right. I make a panel that uses the BorderLayout.SOUTH and then add another layout type inside that layout? How? A link to the docs would be nice

    Thanks,

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java GUI

    If you want one button on the bottom right of your GUI and one on the bottom left, you could create a main JPanel that uses BorderLayout that holds most of the GUI in its BorderLayout.CENTER position, then add another JPanel to the BorderLayout.SOUTH position of the main JPanel that uses BoxLayout (line orientation), Then you could add one JButton, add a Box.createHorizontalGlue() (to create a gap between the two buttons), and then the other JButton.

    for example,

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.*;
    
    public class LayoutEg extends JPanel {
       JButton eastButton = new JButton("East Button");
       JButton westButton = new JButton("West Button");
       
       public LayoutEg() {
          JPanel southPanel = new JPanel();
          southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.LINE_AXIS));
          southPanel.add(westButton);
          southPanel.add(Box.createHorizontalGlue());
          southPanel.add(eastButton);
    
          setLayout(new BorderLayout());
          
          // just to fill in the middle
          add(Box.createRigidArea(new Dimension(400, 400)), BorderLayout.CENTER); 
          add(southPanel, BorderLayout.SOUTH);
    }
       
       private static void createAndShowGui() {
          JFrame frame = new JFrame("LayoutEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new LayoutEg());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    A link to the tutorial on this is here: Visual Guide to the Layout Managers
    Dark likes this.

  5. #5
    Paul_White is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Re: Java GUI

    If you need to put things exactly where you need them you could try something like this:

    Plan = new JFrame();
    Plan.setTitle("Myapp");
    Plan.setBounds(100, 100, 1102, 768);
    Plan.setIconImage(Toolkit.getDefaultToolkit().getI mage("Files/Logo.jpg"));
    Plan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    JDesktopPane desktopPane = new JDesktopPane();
    desktopPane.setBackground(new Color(173, 216, 230));
    Plan.getContentPane().add(desktopPane, BorderLayout.CENTER);


    // Row 1
    JLabel lbl1 = new JLabel("Label 1);
    lbl1.setFont(new Font("Arial", Font.BOLD, 14));
    lbl1.setHorizontalAlignment(JLabel.CENTER);
    lbl1.setForeground(Color.white);
    lbl1.setBackground(Color.black);
    lbl1.setOpaque(true);
    lbl1.setBounds(5, 18, 640, 26);// This bit places the field where you want it
    desktopPane.add(lbl1);

    Obviously this is only for a label but the same goes for buttons text areas etc.

    I will proablby be shot at for suggesting this but if you are a control freak like me and dont mind there being lots and lots of code then it works well.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java GUI

    Quote Originally Posted by Paul_White View Post
    I will proablby be shot at for suggesting this but if you are a control freak like me and dont mind there being lots and lots of code then it works well.
    If I were the one who had to maintain your code after you left or were fired, yeah, I'd shoot you. Imagine for instance that you've created a complex GUI with a stack of JRadioButtons somewhere in its midsection. Now in your program's upgrade someone tells you that you need to add one more JRadioButton to the stack and remove a component here and there. You'll have to re-arrange not only the radiobuttons, but all other comopnents in the GUI. Next someone complains that your GUI is unusable on their machine because they are required to have their monitor set at such-and-such resolution, and at this settings, your components run together,... I could go on, but what you are suggesting is a potential nightmare.

    To the original poster, just don't listen to Paul.
    DarrylBurke and Dark like this.

  7. #7
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: Java GUI

    Fubarable, I'm not trying to make it be west and east, just 2 buttons to the east of the center at the bottom, but one in the corner and another one just a little bit to the left of it.

    Though I understand the basics now, Thanks.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Java GUI

    In that case use Fubarables basic structure and instead of Button-Glue-Button do Glue-Button-Button.
    That should force the two buttons to the right (glue pushes things away from it).
    Dark and Fubarable like this.

  9. #9
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •