1

I'm currently working on a browser in Java. I want to have a back button on the top left and to its right a JTextfield with the URL. I want the button to always have the same size but the textfield to change it's width to match the JFrame's width. It doesn't work with BorderLayout and I've tried this:

SpringLayout sl = new SpringLayout();
setLayout(sl);
sl.putConstraint(SpringLayout.WEST, back, 5, SpringLayout.WEST, this);
sl.putConstraint(SpringLayout.NORTH, back, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.WEST, addressBar, 5, SpringLayout.EAST, back);
sl.putConstraint(SpringLayout.NORTH, addressBar, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.SOUTH, back, 25, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.SOUTH, addressBar, 25, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.EAST, addressBar, 5, SpringLayout.EAST, this);

add(back);
add(addressBar);

where "back" is a JButton and addressBar a JTextField. The button seems to work but the addressBar just doen't draw at all.

Any suggestions?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shujal
  • 252
  • 2
  • 6

1 Answers1

2

There are many ways to solve this, and one in fact involves BorderLayout by nesting JPanels. Put the button into a BorderLayout.WEST position of a BorderLayout using container, but the JTextField BorderLayout.CENTER in the same container, and then put that container into the main container BorderLayout.CENTER.

GridBagLayout could also solve this, but again, often the best/simplest solution will involve nesting JPanels (for your containers), each with its own layout manager.


Edit
For example:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;

public class BrowserFoo {

   private static void createAndShowGui() {
      JPanel topPanel = new JPanel(new BorderLayout(2, 2));
      topPanel.add(new JButton("Back"), BorderLayout.WEST);
      topPanel.add(new JTextField(20), BorderLayout.CENTER);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(topPanel, BorderLayout.NORTH);
      mainPanel.add(Box.createRigidArea(new Dimension(400, 400)));

      JFrame frame = new JFrame("BrowserFoo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Note that if you re-size this GUI, the textarea and button remain in proper location.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Can I just add components to a container like to a JFrame and then add the Container to the JFrame? (like topBar.add(back, BorderLayout.WEST); topBar.add(addressBar, BorderLayout.CENTER); topBar.setSize(500, 20); topBar.setVisible(true); add(topBar); setSize(500, 300); setVisible(true); Because that doesn't seem to display anything. – Shujal Nov 06 '13 at 20:00
  • @Shujal: I have no idea about the code you've posted in a comment since comments can't format code. Please edit your original question and post new code and new information at the bottom. Also please see my example code above, and try to run it to see what I mean. – Hovercraft Full Of Eels Nov 06 '13 at 20:02
  • Not in a JFrame, but a JPanel. Each one can have its own layout. This is a personal preference, but I've found that GridBagLayout lets me do almost anything I want. I took some time to figure it out, but it would do what you want. – Steve11235 Nov 06 '13 at 20:56