0

I have a dictionary that im coding. Its in the barebones right now. This is my first swing project. The class that contains the main method is the following:

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class TestDemo
    {
    public static void main (String[] args)
    {
    SwingUtilities.invokeLater(new Runnable()
       {

        @Override
        public void run()
           {
            // TODO Auto-generated method stub
            JFrame frm = new MainFrame("First Window");
            frm.setVisible(true);
            frm.setSize(500, 500);
            frm.setLocationRelativeTo(null);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }

       });
    }
}

Then i have this class which creates the initial screen you see: import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JRadioButton;
    import javax.swing.JTextArea;


    public class MainFrame extends JFrame{

public MainFrame(String title) {
    // TODO Auto-generated constructor stub
    super(title);

    // Set the Layout Manager
    setLayout(new BorderLayout());

    // Create Swing components
    //Text Fields


    JButton btn = new JButton("Continue");

    JRadioButton addWord = new JRadioButton("Add Word to the Dictionary");

    // Add Swing Components to content pane 
    Container c = getContentPane();
    c.add(btn, BorderLayout.SOUTH);
    c.add(addWord, BorderLayout.CENTER);
    //Add behaviour of Buttons. 
    btn.addActionListener(new ActionListener(){

        // This is when your button is clicked 
        @Override
        public void actionPerformed(ActionEvent e) {
            if(addWord.isSelected()==true){

                new addWordFrame();
            }
        }

    });
}

    }

There is only one button so far, which is to add a word to the dictionary. If its pressed, it opens up a new window. The new window will have one text input field, asking for the word. Once it checks that the word is a valid string, it asks for the definition. If the word is not a valid string, it does not let you enter text into the "definition" field. This is what i have so far:

    import javax.swing.JFrame;


    public class addWordFrame extends JFrame{
    public addWordFrame(){
    super("Adding a word to the dictionary");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

}

How can i make it so when you enter the word, it will not let you enter a definition until you enter a valid string? P.s. i want the word option to be a jtextfield and the definition a jtextarea. I hope this is readable im kind of rushing so its sloppy but i just want to practice.

Armando Xhimanki
  • 115
  • 1
  • 11

1 Answers1

-1

Its the simple example of validating textField content by keyTyped method. That example makes textArea firstly makes the text area disabled. But when the word typed in textField will have length > 2 then text area will enabled. Its one of the simplest method to solve your problem. I have an advice to you don't use to much JFrames make home window as JFrame but to make other windows use JDialog class.

public class AddWordWindow extends JFrame{

public AddWordWindow(){
    getContentPane().setLayout(null);

    JTextPane wordText = new JTextPane();
    wordText.setBounds(10, 21, 82, 20);
    getContentPane().add(wordText);

    JTextArea descriptionText = new JTextArea();
    descriptionText.setBounds(10, 66, 143, 57);
    getContentPane().add(descriptionText);
    descriptionText.setEnabled(false);


    wordText.addKeyListener(new KeyAdapter() {

        @Override
        public void keyTyped(KeyEvent e) {              
            if(wordText.getText().length()>2){
                descriptionText.setEnabled(true);                   
            }
        }           
    });
}

}

Bleedsteel
  • 21
  • 2
  • 5
  • See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Aug 16 '16 at 19:48
  • `getContentPane().setLayout(null);` You should not do this (in code intended for public/newbie viewing) even if the original question *starts out* that way! – Andrew Thompson Aug 16 '16 at 19:49
  • More on that.. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 16 '16 at 19:50