The following class creates a form (Jpanel) tasked with acquiring multiples Strings from the user and do something with them. It it functionally working, but it bugs me that the height of the JTextField
s (a component that allows for the modification of one line of text) is automatically adjusted and can became extravagantly big.
I have tried the method setBounds()
, but:
- I do not want to calculate the position or width of the
JTextField
, just its height; and - It does not limit the height of the
JTextField
!
Any suggestion, please?
public class MultiplesStrings extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1106317992372206473L;
/** The greater {@link JPanel}. */
private JPanel contentPane;
private JPanel[] interaction;
private JLabel[] text;
private JTextField[] insertText;
/** The {@link JButton} that submits the form information. */
JButton button;
@SuppressWarnings("unused")
private Consumer<MultiplesStrings> instructions;
// =========================================================
// TODO | Constructor
/**
* Create the frame.
*/
public MultiplesStrings(String title, String[] messages,
int x, int y, int width, int height,
Consumer<MultiplesStrings> instructions) {
// ===== MAIN FRAME DEFINITION =====
this.setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(x, y, width, height);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
// contentPane.setBackground(Colours.newColor("DDDDDD"));
// ===== INTERACTION FRAME DEFINITION =====
this.interaction = new JPanel[messages.length];
this.text = new JLabel[messages.length];
this.insertText = new JTextField[messages.length];
for(int i=0 ; i<messages.length ; i++)
{
interaction[i] = new JPanel();
interaction[i].setLayout(new BoxLayout(interaction[i], BoxLayout.LINE_AXIS));
interaction[i].setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
interaction[i].add(Box.createHorizontalGlue());
// ===== TEXT =====
text[i] = new JLabel(messages[i]);
text[i].setAlignmentY(RIGHT_ALIGNMENT);
// text.setBounds(0, imageResolution + margin, Width, buttonHeight);
interaction[i].add(text[i]);
// ===== INSERT TEXT FIELD =====
insertText[i] = new JTextField();
// this.insertTextField.setBounds(Width + margin, imageResolution + margin, moveWidth, buttonHeight);
insertText[i].setColumns(10);
interaction[i].add(insertText[i]);
this.add(interaction[i]);
}
// ===== SUBMIT BUTTON DEFINITION =====
this.button = new JButton("Submit");
// Button behavior
MultiplesStrings support = this;
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) { }
} );
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
instructions.accept(support);
// support.setVisible(false);
}
} );
this.getRootPane().setDefaultButton(button);
this.add(button);
}
// =========================================================
// TODO | Input-output manipulation
/** Acquires all {@link String}s written by the user in the {@link JTextField}s used for {@code interactions}. */
public String[] acquireInputs() {
String[] output = new String[interaction.length];
for(int i=0 ; i<output.length ; i++)
output[i] = insertText[i].getText();
return output;
}
// =========================================================
// TODO | Main
public static final int width = 300;
public static final int height = 500;
private static String[] input;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { public void run() {
try {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double screenWidth = screenSize.getWidth();
double screenHeight = screenSize.getHeight();
// Creates a centered form
MultiplesStrings ms = new MultiplesStrings("Test",
new String[] { "Insert first string: ", "Insert second string: ", "Insert third string: "},
(int) (screenWidth-width)/2, (int) (screenHeight-height)/2, width, height,
(MultiplesStrings obj) ->
{
input = obj.acquireInputs();
for(int i=0 ; i<input.length ; i++)
System.out.println("The " + i + "-th input is: " + input[i]);
}
);
ms.setVisible(true);
} catch (Exception e) { e.printStackTrace(); }
}
});
}
}