•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 425,937 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,611 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2556 | Replies: 5
![]() |
•
•
Join Date: Apr 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,453
Reputation:
Rep Power: 11
Solved Threads: 296
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
2. listener for each button
I use first option
1. general listener for all
JButton addButton = new JButton ("Add a Book");
JButton searchButton = new JButton ("Search");
JButton borrowingButton = new JButton ("Borrowing");
JButton returnButton = new JButton ("Return a Book");
................................
................................
addButton.addActionListener(this);
searchButton.addActionListener(this);
borrowingButton.addActionListener(this);
returnButton.addActionListener(this);
//then set actionPerformed method
public void actionPerformed(EctionEvent ae)
{
if(ae.getSource() == addButton)
{
//add new book
}
if(ae.getSource() == searchButton)
{
// search for book
}
::::::::::::::::::::::::::::
::::::::::::::::::::::::::::
}2. listener for each button
JButton addButton = new JButton ("Add a Book");
JButton searchButton = new JButton ("Search");
JButton borrowingButton = new JButton ("Borrowing");
JButton returnButton = new JButton ("Return a Book");
................................
................................
addButton.addActionListener(new Actionlistener()
{
public void actionPerformed(ActioneEvent ae)
{
//add new book
}
});
searchButton.addActionListener(new Actionlistener()
{
public void actionPerformed(ActioneEvent ae)
{
//search for book
}
});I use first option
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
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).

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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: May 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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
Thanks Chamsups
PS; My email is chamsups@yahoo.com
•
•
•
•
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
JButton addButton = new JButton ("Add a Book"); JButton searchButton = new JButton ("Search"); JButton borrowingButton = new JButton ("Borrowing"); JButton returnButton = new JButton ("Return a Book"); ................................ ................................ addButton.addActionListener(this); searchButton.addActionListener(this); borrowingButton.addActionListener(this); returnButton.addActionListener(this); //then set actionPerformed method public void actionPerformed(EctionEvent ae) { if(ae.getSource() == addButton) { //add new book } if(ae.getSource() == searchButton) { // search for book } :::::::::::::::::::::::::::: :::::::::::::::::::::::::::: }
2. listener for each button
JButton addButton = new JButton ("Add a Book"); JButton searchButton = new JButton ("Search"); JButton borrowingButton = new JButton ("Borrowing"); JButton returnButton = new JButton ("Return a Book"); ................................ ................................ addButton.addActionListener(new Actionlistener() { public void actionPerformed(ActioneEvent ae) { //add new book } }); searchButton.addActionListener(new Actionlistener() { public void actionPerformed(ActioneEvent ae) { //search for book } });
I use first option
•
•
•
•
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Front-end Java Software Engineer for Digital Video/Media Market Leader (Software Development Job Offers)
- Java Front-end Developer Engineer for Stealth Media Start-up - Hollywood, CA (Web Development Job Offers)
- Java Front-end Developer Engineer for Stealth Media Start-up (Software Development Job Offers)
- Graphics User Interface for Beginners (Python)
- BlueJ problem (Java)
- Linux has come a long way (Getting Started and Choosing a Distro)
- High-Level Languages (Computer Science and Software Design)
Other Threads in the Java Forum
- Previous Thread: Searching an array...where to begin?
- Next Thread: Calendar question



Linear Mode