Results 1 to 3 of 3
- 05-13-2009, 05:13 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
how to add event handling to my buttons
I'm working on a program here, that is suppose to read a file, have the ability to navigate back and forth through it with the help of buttons and also have editing as a added functionality....
I'm completely new to programming, so i'm having some difficulties trying to figure out my next step here, in particular how to make the buttons work..any help is greately appreciated.
Java Code:import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyText extends JFrame { private Toolkit toolkit; public static int LineCount = 0; //Counts the lines. public static String[] content; public static void ReadFile() throws Exception { BufferedReader TextFileReader = new BufferedReader(new FileReader("./info.txt")); String c; String t; t = "Blank"; while((c = TextFileReader.readLine()) != null) { LineCount++; t += ","+ c; } t = t.replaceAll("Blank,",""); content = new String[LineCount]; content = t.split(","); TextFileReader.close(); for(int i = 0; i < 7; i++) //Keep printing as long as i is smaller than 7, which is what I need for my text file. System.out.println(content[i]); //prints the first part of my list of names. } public MyText() { setTitle("Name and Address"); setSize(300, 400); toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation((size.width - getWidth())/2, (size.height - getHeight())/2); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); JButton beep = new JButton("Next"); beep.setBounds(150, 60, 80, 30); beep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); System.out.println("Next"); } }); JButton close = new JButton("Back"); close.setBounds(50, 60, 80, 30); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); System.out.println("Go back"); } }); panel.add(beep); panel.add(close); } public static void main(String[] args) { MyText buttons = new MyText(); buttons.setVisible(true); } }
- 05-13-2009, 07:59 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
For something as simple as this, use the ActionListener interface. See the Java Tutorial's section on this topic for more information, as it is a very large subject.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-13-2009, 08:33 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What suppose to do the following code segment?
Did you get any error message? If so better to copy-paste here to see.Java Code:JButton close = new JButton("Back"); close.setBounds(50, 60, 80, 30); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); System.out.println("Go back"); } });
Similar Threads
-
Event handling... help?
By kevzspeare in forum New To JavaReplies: 2Last Post: 04-04-2009, 08:46 PM -
Event handling in JSF
By java08 in forum JavaServer Faces (JSF)Replies: 0Last Post: 03-24-2009, 06:42 AM -
rmi and event handling
By darkhorse in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 08:20 AM -
SWT Event Handling
By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:21 PM -
Event Handling
By luisarca in forum Sun Java Wireless ToolkitReplies: 5Last Post: 05-07-2007, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks