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.