In regards to JToolBars
Author |
Message |
Pavel
|
Posted: Fri Mar 05, 2010 6:35 pm Post subject: In regards to JToolBars |
|
|
Hey, I'm trying to understand something about the tutorial given by the Sun Java website. In their code, they set the JToolBar object as a parameter pass in addButtons (), why?
Code below:
Quote: import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JToolBar;
public class TextTest extends JPanel implements ActionListener
{
protected JTextArea textArea;
protected String newline = "\n";
static final private String PREVIOUS = "previous";
static final private String DOWN = "down";
static final private String NEXT = "next";
public TextTest ()
{
super (new BorderLayout ());
//Create the toolbar.
JToolBar toolBar = new JToolBar ("Still draggable");
addButtons (toolBar);
//Create the text area used for output. Request
//enough space for 5 rows and 30 columns.
textArea = new JTextArea (5, 30);
textArea.setEditable (false);
JScrollPane scrollPane = new JScrollPane (textArea);
//Lay out the main panel.
setPreferredSize (new Dimension (450, 130));
add (toolBar, BorderLayout.PAGE_START);
add (scrollPane, BorderLayout.CENTER);
}
protected void addButtons (JToolBar toolBar)
{
JButton button = null;
//first button
button = makeNavigationButton ("Back24", PREVIOUS, "Back to previous something-or-other", "Previous");
toolBar.add (button);
//second button
button = makeNavigationButton ("Down24", DOWN, "Down to something-or-other", "Up");
toolBar.add (button);
//third button
button = makeNavigationButton ("Forward24", NEXT, "Forward to something-or-other", "Next");
toolBar.add (button);
}
protected JButton makeNavigationButton (String imageName, String actionCommand, String toolTipText, String altText)
{
String imgLocation = imageName + ".gif";
Image imageGIF = Toolkit.getDefaultToolkit ().getImage (imgLocation);
//Create and initialize the button.
JButton button = new JButton ();
button.setActionCommand (actionCommand);
button.setToolTipText (toolTipText);
button.addActionListener (this);
button.setIcon (new ImageIcon (imageGIF, altText));
return button;
}
protected void displayResult (String actionDescription)
{
textArea.append (actionDescription + newline);
textArea.setCaretPosition (textArea.getDocument ().getLength ());
}
public void actionPerformed (ActionEvent e)
{
String cmd = e.getActionCommand ();
String description = null;
// Handle each button.
if (PREVIOUS.equals (cmd))
{ //first button clicked
description = "taken you to the previous <something>.";
}
else if (DOWN.equals (cmd))
{ // second button clicked
description = "taken you down one level to <something>.";
}
else
{
if (NEXT.equals (cmd))
{ // third button clicked
description = "taken you to the next <something>.";
}
}
displayResult ("If this were a real app, it would have " + description);
}
}
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Barbarrosa

|
Posted: Fri Mar 05, 2010 9:51 pm Post subject: Re: In regards to JToolBars |
|
|
They are using "addButtons" to divide the code into smaller chunks. It takes the JToolbar, then adds buttons to it.
The method needs to have a reference to the JToolbar so it can add buttons.
Java: |
protected void addButtons (JToolBar toolBar )
{
JButton button = null;
//first button
button = makeNavigationButton ("Back24", PREVIOUS, "Back to previous something-or-other", "Previous");
toolBar. add (button );
//second button
button = makeNavigationButton ("Down24", DOWN, "Down to something-or-other", "Up");
toolBar. add (button );
//third button
button = makeNavigationButton ("Forward24", NEXT, "Forward to something-or-other", "Next");
toolBar. add (button );
}
|
|
|
|
|
|
 |
|
|