Layout for tags and buttons programmatically

I am trying to create SpringLayout in Java, this is the code I have (got documents from Oracle Java)

import javax.swing.*; import java.awt.*; public class SpringForm { private static void createAndShowGUI() { String[] labels = {"Side1: ", "Side2: ", "Side3: "}; int numPairs = labels.length; //Create and populate the panel. JPanel p = new JPanel(new SpringLayout()); javax.swing.JButton calculate_btn; calculate_btn = new javax.swing.JButton(); for (int i = 0; i < numPairs; i++) { JLabel l = new JLabel(labels[i], JLabel.TRAILING); p.add(l); JTextField textField = new JTextField(10); l.setLabelFor(textField); p.add(textField); } calculate_btn.setText("Calculate"); p.add(calculate_btn); //Lay out the panel. SpringUtilities.makeCompactGrid(p, numPairs, 2, //rows, cols 6, 6, //initX, initY 6, 6); //xPad, yPad //Create and set up the window. JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. p.setOpaque(true); //content panes must be opaque frame.setContentPane(p); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } 

With the code above, I get this

enter image description here

But actually I want to get this

enter image description here

I AM NOT AGAINST ANOTHER OTHER PLAN! just the oracle's help shows that Springlayout is really close to my need, what I'm trying to do is get the layout as shown in the figure below, I'm not sure which layout is used below, and also in my attempt I use SpringLayout I noticed that the elements the controls resize automatically when we increase the size of the window, I want to disable this, but it does not make sense, since SpringLayout clearly means what it does by adjusting the controls when we adjust the window

+7
source share
2 answers

From Java trails SpringLayout (pretty much the first line, actually):

The SpringLayout class was added in JDK version 1.4 to support layout in GUI builders. SpringLayout is a very flexible layout manager that can emulate many of the functions of other layout managers. SpringLayout , however, is very low level and, as such , you should really use it only with a GUI constructor, and not try to code the spring layout manager manually . (Emphasis added.)

I have professionally programmed Java for many years, and even I will not program SpringLayout manually. My recommendation is to use the MigLayout library instead. Their APIs are much simpler to build code and can create very close layouts. I use it for a long time, and I prefer it in everything that I have tried. This is especially nice when used in conjunction with Java BorderLayout because of how space filling works. I highly recommend it.


First of all:

  • MigLayout is cell based, but also supports cell splitting and overlapping. If you've ever worked with HTML or Excel, you should know what that means. This is pretty clear.
  • The default input method of MigLayout is strings, and they are easy to understand, but they also have a very good API for creating layouts.
  • MigLayout supports much more than ever, I will be able to answer the SO question, so follow the link above and read the quick start and trick guide. They are by far the best resource for what you can use in your limitations.

Here is an example using MigLayout to create a similar layout for the example image you provided:

 public static void main(String[] args) { JFrame frame = new JFrame("Testing MigLayout"); JPanel contentPane = new JPanel(new MigLayout("fillx")); // Row 1 JLabel areaLabel = new JLabel("Area of Triangle"); areaLabel.setFont(areaLabel.getFont().deriveFont(16.0f)); areaLabel.setHorizontalAlignment(JLabel.CENTER); contentPane.add(areaLabel, "spanx, growx, wrap"); // wrap indicates a new row // Row 2 JLabel side1Label = new JLabel("Side 1:"); contentPane.add(side1Label, "alignx trailing"); JTextField side1Field = new JTextField(); side1Field.setColumns(6); contentPane.add(side1Field, "alignx leading, wrap"); // Row 3 JLabel side2Label = new JLabel("Side 2:"); contentPane.add(side2Label, "alignx trailing"); JTextField side2Field = new JTextField(); side2Field.setColumns(6); contentPane.add(side2Field, "alignx leading, wrap"); // Row 4 JLabel side3Label = new JLabel("Side 3:"); contentPane.add(side3Label, "alignx trailing"); JTextField side3Field = new JTextField(); side3Field.setColumns(6); contentPane.add(side3Field, "alignx leading, wrap"); // Row 5 JButton calculateButton = new JButton("Calculate Area"); contentPane.add(calculateButton, "spanx, growx"); frame.setContentPane(contentPane); // Resizes automatically frame.pack(); // Centers automatically frame.setLocationRelativeTo(null); // Exit when the frame is closed frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } 

And his conclusion:

MigLayout Example

Of course, this is not all logic, but it still shows the power of MigLayout. When you start working in more complex applications and want the components to expand and contract with the window, MigLayout works very well. If you've ever used GridBadLayout , you'll notice that MigLayout is just a version of it.

For references that all are separate parts of this, just look at the hype sheet. There is an explanation for every part that I used. In particular, everything that is declared in the MigLayout constructor (here, "fillx" ) is a layout constraint, and everything that is declared in add methods (for example, "spanx" and "wrap" ) are component constraints. There you can practice and experiment more to get only the right combination, which creates a great graphical interface.

However, there are always other, simpler layout managers, such as GridLayout or BoxLayout . For simple applications like yours, these layout managers are great. When you start getting into more intensive applications, I recommend logging into MigLayout. For reading on them I recommend Java trails. There is a visual guide to layouts , and you can use this as a transition point. I recommend sticking with them for manual layout:

  • BorderLayout
  • BoxLayout
  • CardLayout
  • FlowLayout
  • GridBagLayout
  • GridLayout
+2
source

I tried using the spring layout, but outside the GUI editor, I found it very confusing. If you can, I recommend you switch to another manager.

  • JLoodies FormLayout is very well known and supported by many GUI developers. The syntax may be hard to catch at first, but once you get it, creating good forms is easy.
  • MigLayout is considered very good.
0
source

All Articles