I am trying to make a simple UI in java swing that will ask user to input text.
Here is my program:
package practice;
import java.awt.*;
import javax.swing.*;
public class gui_demo {
JFrame frame;
JLabel lblEnterText;
static JTextField textField1;
private JTextField textField2;
// static String userText = textField.getText();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gui_demo window = new gui_demo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public gui_demo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblEnterText = new JLabel("ENTER TEXT");
lblEnterText.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
lblEnterText.setHorizontalAlignment(SwingConstants.CENTER);
lblEnterText.setBounds(46, 70, 94, 25);
frame.getContentPane().add(lblEnterText);
textField1 = new JTextField();
textField1.setBounds(214, 63, 181, 41);
frame.getContentPane().add(textField1);
textField1.setColumns(10);
JButton btnNewButton = new JButton("Show Result");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
ParserTest pt = new ParserTest();
try {
pt.parserAction();
textField2.setText(pt.result);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Oops!!Error occured!");// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btnNewButton.setBounds(157, 135, 131, 51);
frame.getContentPane().add(btnNewButton);
JLabel lblFindNounsVerbs = new JLabel("Find nouns, verbs, adjectives from a given text");
lblFindNounsVerbs.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 12));
lblFindNounsVerbs.setBounds(10, 11, 371, 25);
frame.getContentPane().add(lblFindNounsVerbs);
textField2 = new JTextField();
textField2.setBounds(30, 197, 387, 61);
frame.getContentPane().add(textField2);
textField2.setColumns(10);
}
}
I have another class named parserTest that finds nouns/verbs/adjectives within a sentence.Here is the parserTest class:
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
public class ParserTest {
static Set<String> nounPhrases = new HashSet<>();
static Set<String> adjectivePhrases = new HashSet<>();
static Set<String> verbPhrases = new HashSet<>();
String result = new String;
gui_demo gd = new gui_demo();
String line = practice.gui_demo.textField1.getText();
public ParserTest(String line) {
// TODO Auto-generated constructor stub
}
public ParserTest() {
// TODO Auto-generated constructor stub
}
public void getNounPhrases(Parse p) {
if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP") || p.getType().equals("NNPS")) {
nounPhrases.add(p.getCoveredText());
}
if (p.getType().equals("JJ") || p.getType().equals("JJR") || p.getType().equals("JJS")) {
adjectivePhrases.add(p.getCoveredText());
}
if (p.getType().equals("VB") || p.getType().equals("VBP") || p.getType().equals("VBG")|| p.getType().equals("VBD") || p.getType().equals("VBN")) {
verbPhrases.add(p.getCoveredText());
}
for (Parse child : p.getChildren()) {
getNounPhrases(child);
}
}
public void parserAction() throws Exception {
InputStream is = new FileInputStream("en-parser-chunking.bin");
ParserModel model = new ParserModel(is);
Parser parser = ParserFactory.create(model);
Parse topParses[] = ParserTool.parseLine(line, parser, 1);
for (Parse p : topParses){
getNounPhrases(p);
}
}
public static void main(String[] args) throws Exception {
new ParserTest().parserAction();
result = "Nouns :"+ nounPhrases + "\n" + "Verbs:" + verbPhrases +"Adjectives:" + adjectivePhrases;
}
}
Both classes work alright separately, but I can not bind them together.
When I run this program it gives following Exceptions..
exceptions
java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.AbstractStringBuilder.substring(Unknown Source) at java.lang.StringBuilder.substring(Unknown Source) at opennlp.tools.cmdline.parser.ParserTool.parseLine(ParserTool.java:66) at practice.ParserTest.parserAction(ParserTest.java:59) at practice.gui_demo$2.actionPerformed(gui_demo.java:82) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
please help me to solve the problem.
ParserTool
, which you haven't posted. – shmoselnull
layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammerParserTest
is looking for in theString
, it doesn't exists – MadProgrammer