Hi, I have the following code that puts a JPanel inside of a JFrame. The JPanel itself, consists of two other JPanels, which contain JComboBoxes, JLabels and JButtons.
Currently, when you click on "SecondButton", it removes everything and brings out a new panel.
Here's my problem:
From the layout perspective, when I click on secondButton, it moves to the left of the panel (which is because my panel has FlowLayout.LEFT settings.) I need help in figuring out what layout to use so that:
1. Before/After resizing of JFrame all compoents are alligned together to the left.
2. secondButton stays at its location and does not move.
And I have tried using: 1) BoxLayout with X_AXIS orientation and BorderLayout. BoxLayout doesn't work because it expands my components throughout the panel, which is not what I want.
And BorderLayout doesn't work because if I place secondButton in BorderLayout.EAST orientation it will move to the right side of the panel, naturally. I want the button to be right next to all the other components. And when I add it with BorderLayout.CENTER orientation, it will move after clicking.
Your help is very much appreciated.
Here's my SSCCE:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyLayoutTest {
/**
* @param args
*/
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
JPanel firstPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
JPanel secondPanel = new JPanel();
JComboBox firstComboBox = new JComboBox();
JComboBox secondComboBox = new JComboBox();
JComboBox thirdComboBox = new JComboBox();
JButton firstButton = new JButton("FirstButton");
JButton secondButton = new JButton();
JLabel firstLabel = new JLabel("Names:");
JComboBox newComboBox = new JComboBox();
public MyLayoutTest()
{
JFrame frame = new JFrame("MyLayoutTest");
//components for Panel
//firstLabel = new JLabel("Names:");
newComboBox.setPreferredSize(new Dimension(100,20));
newComboBox.getModel().setSelectedItem("Fourth");
firstComboBox.setPreferredSize(new Dimension(100,20));
firstComboBox.getModel().setSelectedItem("First");
secondComboBox.setPreferredSize(new Dimension(100,20));
secondComboBox.getModel().setSelectedItem("Second");
thirdComboBox.setPreferredSize(new Dimension(100,20));
thirdComboBox.getModel().setSelectedItem("Third");
secondButton = getSecondButton();
//Add components to panel
firstPanel.add(firstLabel);
firstPanel.add(new JLabel(" "));
firstPanel.add(firstComboBox);
firstPanel.add(new JLabel(" "));
firstPanel.add(secondComboBox);
firstPanel.add(new JLabel(" "));
firstPanel.add(thirdComboBox);
firstPanel.add(new JLabel(" "));
firstPanel.add(firstButton);
firstPanel.add(new JLabel(" "));
secondPanel.add(secondButton);
mainPanel = new JPanel();
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.getContentPane().add(mainPanel);
frame.setSize(new Dimension(700,70));
frame.setVisible(true);
}
public JButton getSecondButton()
{
secondButton = new JButton("SecondButton");
secondButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(firstLabel.getText().equals("Names:"))
{
firstComboBox.setVisible(false);
secondComboBox.setVisible(false);
thirdComboBox.setVisible(false);
firstButton.setVisible(false);
firstLabel.setText("Search:");
firstPanel.add(newComboBox);
}
else
{
firstLabel.setText("Names:");
firstComboBox.setVisible(true);
secondComboBox.setVisible(true);
thirdComboBox.setVisible(true);
firstButton.setVisible(true);
newComboBox.setVisible(false);
}
}
});
return secondButton;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyLayoutTest test = new MyLayoutTest();
}
}