Question

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?

Was it helpful?

Solution

To make your frame have fixed size just use setResizable(false)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);// <- add this line

If you want to change content of combobox you can use one of many methods like

  • removeItem(item) will remove specific element
  • removeItemAt(index) will remove element at specified position (counting from 0)
  • addItem(item) will add new item at the end of combobox
  • insertItemAt(item, index) will insert item on specified place, moving current item down
  • you also have removeAllItems() guess what it does :)

You can find many useful informations in documentation of that class

OTHER TIPS

is to disable the ability to resize the window.

frame.setResizable( false );

Since I am unable to find a simple way to accomplish keeping a fields relative upon resize

There are easier layout managers to use than the SpringLayout. This layout is usually only used by an IDE because it is relatively complex. That is why most of us build GUI's by hand so we are not restricted by the IDE.

I am unable to find a method which allows me to set the contents of the box,

To replace the entire contents you need to replace the model.

comboBox.setModel(...);

This is how all Swing components work so understanding the concepts of a model is very important. Your application code should only ever update the model. In the case of a JComboBox, the component has a few helper methods that will update the model for you.

With respect to your JComboBox issue, you can use getItemAt(int index) to retrieve your item (there are additional getters), addItem(E item) to add to your list and finally removeItemAt(int index) to remove an item from your list (there are additional remove methods). Just make sure that you call setEditable(true) on it, since by defualt a JComboBox is not editable.

Additionally, to make your window a fixed size, call setResizable(false); on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top