Reputation: 135
I am attempting to utilize the MigLayout layout manager to create a GUI that allows the user to move items from one list to the other. I need the arrows to be close together (vertical). The problem I am running into is that the top arrow is at the top of the cell and I have been unsuccessful in trying to move it down to the bottom. I'm trying out the Cell feature of MigLayout but I will use whatever works. Thank you.
public class StackCode extends JPanel{
public StackCode(){
super(new MigLayout());
JButton run = new JButton("Okay");
JButton cancel = new JButton("Cancel");
JList uList = new JList(new String[]{"test1","test2","test3","test4","test5"});
JList nList = new JList();
uList.setBorder(new LineBorder(Color.black));
nList.setBorder(new LineBorder(Color.black));
uList.setPreferredSize(new Dimension(100,150));
nList.setPreferredSize(new Dimension(100,150));
add(run,"cell 0 0");
add(cancel,"cell 1 0 4 1");
add(new JLabel("List1"),"cell 0 1 2 1"); // List1 title label -- cell column row width height
add(new JLabel("List2"),"cell 4 1 2 1"); // List2 title label
add(uList,"cell 0 2 2 5"); // JList1
add(nList,"cell 4 2 2 5"); // JList 2
add(new JLabel("-->"),"cell 3 3 1 3, align center"); // eventually import arrow image
add(new JLabel("<--"),"cell 3 6 1 1, align center"); // eventually import arrow image
}
private static void createAndShowGUI(){
//Create and set up the window.
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
StackCode newContentPane = new StackCode();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Edited to include image of desired graphics.
Upvotes: 2
Views: 2096
Reputation: 135
I was able to figure out how to accomplish this by adding this to the super(new MigLayout()) snippet
super(new MigLayout(
"",
"",
"[center][center][b][top]"
));
// This sets the 1st/2nd row to center aligned, 3rd row to bottom aligned and the
// 4th row to top aligned.
And change this:
add(new JLabel("-->"),"cell 3 3 1 3, align center");
add(new JLabel("<--"),"cell 3 6 1 1, align center");
To:
add(new JLabel("-->"),"cell 3 3 1 1, align center");
add(new JLabel("<--"),"cell 3 4 1 1, align center");
Upvotes: 3