473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

JButtons and maybe JTextArea not working

I've hit a brick wall. I have an GUI inventory program that has three buttons and one textarea. I can get the GUI to appear and the input is correct. But my buttons won't work. When I click the Next button, I should see the next inventory item. Previous doesn't work and neither does Exit. And I a have a sinking feeling that there's something wrong with the logic to modify the textarea, but until the buttons function I can't be sure. Can anyone point out where I were wrong?
Expand|Select|Wrap|Line Numbers
  1. setLayout( new FlowLayout() );
  2.  
  3.         JLabel instructions1 = new JLabel( "Press Next or Previous to navigate Inventory");
  4.         add( instructions1 );
  5.  
  6.         JLabel instructions2 = new JLabel( "Press Exit to End" );
  7.         add( instructions2 );
  8.  
  9.         JTextArea textarea = new JTextArea( inventory[0].toString() );
  10.         add( textarea );
  11.  
  12.         JButton previous = new JButton( "PREVIOUS" );
  13.         add( previous );
  14.  
  15.         JButton next = new JButton( "NEXT" );
  16.         add( next );
  17.  
  18.         JButton exit = new JButton( "EXIT" );
  19.         add( exit );
  20.  
  21.         ButtonHandler handler = new ButtonHandler();
  22.         previous.addActionListener( handler );
  23.         next.addActionListener( handler );
  24.         exit.addActionListener( handler );
  25.     }        
  26.  
  27.         private class ButtonHandler implements ActionListener
  28.         {
  29.             final CD inventory[] = new CD[ counter ];
  30.  
  31.             public void actionPerformed( ActionEvent actionEvent )
  32.             {
  33.                 if ( actionEvent.getSource() == previous )
  34.                 {
  35.                     if ( counter > 0 )
  36.                         textarea = new JTextArea( inventory[count--].toString() );
  37.                     else
  38.                         textarea = new JTextArea ( inventory[inventory.length].toString() );
  39.                 }
  40.  
  41.                 if ( actionEvent.getSource() == next )
  42.                 {
  43.                     if ( counter < inventory.length )
  44.                         textarea = new JTextArea( inventory[ count++ ].toString() );
  45.                     else
  46.                         textarea = new JTextArea( inventory[0].toString() );
  47.                 }
  48.  
  49.                 if ( actionEvent.getSource() == exit )
  50.                     System.exit( 0 );
  51.             }
  52.         }
  53.  
The program compiles but doesn't run properly
Oct 31 '11 #1

✓ answered by rotaryfreak

i've never used the ButtonHandler class before so i'm not quite sure how it's supposed to work, but i have gotten events fired off after clicking a button. here's how i did it:

for each button:
Expand|Select|Wrap|Line Numbers
  1. JButton searchButton = new JButton("Search");
  2. searchButton.addActionListener(this);
  3.  
that's pretty much all you need for the button and now for the action listener:

Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent evt) {
  2.     Object source = evt.getSource();
  3.         if (source == searchButton){
  4.             //do some action
  5.         }
  6.         else if (source == someOtherButton) {
  7.             //do something else
  8.         }
  9.         else if (...) {
  10.             //do something else
  11.         }
  12.                ...
  13.  
  14.     }
  15. }
  16.  
it's basically the same as your code except i add an action listener straight to the button instead of going through the button handler class.

here's another way to accomplish your task:
http://www.java2s.com/Code/JavaAPI/j...isteneract.htm

hope this helps

2 2154
i've never used the ButtonHandler class before so i'm not quite sure how it's supposed to work, but i have gotten events fired off after clicking a button. here's how i did it:

for each button:
Expand|Select|Wrap|Line Numbers
  1. JButton searchButton = new JButton("Search");
  2. searchButton.addActionListener(this);
  3.  
that's pretty much all you need for the button and now for the action listener:

Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent evt) {
  2.     Object source = evt.getSource();
  3.         if (source == searchButton){
  4.             //do some action
  5.         }
  6.         else if (source == someOtherButton) {
  7.             //do something else
  8.         }
  9.         else if (...) {
  10.             //do something else
  11.         }
  12.                ...
  13.  
  14.     }
  15. }
  16.  
it's basically the same as your code except i add an action listener straight to the button instead of going through the button handler class.

here's another way to accomplish your task:
http://www.java2s.com/Code/JavaAPI/j...isteneract.htm

hope this helps
Nov 1 '11 #2
Sadly, I only marginally got this program to work. Even after the buttons were fixed, it still was a mess. So I'm trying a simpler program right now and will attempt the inventory program again later. Thanks rotaryfreak for trying to help. :)
Jan 27 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: - ions | last post by:
Hello, i am relatively new to programming, Java being the first language im learning. At the moment im having trouble changing the setEnable value of a button, relative to the state of a JTextArea,...
1
by: Chris Williams | last post by:
I haven't done any Java programming for a good long bit, so this is probably something really silly but anyways, can anyone tell me why my JTextArea is not appearing when I run this? public...
1
by: Colibasi | last post by:
I would like to have a JTextArea with a background white and text should be readonly. The way I tried is something like this: JTextArea text = new JTextArea(); text.setEditable(false);...
3
by: ITAutobot25 | last post by:
Now this is really the last problem (for real now) with this assignment. My sorter is not working. I managed to sort by product name in my previous assignment; however, I can't get it to work on this...
2
by: JinFTW | last post by:
Hey guys I hate to bother you with this, but I'm having a problem with creating an arrayList of JButtons that will end up using an actionlistener. My original program was this: import...
4
by: Badboy112 | last post by:
Hi guys im not sure what i am doinf wrong but the if statement in the action performed is not working any ideas import java.io.*; import javax.swing.*; import java.awt.*; import...
6
by: Noonga | last post by:
I have tried to search, maybe I did not locate the right information since I am new to these forums and new to java, but, I have a quick question... I am working on an inventory program for a...
1
by: chanshaw | last post by:
Ok my problem is that when it loads the JTextArea, it does not show the full component's width and height. I've tried specifying it with setrow, setcolumn, setprefferedsize, all of that. It does...
0
by: Strife1817 | last post by:
Hey guys... Sorry to bother you with this, however, I am stumped. The project that I built - builds clean, and executes, however only one out of three buttons in working. Could someone take a...
0
by: ndedhia1 | last post by:
Hi, I need help updating my jtextarea. I am reading rows from an excel file and after it is done writing to the jtextarea, the excel file is rewritten with the old rows plus new rows and then...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.