Need Help With Java

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
pabster
Posts: 10
Joined: Sat Feb 27, 2016 12:01 am
Occupation: Student

Need Help With Java

Post by pabster »

Hello, I am in 8th grade and would like to learn java, I already know a bit of java but my code is not working. It basically opens a pop-up asking for password, if it is right it will let you open a Jtree (which I would like to make editable but don't know how) and if it is not it will just say that it is wrong. Here is the code:

Code: Select all


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;


public class LoginGateway extends JPanel
                          implements ActionListener {
    private static String OK = "ok";
    private static String HELP = "help";

    private JFrame controllingFrame;
    private JPasswordField passwordField;

    public LoginGateway(JFrame f) {
        controllingFrame = f;

        passwordField = new JPasswordField(10);
        passwordField.setActionCommand(OK);
        passwordField.addActionListener(this);

        JLabel label = new JLabel("Enter Your Password For Pablo's Private Sector Gateway: ");
        label.setLabelFor(passwordField);

        JComponent buttonPane = createButtonPanel();

        JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        textPane.add(label);
        textPane.add(passwordField);

        add(textPane);
        add(buttonPane);
    }

    protected JComponent createButtonPanel() {
        JPanel p = new JPanel(new GridLayout(0,1));
        JButton okButton = new JButton("OK");
        JButton helpButton = new JButton("Help");

        okButton.setActionCommand(OK);
        helpButton.setActionCommand(HELP);
        okButton.addActionListener(this);
        helpButton.addActionListener(this);

        p.add(okButton);
        p.add(helpButton);

        return p;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (OK.equals(cmd)) { 
            char[] input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Success! Welcome Pablo"); 
                javax.swing.JFrame topFrame = new JFrame();
        javax.swing.JPanel topPanel = new JPanel();
        javax.swing.JSplitPane splitPane = new JSplitPane();
        javax.swing.JPanel  leftPanel   = new JPanel();
        javax.swing.JPanel  rightPanel   = new JPanel();
        javax.swing.JScrollPane leftScrollPane = new JScrollPane();
        javax.swing.JScrollPane rightScrollPane = new JScrollPane();
        javax.swing.JTree  leftTree = new JTree();
        
        //layouts
        // with border layout, components take maximum size they could get.
        topFrame.setLayout(new BorderLayout());
        topPanel.setLayout(new BorderLayout());
        leftPanel.setLayout(new BorderLayout());
        rightPanel.setLayout(new BorderLayout());
        
       topPanel.add(splitPane);
        
        splitPane.setRightComponent(rightPanel);
        splitPane.setLeftComponent(leftPanel);
        splitPane.setDividerLocation(200);
        
        rightPanel.add(rightScrollPane);
        leftPanel.add(leftScrollPane);
        
        leftScrollPane.getViewport().add(leftTree);
        
        topFrame.setSize(600, 500);
        topFrame.setContentPane(topPanel);
        topFrame.setVisible(true);
                tree.setEditable(true);

            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Sorrrrry, YOU DUMB AS HELL",
                    "Error Message For Dumbasses",
                    JOptionPane.ERROR_MESSAGE);
            }

            Arrays.fill(input, '0');

            passwordField.selectAll();
            resetFocus();
        } else { 
            JOptionPane.showMessageDialog(controllingFrame,
                "You have failed.");
        }
    }

    private static boolean isPasswordCorrect(char[] input) {
        boolean isCorrect = true;
        char[] correctPassword = { '1', '0', '0', '8', 'p', 'a', 'b', 's' };

        if (input.length != correctPassword.length) {
            isCorrect = false;
        } else {
            isCorrect = Arrays.equals (input, correctPassword);
        }

        Arrays.fill(correctPassword,'0');

        return isCorrect;
    }

    protected void resetFocus() {
        passwordField.requestFocusInWindow();
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Pablo's Secure Sector Gateway");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final PasswordDemo newContentPane = new PasswordDemo(frame);
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.addWindowListener(new WindowAdapter() {
            public void windowActivated(WindowEvent e) {
                newContentPane.resetFocus();
            }
        });

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
		UIManager.put("swing.boldMetal", Boolean.FALSE);
		createAndShowGUI();
            }
        });
    }
}
It always says:
LoginGateway.java:91: error: cannot find symbol
tree.setEditable(true);
^
symbol: variable tree
location: class LoginGateway
./PasswordDemo.java:91: error: cannot find symbol
tree.setEditable(true);
^
symbol: variable tree
location: class PasswordDemo
2 errors

I don't know why it says that if its not even IN my code, I put it in for one try but then I took it out. It is an error when compiling.
I would like to have the code right and make the JTree editable.
Thanks a lot!! ;)
pabster
Posts: 10
Joined: Sat Feb 27, 2016 12:01 am
Occupation: Student

Re: Need Help With Java

Post by pabster »

Don't worry, it's not my actual password ;)
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Need Help With Java

Post by HowardE »

pabster-

I'm afraid you stumped the panel, and I'm sorry that no one answered you sooner. I looked over your java code and it isn't obvious to me, but java isn't a strong language for me. I took your code and pasted it into a java compiler and saw the same error. After a few minutes of flailing at a repair I went to Google.

I found this thread: http://stackoverflow.com/questions/1110 ... orlistener which seems to address a similar problem that someone had. Maybe this will help - and if not, please write back.

Howard
Locked

Return to “Grades 6-8: Math and Computer Science”