Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Swing - Events.

$
0
0
I am trying to implement a Netbeans/Eclipse - like feature. When you are attempting to create a new class or package in those IDEs and you type in:

Example

but Example.java already exists then it let's you know with a warning that such class already exists. I am trying to accomplish that with a Note class that has a name instance variable.

I guess i should be looking at keypress action event or something like that but what is the cleanest way to accomplish it.

Code so far - (not great since it creates the Note instance without knowing whether it should be there)
Main bit of code is inside the inner class - Line 48.
package test;


import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import javax.swing.*;
import model.*;

public class MainView 
{
    private JFrame frame;
    private Container contentPane;
    private JTextField addField;
    private JButton addButton;
    
    private LinkedList<Note> list;
    
    public MainView(LinkedList<Note> list){
        this.list = list;
        initFrame();
    }
    
    private void initFrame(){
        frame = new JFrame("Evernote");
        contentPane = frame.getContentPane();
        
        initComponents();    
        
        JPanel panelOne = new JPanel();
        panelOne.add(addField);
        panelOne.add(addButton);
        
        contentPane.add(panelOne, BorderLayout.NORTH);
        
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    
    private void initComponents(){
        addField = new JTextField(30);
        addButton = new JButton("Add Note"); 
        addButton.addActionListener(new AddNoteHandler());
    }
    
    
    class AddNoteHandler implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event){
            //Option 1
            String userInput = addField.getText();
            Note note = new Note(userInput);  //  <--- NOT great, created but maybe should not be
            for(Note n: list){
                if(n.equals(note)){
                    //return some FALSE result
                }
            }
            
            //Option 2
            for(int i=0; i < list.size(); i++){
                if(list.get(i).getName().equals(userInput)){
                    //return some FALSE again
                }
            }
        }
    }
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>