Reputation: 4677
I have a problem with the Mig layout. I've started to recreate the main JPanel of a JFrame where I used an absolute layout in the past. Initially everything went well (cfr second image). The console panel (part of a Box with the tab panel) had a good alignment, but still an absolute layout. When I started to convert the layout of the individual JPanels to a Mig layout, it looked like the first image (no left alignment). The same result holds also for other JPanels where i've changed the absolute layout to Mig layout.
https://i.sstatic.net/97Yop.png [BAD] https://i.sstatic.net/KTLGK.png [GOOD]
https://i.sstatic.net/p3qWZ.png [debug 1000 alignments]
Here is a reduced version of my frame class. The structure looks strange because I tried to reduce it as much as possible. I also removed the ControlConsolePanel because my problem even appears with a default JPanel with MigLayout.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
public class MainControll extends JFrame{
private static final long serialVersionUID = 14L;
private JPanel configurationPane;
private JPanel feedbackPane;
private JTextArea feedback;
private JTabbedPane plotTabPane;
private JPanel consolePane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run(){
try {
MainControll frame = new MainControll();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainControll(){
setTitle("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 860, 660);
initiateComponents();
}
private Box rightPanel;
private void initiateComponents() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout("insets 0, debug 1000", "", ""));
this.configurationPane = new JPanel();
this.configurationPane.setBorder(getTitleBorder("Configuration"));
this.configurationPane.setLayout(new MigLayout());
this.plotTabPane = new JTabbedPane();
this.plotTabPane.add("Tab1", new JPanel());
this.consolePane = new JPanel(new MigLayout("","",""));
// --> The MigLayout ruins the frame.
// --> change it to this and look at the difference:
// this.consolePane = new JPanel();
this.consolePane.setBorder(getTitleBorder("Console"));
this.feedback = new JTextArea();
this.feedbackPane = new JPanel();
this.feedbackPane.setBorder(getTitleBorder("Status"));
this.feedbackPane.setLayout(new MigLayout());
JScrollPane sbrText = new JScrollPane(this.feedback);
this.feedbackPane.add(sbrText, "push, grow");
this.rightPanel = new Box(BoxLayout.Y_AXIS);
this.rightPanel.add(this.plotTabPane);
this.rightPanel.add(this.consolePane);
mainPanel.add(this.configurationPane, "shrinky, top, w 450!");
mainPanel.add(this.rightPanel, "spany 5, wrap, grow, pushx, wmin 400");
mainPanel.add(this.feedbackPane, "pushy, growy, w 450!");
JScrollPane contentScrollPane = new JScrollPane(mainPanel);
contentScrollPane.setBorder(BorderFactory.createEmptyBorder());
setContentPane(contentScrollPane);
}
private Border getTitleBorder(String title){
return BorderFactory.createTitledBorder(null, title, TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLUE);
}
}
Target: The point is that I want that the console panel and plot panel fit the right panel without gaps (perfect border alignment), expands and shrinks depending on the grow and shrink behaviour of the right panel.
Edit: I made a recent discover. It works if I place the Mig layout panel in a JTabbedPane. It doesn't work if I place the Mig layout panel in a seperate JPanel. But how and why, I've not a single clue.
Upvotes: 3
Views: 633
Reputation: 10859
I think the problem is the BoxLayout
that you still use. I added some more MigLayout and now it looks like what you want.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
public class MainControl extends JFrame {
private static final long serialVersionUID = 14L;
private JPanel configurationPane;
private JPanel feedbackPane;
private JTextArea feedback;
private JTabbedPane plotTabPane;
private JPanel consolePane;
private JPanel rightPanel;
public MainControl() {
setTitle("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 860, 660);
initiateComponents();
}
private void initiateComponents() {
JPanel mainPanel = new JPanel();
configurationPane = new JPanel();
configurationPane.setBorder(getTitleBorder("Configuration"));
configurationPane.setLayout(new MigLayout());
plotTabPane = new JTabbedPane();
plotTabPane.add("Tab1", new JPanel());
consolePane = new JPanel(new MigLayout("", "", ""));
consolePane.setBorder(getTitleBorder("Console"));
feedback = new JTextArea();
feedbackPane = new JPanel();
feedbackPane.setBorder(getTitleBorder("Status"));
feedbackPane.setLayout(new MigLayout());
JScrollPane sbrText = new JScrollPane(feedback);
feedbackPane.add(sbrText, "push, grow");
rightPanel = new JPanel(new MigLayout("fill"));
rightPanel.add(plotTabPane, "grow, wrap");
rightPanel.add(consolePane, "grow");
mainPanel.setLayout(new MigLayout("insets 0, debug 1000", "", ""));
mainPanel.add(configurationPane, "shrinky, top, w 450!");
mainPanel.add(rightPanel, "spany 5, wrap, grow, pushx, wmin 380");
mainPanel.add(feedbackPane, "pushy, growy, w 450!");
JScrollPane contentScrollPane = new JScrollPane(mainPanel);
contentScrollPane.setBorder(BorderFactory.createEmptyBorder());
setContentPane(contentScrollPane);
}
private static Border getTitleBorder(String title) {
return BorderFactory.createTitledBorder(null, title, TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLUE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
MainControl frame = new MainControl();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Upvotes: 1