Why don't you just add a small JLabel
to the front of the JTextField
? The JLabel
could contain the number, and because it isn't editable it will always be there no matter what the user changes in the JTextField
. You could also format the JLabel
to make it red by calling setForeground(Color.RED);
. This might be a much simpler solution?
For example, instead of doing this...
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);
You might do something like this...
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);
JLabel label = new JLabel("1.");
label.setForeground(Color.RED);
panel.add(label,BorderLayout.WEST);
Which adds a red JLabel
to the left of the JTextField
, and because you're using BorderLayout
for the JPanel
then it automatically makes the JLabel
the smallest it can possibly be.