Results 1 to 2 of 2
- 06-08-2012, 01:12 AM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
School program GUI output problem
Hello, I am making a program for one of my classes. The program is working as intended but I think that the problem area is the output. The program is designed to accept user input for a sentence then an additional text box to accept a letter or a number that it will count how many occur in the first input of the sentence. However no matter what I do I get an output of this:
Number of javax.swing.JTextField[,826,35,15x20,layout=javax.swing.plaf.basic.BasicT extUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0, border=javax.swing.plaf.BorderUIResource$CompoundB orderUIResource@1d5a0,flags=296,maximumSize=,minim umSize=,preferredSize=,caretColor=sun.swing.PrintC olorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=1,columnWidth=11,command=,horizontalAlign ment=LEADING]'s: 9
Java Code:results.setText("Number of " + searchText + "'s: " + count);
Here is my entire code:
Java Code:import java.awt.BorderLayout; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JFrame; /*********************************************************** Program Name: Index Programmers Name: Nicholas Anderson Program Description: This program checks the occurrences of characters, etc... ************************************************************/ public class indexClass extends JFrame { private JFrame mainFrame; private JButton searchButton; private JLabel mainLabel; private JLabel searchLabel; private JTextArea mainText; private JTextField searchText; private JPanel panel; private JTextField results; public String userMainText, userSearchText; public int current, count; public char key; public indexClass() { mainFrame = new JFrame("Index Search"); searchButton = new JButton("Search the letter"); mainLabel = new JLabel("Enter text to be searched:"); searchLabel = new JLabel("Enter a letter or character:"); searchText = new JTextField(1); mainText = new JTextArea(5, 10); panel = new JPanel(); results = new JTextField(15); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); SearchButtonHandler sbHandler = new SearchButtonHandler(); searchButton.addActionListener(sbHandler); searchButton.setMnemonic('S'); Container c = mainFrame.getContentPane(); c.setLayout(new BorderLayout()); panel.setLayout(new FlowLayout(FlowLayout.CENTER)); panel.add(mainLabel); panel.add(mainText); panel.add(searchLabel); panel.add(searchText); panel.add(searchButton); panel.add(results); c.add(panel); mainFrame.setSize(400, 300); mainFrame.setVisible(true); } class SearchButtonHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { String userMainText, userSearchText; count = 0; char key; userMainText = mainText.getText().toLowerCase(); userSearchText = searchText.getText().toLowerCase(); key = userSearchText.charAt(0); current = userMainText.indexOf(key); while (current != -1) { count++; current = userMainText.indexOf(key, current+1); } results.setText("Number of " + searchText + "'s: " + count); } } public static void main(String[] args) { new indexClass(); } }
-
Re: School program GUI output problem
searchText is working fine. What you're trying to do is print out the toString() method of a JTextField, and as you're finding out, that's not what you really want to do. Instead you should extract the text that the JTextField holds using its getText() method, and then print out that. Most important, please read the JTextField tutorial as this is all well spelled out there.
Similar Threads
-
Date printing program school project
By nanderson05 in forum New To JavaReplies: 2Last Post: 05-21-2012, 04:55 AM -
Payroll program.. School.. Help Please!
By eddyb5 in forum New To JavaReplies: 2Last Post: 06-05-2011, 05:49 PM -
Help with simple area/perimeter program for school
By icarus in forum New To JavaReplies: 3Last Post: 09-26-2010, 04:21 AM -
School project: network program via java any ideas?
By Sernomicus in forum New To JavaReplies: 2Last Post: 12-07-2009, 03:46 PM -
Beginner Needs Help w/ Program for School
By badness in forum New To JavaReplies: 2Last Post: 11-24-2007, 07:51 PM
Bookmarks