Frage

I have a 9x9 panel, which is panel1[][] each panel has a JLabel, so label1[][]

and I add each label to the panel in for loop:

for (int y = 0; y < 9; y++) {
        for (int x = 0; x < 9; x++) {
            label[y][x] = new Grid(x, y);
            panel1[y][x].add(label[y][x]);                
        }
    }

The main goal is to be able to add a addUndoableEditListener() to each of these JLabels. Users will select a number(int) to place in the JLabel, I want them to be able to undo/redo their selection, by clicking the undo/redo button.

I tried:

UndoManager manager = new UndoManager();
label1.addUndoableEditListener(manager);

However I saw that apprently you cannot add "UndoableEditListener" to JLabels. (Right?)

I saw some examples where you could add "UndoableEditListener" to JTextPane, so I though maybe I could create a JTextPane pane [9][9], and add a textpane to each of the JLabels(which are added to the JPanel). So this would solve the problem of UndoableEditListener.

Does this seem logical? I would really appreciate an easier approach to this, all suggestions welcome : )

I'm just having some problem with adding the UndoableEditListener to the components.

(I would prefer to keep the JLabel, since I need to be able to change the background color feature, otherwise is there a better way??)

Thanks.

War es hilfreich?

Lösung

It looks like you don't really want a JLabel. If you want it to be editable (and undoable), why not a JTextField?

A JTextField can have its background color changed as well as a JLabel:

JTextField tf = new JTextField();
tf.setColor(Color.RED);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top