943,621 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9980
  • Java RSS
Apr 21st, 2007
0

Java using BlueJ- GUI (Graphic User Interface)

Expand Post »
Hi Guys,

I was trying to build a library catalogue with GUI (Graphic User Interface) which consists of two text boxes and four buttons.

I have actually created the layout, but I dont know how to add actions to the buttons.

Could anyone help me please.

the Question is:

for the search button which is searching for list of book that contain within their description whatever is in the description text field and/ or is currently on loan to the borrower matching the number in the borrow text field.


What I have done is as follow:

import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LibraryCatalog extends JFrame
{
private ArrayList<Item> items;
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout ();
JPanel centrePanel = new JPanel ();
JTextField titleTextField = new JTextField ();
JTextField IDTextField = new JTextField ();
JPanel southPanel = new JPanel ();
JButton addButton = new JButton ();
JPanel northPanel = new JPanel ();
JLabel northLabel = new JLabel ();
JButton searchButton = new JButton ();
JButton borrowingButton = new JButton ();
JButton returnButton = new JButton ();

//Construct the frame
public LibraryCatalog()
{
items = new ArrayList<Item>();
makeFrame ();
setVisible (true);

}

//Component initialization
private void makeFrame ()
{
contentPane = (JPanel) this.getContentPane ();
contentPane.setLayout (borderLayout1);
this.setSize (new Dimension(420, 160));
this.setTitle ("Library Catalog");
titleTextField.setText ("");
titleTextField.setColumns (20);
IDTextField.setText ("");
IDTextField.setColumns (16);
addButton.setText ("Add a Book");
addButton.addActionListener (new java.awt.event.ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
addButton_actionPerformed (e);
}
});
northLabel.setText ("");
searchButton.setText ("Search");
searchButton.addActionListener (new java.awt.event.ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
searchButton_actionPerformed(e);
}
});

borrowingButton.setText ("Borrowing");
borrowingButton.addActionListener (new java.awt.event.ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
borrowingButton_actionPerformed(e);
}
});

returnButton.setText ("Return a Book");
returnButton.addActionListener (new java.awt.event.ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
returnButton_actionPerformed(e);
}

});
contentPane.add(centrePanel, BorderLayout.CENTER);
centrePanel.add(titleTextField, null);
centrePanel.add(IDTextField, null);
contentPane.add(southPanel, BorderLayout.SOUTH);
southPanel.add(addButton, null);
southPanel.add(searchButton, null);
southPanel.add(borrowingButton, null);
southPanel.add(returnButton, null);
contentPane.add(northPanel, BorderLayout.NORTH);
northPanel.add(northLabel, null);
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}

void addButton_actionPerformed(ActionEvent e)
{
String theTitle= this.titleTextField.getText();
int userID= Integer.parseInt(this.IDTextField.getText());

this.addItem(new Item(theTitle,userID));//create a new object of the class Item
}

void searchButton_actionPerformed(ActionEvent e)
{

}


void borrowingButton_actionPerformed(ActionEvent e)
{


}
void returnButton_actionPerformed(ActionEvent e)
{



}

public void addItem(Item theItem)
{
items.add(theItem);
}

//Main method
public static void main(String[] args)
{
new LibraryCatalog();
}
}

Thanks for your help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mo86uk is offline Offline
1 posts
since Apr 2007
Apr 21st, 2007
0

Re: Java using BlueJ- GUI (Graphic User Interface)

You are complicating your life little. OK in general there are two main ways how to setup actionListener for buttons
1. general listener for all
Java Syntax (Toggle Plain Text)
  1. JButton addButton = new JButton ("Add a Book");
  2. JButton searchButton = new JButton ("Search");
  3. JButton borrowingButton = new JButton ("Borrowing");
  4. JButton returnButton = new JButton ("Return a Book");
  5. ................................
  6. ................................
  7. addButton.addActionListener(this);
  8. searchButton.addActionListener(this);
  9. borrowingButton.addActionListener(this);
  10. returnButton.addActionListener(this);
  11.  
  12. //then set actionPerformed method
  13. public void actionPerformed(EctionEvent ae)
  14. {
  15. if(ae.getSource() == addButton)
  16. {
  17. //add new book
  18. }
  19. if(ae.getSource() == searchButton)
  20. {
  21. // search for book
  22. }
  23. ::::::::::::::::::::::::::::
  24. ::::::::::::::::::::::::::::
  25. }

2. listener for each button
Java Syntax (Toggle Plain Text)
  1. JButton addButton = new JButton ("Add a Book");
  2. JButton searchButton = new JButton ("Search");
  3. JButton borrowingButton = new JButton ("Borrowing");
  4. JButton returnButton = new JButton ("Return a Book");
  5. ................................
  6. ................................
  7. addButton.addActionListener(new Actionlistener()
  8. {
  9. public void actionPerformed(ActioneEvent ae)
  10. {
  11. //add new book
  12. }
  13. });
  14.  
  15. searchButton.addActionListener(new Actionlistener()
  16. {
  17. public void actionPerformed(ActioneEvent ae)
  18. {
  19. //search for book
  20. }
  21. });

I use first option
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,654 posts
since Dec 2004
Apr 22nd, 2007
0

Re: Java using BlueJ- GUI (Graphic User Interface)

which you use depends on how many buttons you have and how reusable you want your code to be
Personally I prefer a distinct method per control and a distinct ActionListener per control.
Makes it a lot easier to share them between controls that do the same thing (say you have a menu option, a context menu entry, a hotkey, and a toolbar option that all do the same thing).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
May 7th, 2008
-1

Re: Java using BlueJ- GUI (Graphic User Interface)

DO yu have the code for the other classes......I have to write the whole code for all classes...So if you could can you please send me the rest of the code
Thanks Chamsups
PS; My email is chamsups@yahoo.com




Click to Expand / Collapse  Quote originally posted by peter_budo ...
You are complicating your life little. OK in general there are two main ways how to setup actionListener for buttons
1. general listener for all
Java Syntax (Toggle Plain Text)
  1. JButton addButton = new JButton ("Add a Book");
  2. JButton searchButton = new JButton ("Search");
  3. JButton borrowingButton = new JButton ("Borrowing");
  4. JButton returnButton = new JButton ("Return a Book");
  5. ................................
  6. ................................
  7. addButton.addActionListener(this);
  8. searchButton.addActionListener(this);
  9. borrowingButton.addActionListener(this);
  10. returnButton.addActionListener(this);
  11.  
  12. //then set actionPerformed method
  13. public void actionPerformed(EctionEvent ae)
  14. {
  15. if(ae.getSource() == addButton)
  16. {
  17. //add new book
  18. }
  19. if(ae.getSource() == searchButton)
  20. {
  21. // search for book
  22. }
  23. ::::::::::::::::::::::::::::
  24. ::::::::::::::::::::::::::::
  25. }

2. listener for each button
Java Syntax (Toggle Plain Text)
  1. JButton addButton = new JButton ("Add a Book");
  2. JButton searchButton = new JButton ("Search");
  3. JButton borrowingButton = new JButton ("Borrowing");
  4. JButton returnButton = new JButton ("Return a Book");
  5. ................................
  6. ................................
  7. addButton.addActionListener(new Actionlistener()
  8. {
  9. public void actionPerformed(ActioneEvent ae)
  10. {
  11. //add new book
  12. }
  13. });
  14.  
  15. searchButton.addActionListener(new Actionlistener()
  16. {
  17. public void actionPerformed(ActioneEvent ae)
  18. {
  19. //search for book
  20. }
  21. });

I use first option
Reputation Points: 9
Solved Threads: 0
Newbie Poster
chamsups is offline Offline
2 posts
since May 2008
May 7th, 2008
0

Re: Java using BlueJ- GUI (Graphic User Interface)

Click to Expand / Collapse  Quote originally posted by chamsups ...
DO yu have the code for the other classes......I have to write the whole code for all classes...So if you could can you please send me the rest of the code
Thanks Chamsups
PS; My email is chamsups@yahoo.com
This is not a "gimme teh codez" forum. Do your own work and post specific questions if you are having problems. I hope you realize how incredibly lame your request is.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
May 8th, 2008
0

Re: Java using BlueJ- GUI (Graphic User Interface)

especially since he revives a zombie thread from a year ago to post his demand for "zuh koduz".
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Searching an array...where to begin?
Next Thread in Java Forum Timeline: Calendar question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC