Ive came across problems while doing a tutorial work piece.

i've to design a toobar ( yup a JToolBar ) with 5 buttons inside it, that will do certain functions for a JSlider.( set them at certain values )

Its simple enough to go and write 5 buttons into my code, but thats not wot ive to do.

The idea is that you will
develop one button that is customisable at design time to have one of 5 different
appearances. Developing 5 separate buttons is NOT an acceptable solution.

Is this even possible? ( pls note the 5 different appearances are Symbals )

Problem: Placing 5 buttons inside a JToolBar, and positing it under my created JSlider

// bob smith 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Testing extends JFrame
  int min = 40;
  int max = 180;
  int value = min;

  JSlider slider = new JSlider(min,max,value);
  MyUI ui = new MyUI(slider);
  int tw,sw,startGap;

  public Testing(){

    slider.addMouseListener(new MouseAdapter(){
      public void mousePressed(MouseEvent me)
    tw = ui.getTrackWidth();
    sw = slider.getWidth();
    startGap = (sw-tw)/2;
  public static void main(String args[]){new Testing();}
class MyUI extends javax.swing.plaf.basic.BasicSliderUI
  public MyUI(JSlider js)
  public int getTrackWidth()
    return (int)((Rectangle)trackRect).getWidth();


Any help or guidance AT all, would be amazing :)


Sure it's possible. You just have to correctly read your assignment.

Putting a JToolBar anywhere except on the edges of a window is however NOT possible.

I looked through some of my old codes, and found something i thought would be useful.

Upon editing it i came up with this

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

public class ToolBarTest {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    JToolBar bar;
    bar = new JToolBar();
    JToggleButton jb;
    for (int i = 0; i < 5; i++) {
      jb = new JToggleButton("" + i);

    contentPane.add(bar, BorderLayout.SOUTH);
    frame.setSize(300, 300);;

So far my attempts to implement it into my code have failed.
Could anyone give me pointers? ( in my first code im using the light layout functions ( UI ) im afraid thats all ive been taught in so far :(

That isn't what you're supposed to do.
Think subclassing the button to create one that has a custom constructor taking an argument telling it what incrememt or decrement it should apply to the slider and another argument passing a reference to the slider.

This article has been dead for over six months. Start a new discussion instead.