Reputation: 316
I have a six JTextFields for the user to enter numbers. The numbers can only be entered by clicking on a button with the desired number on it (numbers are from 1 - 49).
I am trying to design it so that if one textfield has a number in it already, the next number entered is placed in the next available textfield and also if all textfields have numbers in then a warning message comes up if the user tries entering a seventh number.
The code below is what I have now after playing around with it for a few hours but it still does not do what I want it to:
boolean clicked = true;
public void button()
{
try
{
if(clicked)
{
btn1.setEnabled(false);
if(" ".equals(txtSubOne.toString()))
{
txtSubOne.setText("1");
}
else if(!" ".equals(txtSubOne.toString()))
{
txtSubTwo.setText("1");
}
else if(txtSubThree == null)
{
txtSubThree.setText("1");
}
else if(txtSubFour == null)
{
txtSubFour.setText("1");
}
else if(txtSubFive == null)
{
txtSubFive.setText("1");
}
}
}
catch(Exception e)
{
printStackTrace();
}
Upvotes: 0
Views: 139
Reputation: 324088
Create a panel to hold the text fields to be displayed on the frame. Create a List to contain the text fields so you know which text field should be updated.
Use a loop to create the JTextField and add it to the panel and the List.
Create an ActionListener toad to every button. The ActionListener will get the first textField in the List and add the text from the button to the text field. Then it will remove the textField from the list so you don't add text to it again.
Create a panel for the buttons. Then create a loop to create the buttons and add the ActionListener to each button and the button to the panel.
Something like:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JPanel
{
private List<JTextField> textFields = new ArrayList<JTextField>();
public SSCCE()
{
setLayout( new BorderLayout() );
JPanel textFieldPanel = new JPanel();
for (int i = 0; i < 7; i++)
{
JTextField tf = new JTextField(2);
tf.setEditable( false );
textFieldPanel.add( tf );
textFields.add( tf );
}
ActionListener al = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (textFields.size() == 0)
{
System.out.println("Text Fields are full");
return;
}
JTextField tf = textFields.remove(0);
JButton button = (JButton)e.getSource();
String text = e.getActionCommand();
tf.setText( text );
}
};
JPanel buttonPanel = new JPanel( new GridLayout(0, 7) );
for (int i = 0; i < 14; i++)
{
JButton button = new JButton("" + i);
button.addActionListener( al );
buttonPanel.add( button );
}
add(textFieldPanel, BorderLayout.PAGE_START);
add(buttonPanel, BorderLayout.CENTER);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
Upvotes: 4