I am attempting to teach myself how to make a GUI using Java swing and Window Builder Pro, after watching several youtube videos, and reading some tutorials, and asking a previous question located here I have accomplished the following.
public class JetstreamFrame extends JFrame {
private static final long serialVersionUID = 1L;
JTabbedPane tabPane;
private JPanel buttonOnePanel;
private JPanel buttonTwoPanel;
private SpringLayout springLayout;
private SpringLayout sl_buttonTwoPanel;
private SpringLayout sl_buttonOnePanel;
private JButton ButtonTwo;
private JButton ButtonOne;
private JScrollPane displayTextPanel;
private JTextArea textArea;
private ScrollPaneLayout sl_displayTextPanel;
private JComboBox<String> comboBox;
public JetstreamFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
tabPane = new JTabbedPane(JTabbedPane.TOP);
setupTabPane();
buttonOnePanel = new JPanel();
sl_buttonOnePanel = new SpringLayout();
setupButtonOnePanel();
buttonTwoPanel = new JPanel();
sl_buttonTwoPanel = new SpringLayout();
setupButtonTwoPanel();
ButtonOne = new JButton("Click Here!");
setupButtonOne();
setupComboBox();
ButtonTwo = new JButton("Click Here!");
setupButtonTwo();
displayTextPanel = new JScrollPane(textArea);
sl_displayTextPanel = new ScrollPaneLayout();
setupDisplayTextPanel();
textArea = new JTextArea();
displayTextPanel.setViewportView(textArea);
}
private void setupTabPane()
{
springLayout = new SpringLayout();
springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, tabPane, 245, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane());
getContentPane().setLayout(springLayout);
springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, tabPane, -198, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane());
getContentPane().add(tabPane);
}
private void setupButtonOnePanel()
{
tabPane.addTab("Tab One", null, buttonOnePanel, null);
buttonOnePanel.setLayout(sl_buttonOnePanel);
}
private void setupButtonOne()
{
ButtonOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
textArea.append("You pressed button one, ");
textArea.append("ComboField: " + (String)comboBox.getSelectedItem() + "\n");
}
});
sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, ButtonOne, 99, SpringLayout.NORTH, buttonOnePanel);
sl_buttonOnePanel.putConstraint(SpringLayout.WEST, ButtonOne, 187, SpringLayout.WEST, buttonOnePanel);
buttonOnePanel.add(ButtonOne);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void setupComboBox()
{
String[] array = { "1" , "2", "3", "4" };
comboBox = new JComboBox(array);
sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, comboBox, 99, SpringLayout.NORTH, buttonOnePanel);
sl_buttonOnePanel.putConstraint(SpringLayout.WEST, comboBox, 6, SpringLayout.EAST, ButtonOne);
sl_buttonOnePanel.putConstraint(SpringLayout.EAST, comboBox, 167, SpringLayout.EAST, ButtonOne);
buttonOnePanel.add(comboBox);
}
private void setupButtonTwoPanel()
{
tabPane.addTab("Tab Two", null, buttonTwoPanel, null);
buttonTwoPanel.setLayout(sl_buttonTwoPanel);
}
private void setupButtonTwo()
{
ButtonTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
textArea.append("You pressed button two\n");
}
});
sl_buttonTwoPanel.putConstraint(SpringLayout.NORTH, ButtonTwo, 99, SpringLayout.NORTH, buttonTwoPanel);
sl_buttonTwoPanel.putConstraint(SpringLayout.WEST, ButtonTwo, 187, SpringLayout.WEST, buttonTwoPanel);
buttonTwoPanel.add(ButtonTwo);
}
private void setupDisplayTextPanel()
{
springLayout.putConstraint(SpringLayout.NORTH, displayTextPanel, 5, SpringLayout.SOUTH, tabPane);
springLayout.putConstraint(SpringLayout.WEST, displayTextPanel, 5, SpringLayout.WEST, tabPane);
springLayout.putConstraint(SpringLayout.SOUTH, displayTextPanel, 195, SpringLayout.SOUTH, tabPane);
springLayout.putConstraint(SpringLayout.EAST, displayTextPanel, -5, SpringLayout.EAST, tabPane);
getContentPane().add(displayTextPanel);
displayTextPanel.setLayout(sl_displayTextPanel);
}
public void start()
{
this.setSize(500, 500);
this.setVisible(true);
}
}
Since the previous question I have been able to implement proper exiting of the program upon window close, and the implementation of a combobox where a button press will print the value that is selected inside the combobox.
I have a couple of features I would like to implement, but I am unsure on how to move forward.
The first thing I would like to accomplish, and likely the easiest, is to disable the ability to resize the window. Since I am unable to find a simple way to accomplish keeping a fields relative upon resize, eliminating the ability to resize the window is the next best option.
The other option I would like clarification on is editing the contents of a combobox after construction. Ideally, I would like to be able to add and remove entries from the field via button presses. I am unable to find a method which allows me to set the contents of the box, and it appears the only way to accomplish this is create a new combobox every time a change is made. Is there a more efficient way to do this?