• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Very GUI mess

 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help me understand why two sets of JButtons works here, but the one I'm trying to add doesn't?

The code is part of an application that adds/edits a list of names and some information connected to those names. I'm trying to add a delete function.
The program is set up to display one of four cards. The first card gives you the option of adding, editing, or deleting a student. The second card displays if you select the edit option. It displays a JComboBox that shows the list of names for editing. The submit and cancel buttons work on this card. The third card displays a text box which allows you to add a name to the list. The submit and cancel buttons work here, as well. The fourth card is the one I'm trying to add, and even though I've followed the same logic as for the two cards that work, this fourth card won't even display. When the Edit Student button is selected, I get a series of error messages that seem to indicate that the event can't be traced back to its source.

JPanel card1 = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout( 6 , 1 ) );
newStudent = new JButton( "Add New Student" );
editStudent = new JButton( "Edit existing Student" );
deleteStudent = new JButton( "Delete existing Student" );
finish = new JButton( "Finish" );
htmlLog = new JButton( "Generate HTML Log" );
newStudent.addActionListener( firstActions );
editStudent.addActionListener( firstActions );
deleteStudent.addActionListener( firstActions );
finish.addActionListener( firstActions );
htmlLog.addActionListener( firstActions );
buttonPanel.add( newStudent );
buttonPanel.add( editStudent );
buttonPanel.add( deleteStudent );
buttonPanel.add( htmlLog );
buttonPanel.add( finish );
card1.add( buttonPanel );

card2 = new JPanel();
card3 = new JPanel();
card4 = new JPanel();
card2.setLayout( new FlowLayout( FlowLayout.CENTER , 20 , 10 ) );
card3.setLayout( new FlowLayout( FlowLayout.CENTER , 20 , 10 ) );
card4.setLayout( new FlowLayout( FlowLayout.CENTER , 20 , 10 ) );
card2.add( labelsAndFields1 );
card3.add( labelsAndFields2 );
card4.add( labelsAndFields3 );


buttons = new JPanel();
submit = new JButton( "Submit" );
cancel = new JButton( "Cancel" );
ButtonPanelActions buttonActions = new ButtonPanelActions();
submit.addActionListener( buttonActions );
submit.setActionCommand( "edit" );
cancel.addActionListener( buttonActions );
buttons.add( submit );
buttons.add( cancel );
card2.add( buttons );

buttons = new JPanel();
submit = new JButton( "Submit" );
cancel = new JButton( "Cancel" );
buttonActions = new ButtonPanelActions();
submit.addActionListener( buttonActions );
submit.setActionCommand( "add" );
cancel.addActionListener( buttonActions );
buttons.add( submit );
buttons.add( cancel );
card3.add( buttons );

buttons = new JPanel();
delete = new JButton( "Delete" );
cancel = new JButton( "Cancel" );
buttonActions = new ButtonPanelActions();
delete.addActionListener( buttonActions );
delete.setActionCommand( "delet" );
cancel.addActionListener( buttonActions );
buttons.add( delete );
buttons.add( cancel );
card4.add( buttons );

Obviously there's a lot of code that does other things, but can anyone see anything in here that would render card4 non-functional?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carol Murphy:
this fourth card won't even display. When the Edit Student button is selected, I get a series of error messages that seem to indicate that the event can't be traced back to its source.



That sounds like you didn't add card4 to the card layout.
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, but I did. There is a later bit of code when all 4 cards are added to the contentPane() or the cardManager. (I'm at work, I don't have the code in front of me.) My dilemma is that I have duplicated the code for the working cards, and at one point got the 4th card to display, but the buttons didn't work correctly. When the delete button on the fourth card (if and when I figure out how to change the code back to get it to display again!) is pressed, it doesn't register as the delete button, I get error messages there, too, indicating the source can't be determined. But all of the cards are coded identically, so why do the first 3 work, but not the 4th?
I'm not well-versed in swing or awt, but I figured that duplicating functional code should create a functioning button! Does anything in this code look wrong to anyone who knows what is going on behind the scenes?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reusing a JPanel reference is okay for layouts. But reusing a JButton reference that is being/has been added to a listener can be confusing. If two buttons with the same name are added and one sends an event, how do we know which one was pressed. Encapsulation, using unique action commands and making all reference names unique are some ways to eliminate the possible confusion.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carol Murphy:

I'm not well-versed in swing or awt, but I figured that duplicating functional code should create a functioning button!



What Craig said. Swing code gets huge quick and it's easy to make a typo.
How's about sharing that error stack trace with us?
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I can get the whole thing, but I will try when I get home from work tonight.
I'm starting to think that perhaps the problem lies in another part of the code, where the actions occur.
I think I'm going to set up card4 to duplicate card2 in everything except the name of its submit/delete button and the actionCommand produced by the button in each panel. If it works then, I'll know my problem lies in the delete function I coded.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Click on the command prompt icon (upper left hand corner of the DOS window), click on "Properties", click on the "Layout" tab and change "Screen Buffer Size" to something huge. I use somewhere between 500 and 1000.
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I'm trying it now!
Got it. My 4/dollar sign key isn't working. I'll have to dig out my spare keyboard later, so (dollarsign) means shift/4!

Here's the trace:

Exception occurred during event dispatching:
java.lang.NullPointerException
at LogApp(dollarsign)FirstPanelActions.actionPerformed(LogApp.java:195)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton(dollarsign)ForwardActionEvents.actionPerformed(UnknownSource)
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 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.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The line of code at 195 is this:
labelsAndFields3.add( deletePanel );

The deletePanel is coded in exactly the same way as the editPanel. The editPanel works, the deletePanel doesn't!

This is the FirstPanelActions class:

private class FirstPanelActions implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
Object o = e.getSource();
if( o == finish )
{
processor.writeData();
System.exit( 0 );
}
else if( o == newStudent )
{
cardManager.show( cards , "card3" );
}
else if( o == htmlLog )
{
String s = processor.generateLog();
showHTMLData( s );
}
else if( o == deleteStudent )
{
if( deletePanel != null )
{
labelsAndFields3.remove( deletePanel );
}
deletePanel = new EditCardPanel( processor );
labelsAndFields3.add( deletePanel );
cardManager.next( cards );
}
else
{
if( editPanel != null )
{
labelsAndFields1.remove( editPanel );
}
editPanel = new EditCardPanel( processor );
labelsAndFields1.add( editPanel );
cardManager.next( cards );
}
}
}

Shed any light? I'm totally stumped!
[ August 12, 2005: Message edited by: Carol Murphy ]
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This just keeps getting wierder.
I got rid of the null pointer exception by adding a few code declarations and moving some around, and then I got the Delete Existing Student button to display card2 (the editPanel card). Not what I was shooting for, but hey.
Went back and changed one line of code from:
cardManager.next( cards );
to:
cardManager.show(cards , "card4" ) ;

and voil�! My deletePanel (aka card4) works, even if it still does exactly what the editPanel does. Now all I have to do is code my delete method properly, and all should work out as planned.
My frustration is that I still don't fully understand why this version works when the other didn't. I have to figure this out or it will drive me bats!


Well, the application works now, but I still don't understand exactly what was wrong with the original code. Very frustrating. I know I'm missing some important point here, but I'm stumped as to what it might be!
[ August 12, 2005: Message edited by: Carol Murphy ]
reply
    Bookmark Topic Watch Topic
  • New Topic