Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java Swing - Adding a background image to the JToolBar object

Java Swing - Adding a background image to the JToolBar object
Thread Tools
Registered User
Join Date: Feb 2003
Status: Offline
Reply With Quote
Feb 4, 2003, 06:53 AM
 
Hello Java Swing Gurus

I am working to create a pure swing Java GUI, that will conform to the Apple Aqua guidelines. Please no negative comments about Swing - Aqua and the Macintosh.

I am trying to add a dividing line just like that found in the System Preferences App. This line separates the Tool Bar icons from the panels below.

As far as I can tell to achieve this Apple used an image that tiled across the background of the toolbar to create the line.

What is the best way to achieve this in Swing, a background image added to JToolBar or maybe just some kind of border or can I just draw a Hrule in Swing.

I am very new to Swing and Java, so take it easy in your explanations. Any help welcome as usual.

This is the code I am using for the toolbar.

<pre>

import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;

import javax.swing.JTextField;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;

import java.awt.*;
import java.awt.event.*;

import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

class ToolBar extends JPanel {

public ToolBar() {
//Create the toolbar.
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
addButtons(toolBar);

//Lay out the content pane.
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(toolBar, BorderLayout.NORTH);
add(contentPane);
}

private void addButtons(JToolBar toolBar) {
JButton button = null;

//first button
button = new JButton( "Quit Application", new ImageIcon("images/delete.png"));
button.setVerticalTextPosition(AbstractButton.BOTT OM); // sets vertical position of text
button.setHorizontalTextPosition(AbstractButton.CE NTER); // sets horizontal position of text

button.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(0,5,0,5), button.getBorder() ));

button.setToolTipText("Quit Application");
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
toolBar.add(button);
}


}

</pre>










Report abuse |



























_
_
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2