Using MigLayout

Question for those familiar with MigLayout

Sorry, I could not come up with a more suitable name for the question ...

I am trying to create a layout that will look like this:

+---------+---------+ | btn1 | btn2 | +---------+---------+ | | | btn3 | | | +-------------------+ 

when changing the window size, the btn1 and btn2 components should fill the x axis (half of each), and the btn3 component should fill both the x axis and all available space along the y axis.

how would you achieve this?

here is the code to start:

 public static void main(String[] args) { JFrame window = new JFrame(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cp = window.getContentPane(); cp.setLayout(new MigLayout("")); cp.add(new JButton("btn1"), ""); cp.add(new JButton("btn2"), ""); cp.add(new JButton("btn3"), ""); window.pack(); window.setVisible(true); } 
+7
java swing miglayout
source share
3 answers

This is pretty easy in MigLayout:

 setLayout(new MigLayout("fill")); add(new JButton("button 1"), "w 50%"); add(new JButton("button 2"), "w 50%, wrap"); add(new JButton("button 3"), "grow, push, span"); 

If you read the original pstanton question, I think the necessary layout instructions are very close to how he formulated it. What I like about MigLayout :)

+26
source share

I have never used miglayout, but it should look something like this:

 ... cp.add(new JButton("btn1")); cp.add(new JButton("btn2"), "wrap"); cp.add(new JButton("btn3"), "span"); ... 
+2
source share

So you want something like this:

sample image http://img22.imageshack.us/img22/9479/capturadepantalla201001g.png

Swing Layout Demo itself has in the "Flow Direction" section

Here is the code from this example:

 JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("Layout: flowx, Cell: flowx", createFlowPanel("", "flowx")); tabbedPane.addTab("Layout: flowx, Cell: flowy", createFlowPanel("", "flowy")); tabbedPane.addTab("Layout: flowy, Cell: flowx", createFlowPanel("flowy", "flowx")); tabbedPane.addTab("Layout: flowy, Cell: flowy", createFlowPanel("flowy", "flowy")); public JPanel createFlowPanel(String gridFlow, String cellFlow) { MigLayout lm = new MigLayout("center, wrap 3," + gridFlow, "[110,fill]", "[110,fill]"); JPanel panel = createTabPanel(lm); for (int i = 0; i < 9; i++) { JButton b = createButton("" + (i + 1)); b.setFont(b.getFont().deriveFont(20f)); panel.add(b, cellFlow); } JButton b = createButton("5:2"); b.setFont(b.getFont().deriveFont(20f)); panel.add(b, cellFlow + ",cell 1 1"); return panel; } 
-one
source share

All Articles