Reputation: 1
I created a GUI using Java Swing and MigLayout that features a button ("Add new competitor") to add boxes containing JTextFields for user input. It initially starts with one such box, and upon running the program, if the text fields are not changed, then adding a new box works perfectly.
However, if anything is typed into the text field, then hitting the "Add new competitor" button will result in a box being added that has none of the proper components and a small unreadable sliver of text at the top.
GUI class:
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class GUI extends JFrame implements ActionListener {
private JPanel inputPanel, inputComponentPanel;
public GUI() {
super("GUI");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(CommonConstants.GUI_SIZE);
pack();
setLocationRelativeTo(null);
setLayout(CommonConstants.getLayout());
addGUIComponents();
}
private void addGUIComponents() {
//banner
JLabel bannerText = new JLabel("GUI Application");
//inputPanel
inputPanel = new JPanel();
inputPanel.setLayout(CommonConstants.getLayout());
//inputPanelContainer
inputComponentPanel = new JPanel();
inputComponentPanel.setLayout(CommonConstants.getLayout());
inputPanel.add(inputComponentPanel, "wrap");
//newCompButton
JButton newCompButton = new JButton(CommonConstants.NEW_COMP_BUTTON);
newCompButton.addActionListener(this);
inputPanel.add(newCompButton, "wrap");
//inputPanel
JScrollPane scrollPane = new JScrollPane(inputPanel);
inputComponentPanel.add(new InputComponent(inputComponentPanel), "wrap");
this.getContentPane().add(bannerText, "wrap");
this.getContentPane().add(scrollPane, "wrap");
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equalsIgnoreCase(CommonConstants.NEW_COMP_BUTTON)) {
InputComponent inputComponent = new InputComponent(inputComponentPanel);
inputComponentPanel.add(inputComponent, "wrap 10");
inputComponent.getPlay1TextField().requestFocus();
repaint();
revalidate();
}
}
}
(where CommonConstants contains some Dimensions and CommonConstants.LAYOUT is a MigLayout)
InputComponent class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class InputComponent extends JPanel implements ActionListener {
private JLabel compLabel, play1Label, play2Label, play3Label, play4Label;
private JTextField compTextField, play1TextField, play2TextField, play3TextField, play4TextField;
private JButton setButton, deleteButton;
private JPanel parentPanel;
public JTextField getPlay1TextField() {
return play1TextField;
}
public InputComponent(JPanel parentPanel) {
this.parentPanel = parentPanel;
setLayout(CommonConstants.getLayout());
setBackground(Color.YELLOW);
compLabel = new JLabel("Competitor");
compTextField = new JTextField(30);
play1Label = new JLabel("Player 1");
play1TextField = new JTextField(25);
play2Label = new JLabel("Player 2");
play2TextField = new JTextField(25);
play3Label = new JLabel("Player 3");
play3TextField = new JTextField(25);
play4Label = new JLabel("Player 4");
play4TextField = new JTextField(25);
setButton = new JButton("Set");
deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
add(compLabel, "cell 0 0");
add(compTextField, "cell 1 0");
add(deleteButton, "cell 2 0");
add(play1Label, "cell 0 1");
add(play1TextField, "cell 1 1");
add(play2Label, "cell 0 2");
add(play2TextField, "cell 1 2");
add(setButton, "cell 2 2");
add(play3Label, "cell 0 3");
add(play3TextField, "cell 1 3");
add(play4Label, "cell 0 4");
add(play4TextField, "cell 1 4");
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase(CommonConstants.DELETE_INPUT)) {
parentPanel.remove(this);
parentPanel.repaint();
parentPanel.revalidate();
}
}
}
Cursor placement seems to have no effect on this issue. Also, if something is typed in a text field, then deleted (via backspace), the issue still exists. Tabbing through the GUI indicates that the components have been created an exist in the GUI, but are not visible.
Additionally, each box has a "Delete" button that removes it from the GUI. If something is typed into a text field, and then the enclosing box is deleted, the issue disappears.
GUI correctly displays the initial box
Box displays improperly after text is entered into a previous JTextField
Upvotes: 0
Views: 51