user3335429
user3335429

Reputation: 21

Java button doesn't work

I'm trying to debug my program for homework, but I can't even do that because I don't know why my buttons aren't working. Any help is appreciated, thanks! (I know that my findnext is screwy for now, but I didn't know what else to do so I'm just debugging it for now)

public class Window extends JFrame implements ActionListener {
    private JButton findnext;
    private JButton replace;
    private JButton delete;
    private JButton upper;
    private JTextField from,to;
    private JTextArea textArea;
    final static Color found = Color.PINK;
    final Highlighter hilit;
    final Highlighter.HighlightPainter painter;

    public Window() {
        setTitle("Project 8");
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setSize((d.width/4)*3,d.height);
        textArea = new JTextArea ("The apple ate the apple.",8,40);
        textArea.setLineWrap(true);
        Container contentPane = getContentPane();
        addWindowListener(new Close());
        contentPane.add(textArea);
        JPanel panel = new JPanel();
        JButton findnext = new JButton("FindNext");
        panel.add(findnext);
        from = new JTextField(8);
        panel.add(from);
        findnext.addActionListener(this);
        JButton replace = new JButton("Replace");
        panel.add(replace);
        to = new JTextField(8);
        panel.add(to);
        findnext.addActionListener(this);
        JButton delete = new JButton("Delete");
        panel.add(delete);
        findnext.addActionListener(this);
        JButton upper = new JButton("Upper");
        panel.add(upper);
        findnext.addActionListener(this);
        contentPane.add(panel, "South");
        hilit = new DefaultHighlighter();
        painter = new DefaultHighlighter.DefaultHighlightPainter(found);
        textArea.setHighlighter(hilit);
    }

    public void actionPerformed(ActionEvent evt) {
        String f = from.getText();
        String t = to.getText();
        int n = textArea.getText().indexOf(f);
        Object source = evt.getSource();

        if (source == findnext) {
            hilit.removeAllHighlights();
            String text = textArea.getText();
            int index = text.indexOf(f,0);
            if (index>0) {
                try {
                    hilit.addHighlight(index, index+f.length(), DefaultHighlighter.DefaultPainter);
                }
                catch (BadLocationException e) {
                    ;
                }
            }else if (source == replace) {
                if (n>=0 && f.length() > 0) {
                    textArea.replaceRange(to.getText(),n,n+f.length());
                    ;
                }else if (source == delete) {
                    textArea.setText(" ");
                }else if (source == upper) {
                    f.toUpperCase() ;
                }
            }
        }
    }
}

Upvotes: 1

Views: 1856

Answers (2)

user2575725
user2575725

Reputation:

try this: in ur constructor update this lines:

JButton findnext = new JButton("FindNext");
//
JButton replace = new JButton("Replace");
//
JButton delete = new JButton("Delete");
//
JButton upper = new JButton("Upper");

use this one:

findnext = new JButton("FindNext");
//
replace = new JButton("Replace");
//
delete = new JButton("Delete");
//
upper = new JButton("Upper");

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347332

You have a shadowing problem. You declare...

private JButton findnext;
private JButton replace;
private JButton delete;
private JButton upper;

But in your constructor you do...

JButton findnext = new JButton("FindNext");
//...
JButton replace = new JButton("Replace");
//...
JButton delete = new JButton("Delete");
//...
JButton upper = new JButton("Upper");

Which is re-declaring those variables.

This means that when you try and do...

if (source == findnext) {

It's always false

You're also adding the ActionListener (this) to the findnext button four times...I think you mean to be adding it to each of the other buttons

Beware, there is already a class called Window in AWT, which could cause confusion for people. It's also discouraged to extend directly from a top level container like JFrame and instead should start with a JPanel and the add it to an instance of JFrame (or what ever container you like)

Upvotes: 1

Related Questions