JSlider

Hello i have my JSlider workds fine i want have it fill the Track when it slides i tryed
        slrSound = new JSlider(JSlider.VERTICAL,0,100,100);
        slrSound.setMajorTickSpacing(10);
        slrSound.setMinorTickSpacing(2);
        slrSound.setPaintTicks(true);
        slrSound.setPaintLabels(true);
        slrSound.setPaintTrack(true);
        slrSound.setSnapToTicks(true);
        slrSound.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));But its not not filling the track ?

C'on cant be that hard to dooooooo?

Similar Messages

  • JSlider + Display value in a text tool tip box

    Hello,
    I am new to Java. I want to display the value in a text tool tip box of the JSlider as the user moves the knob . Is there any way to achieve this.
    I tried setting up the value of text tool tip in the stateChanged method. But that didn't helped
    Ritesh

    Finally Got it working. Below I am attaching the code for reference
    Thanks for your code. It came very handy
    The only problem is that toolTextTip is appearing on one side rather than below the cursor.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class EventTest
    public static void main(String[] args)
    JFrame f = new JFrame();
    System.out.println("starting application");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new setSlider(),"North");
    JSlider slide = new JSlider();
    slide.setToolTipText("Hello");
    f.getContentPane().add(slide,"South");
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    class setSlider extends JSlider implements ChangeListener, MouseMotionListener{
    public setSlider(){
    setToolTipText("hello " + new Integer(getValue()).toString());
    this.setMaximum(10);
    this.setMinimum(0);
    this.setValue(2);
    this.setMajorTickSpacing(2);
    this.setMinorTickSpacing(1);
    this.setPaintTicks(true);
    this.setPaintLabels(false);
    this.addMouseMotionListener(this);
    this.addChangeListener(this);
    public void stateChanged(ChangeEvent e) {
    setToolTipText(new Integer(getValue()).toString());
    System.out.println(this.getToolTipText());
    KeyStroke controlF1 = KeyStroke.getKeyStroke("control F1");
    ActionListener toolTipAction = getActionForKeyStroke( controlF1 );     
    if (toolTipAction != null)     {          
    ActionEvent postTip = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "");
    toolTipAction.actionPerformed( postTip );
    public void mouseDragged(MouseEvent e){
    System.out.println("here");
    setToolTipText(new Integer(getValue()).toString());
    System.out.println(this.getToolTipText());
    KeyStroke controlF1 = KeyStroke.getKeyStroke("control F1");
    ActionListener toolTipAction = getActionForKeyStroke( controlF1 );     
    if (toolTipAction != null)     {          
    ActionEvent postTip = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "");
    toolTipAction.actionPerformed( postTip );
    public void mouseMoved(MouseEvent e){

  • How to disable (grey-out) the ticks of a JSlider?

    Hi everyone,
    In my GUI, I have a JSlider that I want to be disabled (greyed out)
    because it should be only actived on my application next version.
    (It is important though to have it to preserve GUI appearence).
    Problem is that I was unable to greyout the ticks even though
    already managed to grey out the slider and the number labels.
    Right now, it is pretty uggly to have a partial disabled slider,
    because due to ticks not being disabled.
    Questions:
    1) Does anyone have an idea of how to do it?
    A small part of my code is shown below:
    JSlider dynSl = new JSlider();
    dynSl.setMajorTickSpacing(1);
    dynSl.setValue(0); //Default
    dynSl.setMaximum(7);
    dynSl.setMinimum(0);
    dynSl.setPaintLabels(true);
    dynSl.setPaintTicks(true);
    dynSl.setFont(new java.awt.Font("Dialog", 0, 9));
    dynSl.setSnapToTicks(true);
    dynSl.setEnabled(false);
    // small code just to put the numbers of the slider smaller
    Enumeration enum = dynSl.getLabelTable().elements();
    while (enum.hasMoreElements()) {
    JLabel elem = (JLabel)enum.nextElement();
    elem.setFont(new Font("Dialog",0,8));
    elem.setEnabled(false);
    2) If not possible, how do I change the colour of a JSlider (including
    the Ticks)?
    My idea is to change Slider's colour to the same grey colour one used
    to disable it, making at the end the slider look as if it was disabled.
    Thanks in advance,
    Jorge

    Hi again,
    Thanks for the help.
    I was trying to avoid not having the ticks at all but it seems to be
    the best choise because I could not solve the problem of greying out
    them.
    I did some more troubleshooting though and for my surprise, if I use
    the same code alone, i.e, a simple frame with just the slider it works
    fine. I even thought that it was related with having Windows LnF as
    opposed to Metal (Java) LnF, but that was not the problem.
    The only thing it comes to mind, is that there may be some problem by including the slider on a GridBagLayout !?!?!?
    Anyway,
    What has no simple solution, solved is! :-)
    Jorge

  • Is there any way to animate a JSlider and a JProgressBar?

    Hi all,
    I'm working on an assignment using JSliders and JProgressBars in a Java application. I've been trying to let my JSlider and JProgressBar move by itself i.e. reset the slider's current value to a new one after a specific period of time...Tried using a timer and thread but just can't seem to get it moving. Is there any way to set the JSlider to update itself after a specified time period? Same goes for JProgressBar?
    At my wits' end, really appreciate if anyone can offer me some advice and help...Thanks.

    Hmm...use a timertask that resets the value and triggers a repaint in the event thread? That should work at least in theory.

  • How to place a JSlider in the middle of an image?

    Hi,
    Here is the deal. I have an image inside a JLabel. That JLabel is inside a JPanel, which in turn is encompassed by a JScrollPane. This image represents some curve or a frequency function, lets say for instance a sine curve. What I need to do is have a slider, positioned (just to keep things simple for now) in the middle of that image, not above or below or to the side of an image, but rather directly over it. Thus, blocking a small section of the image from view. So that the user can drag the mouse cursor along the (horizontal) slider and see the different values on that curve. I dont know of any LayoutManager that would allow me to place a JSlider at an arbitrary position on top of a JLabel, besides just using a Null Layout. However, when I use a Null Layout my scroll bars disappear and the JSlider itself just sits there doing nothing, without reacting to any mouse movements, and mroe importantly as soon as the application is resized, the JSlider disappears completely.
    Any comments/code snippets would be greatly appreciated.
    Thanks,
    Val

    /* From: Java Tutorial - How To Use Layered Panes
    *       LayeredPaneDemo.java
    *       http://java.sun.com/docs/books/tutorial/uiswing/
    *                                components/layeredpane.html
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class valy extends JPanel implements ChangeListener {
      private SineWave sineWave = new SineWave();
      JSlider slider;
      JLayeredPane layeredPane;
      public valy() {
        slider = new JSlider(1, 30, 5);
        slider.setBounds(50,125,300,25);
        slider.addChangeListener(this);
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(400,300));
        layeredPane.setBorder(
          BorderFactory.createTitledBorder("Layered Pane App"));
        layeredPane.add(slider, JLayeredPane.PALETTE_LAYER);
        JPanel panel = new JPanel();
        panel.add(sineWave);
        JScrollPane scrollPane = new JScrollPane(panel);
        int width = layeredPane.getPreferredSize().width;
        int height = layeredPane.getPreferredSize().height;
        scrollPane.setBounds(15, 25, width - 30, height - 40);
        layeredPane.add(scrollPane, JLayeredPane.DEFAULT_LAYER);
        add(layeredPane);
      public void stateChanged(ChangeEvent e) {
        sineWave.setCycles(
          ((JSlider)e.getSource()).getValue());
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        JComponent contentPane = new valy();
        contentPane.setOpaque(true);
        frame.setContentPane(contentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(300,200);
        frame.setVisible(true);
    /* From: Thinking in Java by Bruce Eckel
      *       3rd edition, Chapter 16
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class SineWave extends JLabel {
      private static int SCALEFACTOR = 200;
      private int cycles, points;
      private double[] sines;
      private int[] pts;
      public SineWave() {
        setCycles(5);
        setPreferredSize(new Dimension(400,400));
      public void setCycles(int newCycles) {
        cycles = newCycles;
        points = SCALEFACTOR * cycles * 2;
        sines = new double[points];
        for(int j = 0; j < points; j++) {
          double radians = (Math.PI/SCALEFACTOR) * j;
          sines[j] = Math.sin(radians);
        repaint();
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);   
        int maxWidth = getWidth();
        double hstep = (double)maxWidth/(double)points;
        int maxHeight = getHeight();
        pts = new int[points];
        for(int j = 0; j < points; j++)
          pts[j] =
            (int)(sines[j] * maxHeight/2 * .95 + maxHeight/2);
        g2.setPaint(Color.red);
        for(int j = 1; j < points; j++) {
          int x1 = (int)((j - 1) * hstep);
          int x2 = (int)(j * hstep);
          int y1 = pts[j - 1];
          int y2 = pts[j];
          g2.drawLine(x1, y1, x2, y2);
    }

  • JSlider in JTable

    Hi,
    I insert JSlider in a column of a JTable, redefining renderer and editor. The problem is that when I resized the size of the JTable (or the column with the sliders), the sliders are not resized. They are only resized if I move another window over them. Is is a repaint I have to apply ? The problem is that I do not know where to apply it.
    This is my code: I put only the JTable and the renderer, not the editor and the other components.
    This is the JTable (with a main to launch it)
    package GUI.TimeView.Component.essai2;
    import GUI.TimeView.CategoryTimeDimensionView;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    public class TableDialogEditDemo extends JPanel {
        private boolean DEBUG = false;
        JTable table;
        public TableDialogEditDemo() {
            super(new GridLayout(1,0));
            table = new JTable(new MyTableModel());
            table.setPreferredScrollableViewportSize(new Dimension(200, 48));
            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
            table.getColumnModel().getColumn(0).setResizable(false);
            table.getColumnModel().getColumn(0).setPreferredWidth(80);
            table.getColumnModel().getColumn(1).setResizable(false);
            table.getColumnModel().getColumn(1).setPreferredWidth(80);
            table.getColumnModel().getColumn(2).setCellRenderer(new SliderRenderer());
            table.getTableHeader().setReorderingAllowed(false);
            //Add the scroll pane to this panel.
            add(scrollPane);
        class MyTableModel extends AbstractTableModel {
            private String[] columnNames = {"Curve", "Color", "Transparency", "Displayed"};
            private Object[][] data = {
                {"Average", Color.black, 70, new Boolean(false)},
                {"Minimum", Color.black, 70, new Boolean(false)},
                {"Maximum", Color.black, 70, new Boolean(false)}
            public int getColumnCount() {
                return columnNames.length;
            public int getRowCount() {
                return data.length;
            public String getColumnName(int col) {
                return columnNames[col];
            public Object getValueAt(int row, int col) {
                return data[row][col];
             * JTable uses this method to determine the default renderer/
             * editor for each cell.  If we didn't implement this method,
             * then the last column would contain text ("true"/"false"),
             * rather than a check box.
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 1) {
                    return false;
                } else {
                    return true;
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                            + " to " + value
                            + " (an instance of "
                            + value.getClass() + ")");
                data[row][col] = value;
                fireTableCellUpdated(row, col);
                if (DEBUG) {
                    System.out.println("New value of data:");
                    printDebugData();
            private void printDebugData() {
                int numRows = getRowCount();
                int numCols = getColumnCount();
                for (int i=0; i < numRows; i++) {
                    System.out.print("    row " + i + ":");
                    for (int j=0; j < numCols; j++) {
                        System.out.print("  " + data[i][j]);
                    System.out.println();
                System.out.println("--------------------------");
            private Object[] longValues;
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TableDialogEditDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Create and set up the content pane.
            JComponent newContentPane = new TableDialogEditDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    }and the renderer:
    package GUI.TimeView.Component.essai2;
    import java.awt.Color;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    import java.awt.Component;
    import javax.swing.BorderFactory;
    import javax.swing.JSlider;
    import javax.swing.border.Border;
    public class SliderRenderer extends JSlider implements TableCellRenderer {
        Border unselectedBorder = null;
        Border selectedBorder = null;
        public SliderRenderer() {
            setMinimum(0);
            setMaximum(100);
            setBackground(Color.white);
            setBounds(0, 0, 100, 10);
            setOpaque(true);
        public Component getTableCellRendererComponent(JTable table, Object val, boolean isSelected, boolean hasFocus, int row, int column) {
                if (isSelected) {
                    if (selectedBorder == null) {
                        selectedBorder = BorderFactory.createMatteBorder(1,3,1,3, table.getSelectionBackground());
                    setBorder(selectedBorder);
                } else {
                    if (unselectedBorder == null) {
                        unselectedBorder = BorderFactory.createMatteBorder(1,3,1,3, table.getBackground());
                    setBorder(unselectedBorder);
            int value = (Integer) val;
            setValue(value);
            return this;
    }

    Do you need more information?
    Was I clear in my question?
    Thank you

  • How to set the color of ticks in JSlider

    Hi,
    Question about JSlider.
    I am trying to set various colors between the ticks. However, I have gone thro' the javadoc of JSlider but find no such API.
    How could I go this?
    Thanks in advance for any inputs and ideas.
    Wing

    Here is quick demo about customize thumb.
    It is similar for customize other part of JSlider.
    You can use setUI to force use your own UI.
    Create your UI, the better way is extend from existing UI,
    for example: BasicSliderUI or MetalSliderUI (or... depend on L&F you want).
    Check the BasicSliderUI and MetalSliderUI API
    customize the paintXXXX method, that's it.
    Good Luck.
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.plaf.metal.MetalSliderUI;
    public class Test {
      public static void main(String[] args) {
        JFrame f = new JFrame();
        JSlider slider = new JSlider();
        slider.setPaintTicks(false);
        slider.setUI ( new MetalSliderUI() {
          public void paintThumb(Graphics g)  {
              Rectangle knobBounds = thumbRect;
              Color color = g.getColor();
              g.setColor(Color.red);
              g.fillOval(knobBounds.x, knobBounds.y, 15,15);
              g.setColor(color);
        f.getContentPane().add (slider);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

  • JSlider randomly missing tick labels

    Hello everyone,
    I am having a problem with JSliders. I have an application that has several JSliders throughout. They are vertical, min=100 and max=1000, with major ticks every 100, and minor ticks every 25, and the ticks and labels are painted.
    I have noticed that out of say 10 such sliders in my application, perhaps one or two sliders are randomly missing some ticks. For example, the labels will read 100, 200, 300, 400, 500, 600, 700, 900, 1000 (with the 800 just missing). The space for the text is there, but no text.
    As I mentioned, this doesn't always occur. It occurs perhaps on one or two sliders out of 10 every time I start the application. So this would seem to me to be related to a threading issue.
    Here is the constructor that creates the panel that has the JSlider in it:
           try{                                                                                                                                   
                SwingUtilities.invokeAndWait( new Runnable(){ public void run(){                                                                   
                    // INIT GUI & CUSTOM INIT                                                                                                      
                    initComponents();                                                                                                                                                                                                                          
                    depthJSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);                                                              
            catch(Exception e){ Util.handleExceptionNoRestart("Error building Log Table", e); }  
    Here is initComponents() which actually does the work:
        private void initComponents() {
            depthJSlider = new javax.swing.JSlider();                                                                                              
            depthJSlider.setMajorTickSpacing(100);                                                                                                 
            depthJSlider.setMaximum(1000);                                                                                                         
            depthJSlider.setMinimum(100);                                                                                                          
            depthJSlider.setMinorTickSpacing(25);                                                                                                  
            depthJSlider.setOrientation(javax.swing.JSlider.VERTICAL);                                                                             
            depthJSlider.setPaintLabels(true);                                                                                                     
            depthJSlider.setPaintTicks(true);                                                                                                      
            depthJSlider.setSnapToTicks(true);                                                                                                     
            depthJSlider.setToolTipText("<html>\n<b>Event Log Depth Slider</b><br>\nThis slider allows you to specify the maximum number of visible\
    events<br>\nwhen the \"Refresh Log\" or \"Start Auto-refresh\" buttons are pressed.</html>");                                                 
            depthJSlider.setMaximumSize(null);                                                                                                     
            depthJSlider.setMinimumSize(null);                                                                                                     
            depthJSlider.setPreferredSize(null);                                                                                                   
            gridBagConstraints = new java.awt.GridBagConstraints();                                                                                
            gridBagConstraints.gridx = 0;                                                                                                          
            gridBagConstraints.gridy = 0;                                                                                                          
            gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;                                                                        
            gridBagConstraints.weighty = 1.0;                                                                                                      
            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);                                                                          
            tableJPanel.add(depthJSlider, gridBagConstraints);                                                                                     
             }Does anyone have any ideas at all why some of the labels might be missing?
    (Please drop a line to [email protected] if you have any thoughts!)
    Sincerely,
    Ian

    No problems in j2se 1.5.0
    import java.awt.*;
    import javax.swing.*;
    public class SliderTest {
        private JSlider getSlider() {
            JSlider slider = new javax.swing.JSlider();
            slider.setMajorTickSpacing(100);
            slider.setMaximum(1000);
            slider.setMinimum(100);
            slider.setMinorTickSpacing(25);
            slider.setOrientation(javax.swing.JSlider.VERTICAL);
            slider.setPaintLabels(true);
            slider.setPaintTicks(true);
            slider.setSnapToTicks(true);
            slider.setToolTipText("<html><b>Event Log Depth Slider</b>" +
                            "<br>This slider allows you to specify the maximum " +
                            "number of visible events<br>when the \"Refresh Log\" " +
                            "or \"Start Auto-refresh\" buttons are pressed.</html>");
            slider.setMaximumSize(null);
            slider.setMinimumSize(null);
            slider.setPreferredSize(null);
            return slider;
        private JPanel getPanel() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
            gridBagConstraints.weighty = 1.0;
            gridBagConstraints.weightx = 1.0;
            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
            for(int j = 0; j < 10; j++)
                panel.add(getSlider(), gridBagConstraints);
            return panel;
        public static void main(String[] args) {
            SliderTest test = new SliderTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test.getPanel());
            f.setSize(800,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • JTextField and JSlider

    Hi!
    I have a JSlider and a JTextField. I would like them to "update" eachother...
    When the user is changing the JSlider, I need the String in the JTextField automatically to update the recieveing data from the JSlider, and vice versa.
    I dont know how to do this, and Im new to Swing. However, why Im stuck is because I consider the JTextField as an already constructed component. Dont I only when declaring it decide what the showing String should be?
    Many thanks for help

    I need the String in the JTextField automatically to update the recieveing data from the JSlider, and vice versa.ChangeListener for slider and DocumentListener for the textfield's document

  • JSlide problem

    Hi,
    I would like to know wether is possible inside stateChanged JSlider event to know why it is firing.
    I mean, to know wether the user is dragging the thumb or just clicking at the slide. I know about valueIsAdjusting, but both return true.
    If it's not possible here in this event, where could I get it?
    Thanks a lot

    Did you ever figure this out? I am running into the same thing...

  • Problem with JSlider

    I have a JSlider whose minimum value can be -50 and maximum value +50. Why function setValue(int n) doesn't work?
    Advance thanks!
    Best Regards!

    Come on, you've posted here enough to know you should put the exception your message. Without it you're asking, "I'm getting an error, can someone tell me how to fix it?". Need more information. Post the stack trace. Post some code (using the code tag as shown at http://forum.java.sun.com/faq.jsp#format).

  • Style JSlider

    Hi!
    I'm about to style a JSlider. It's not enough to change a few parameters, I mean really style with images.
    What is the best way to do that? I did try to create a subclass of JSlider and in that override the paint method and paint the slider with images. That works, but, not well. For example when I drag, the slider knob gets stuck at some places, which looks really weird. But as soon as I release the mouse button, the slider updates itself and looks well again.
    I also considered creating my own look and feel by extending SliderUI.
    How would you go about this problem?
    Thanks!

    rejeep wrote:
    I also considered creating my own look and feel by extending SliderUI.I would think that's the way to go. Good luck!
    db
    edit Extend BasicSliderUI, not SliderUI.
    Edited by: DarrylBurke

  • JSlider is not working

    Please give me a hint.
    Thaks
    import java.io.*;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.geom.*;
    import java.awt.*;
    import java.awt.BorderLayout;
    import javax.swing.*;
    import java.util.Hashtable;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.applet.Applet;
    public class TestLayout extends JApplet
         private Drawing drawing;
            private JComboBox comb;
            private JComboBox alfa;
            private JSlider dJSlider;
         private Container panel;
            private String af[] = {"Paint","No Paint"};
            private String e[] = {"One","Two"};
            private GridBagLayout layout;
            private GridBagConstraints gbc;
         public void init()
              panel = getContentPane();
                    drawing = new Drawing();
                    panel.add(drawing,BorderLayout.CENTER);
                    panel.add(new getSlider(),BorderLayout.NORTH);
                    panel.add(new getComboPanel(),BorderLayout.EAST);
            class  getComboPanel extends JPanel {
                    public getComboPanel(){
                    comb = new JComboBox(e);
                    layout = new GridBagLayout();
                    setLayout(layout);
                    gbc = new GridBagConstraints();
                    addComponent(this,comb,0,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER);
                    comb.setEditable(false);
                    comb.setForeground (Color.BLUE);
                    comb.setBackground (Color.GREEN);
                    comb.setAlignmentX(Component.LEFT_ALIGNMENT);
                    alfa = new JComboBox(af);
                    gbc.weightx = 100;
                    gbc.weighty = 0;
                    addComponent(this,alfa,1,0,20000,1,GridBagConstraints.NONE, GridBagConstraints.CENTER);
                    alfa.setEditable(false);
                    alfa.setForeground(Color.BLUE);
                    alfa.setBackground(Color.GREEN);
                    alfa.setAlignmentX(Component.LEFT_ALIGNMENT);
                    add(comb);
                    add(alfa);
             private void addComponent(Container container, Component component,
                     int row, int column, int width, int height, int fill, int anchor)
                 gbc.gridx = column;
                 gbc.gridy = row;
                 gbc.gridwidth = width;
                 gbc.gridheight = height;
                 gbc.fill = fill;
                 gbc.anchor = anchor;
                 layout.setConstraints(component,gbc);
                 container.add(component);
              class  getSlider extends JPanel {
                public getSlider(){
                    int min = 1, max = 16, inc = 5;
                    dJSlider = new JSlider(min, max, 6);
                    dJSlider.setMajorTickSpacing(5);
                    dJSlider.setMinorTickSpacing(1);
                    dJSlider.setSnapToTicks(true);
                    dJSlider.setLabelTable(getLabelTable(min, max, inc));
                    dJSlider.setPaintTicks(true);
                    dJSlider.setPaintLabels(true);
            private Hashtable getLabelTable(int min, int max, int inc) {
            Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
            for(int j = min; j <= max; j+=inc) {
                String s = String.format("%.1f", (j+4)/10.0);
                table.put(Integer.valueOf(j), new JLabel(s));
            return table;
    class Drawing extends JPanel
           public void paintComponent(Graphics g)
                super.paintComponent( g ); // call superclass's paint method
                this.setBackground( Color.WHITE );
                Graphics2D g2d = (Graphics2D)g;
                g2d.draw3DRect(3,100,90,55,true);
       // end method paintComponent
           public static void main(String[] args)
                     JFrame appletFrame = new JFrame("My Applet");
                     Applet theApplet = new TestLayout();
                     appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     appletFrame.setSize(800,800);
                     appletFrame.add(theApplet,"Center");
                     theApplet.init();
                     appletFrame.setVisible(true);
    }

    //  <applet code="TestLayoutRx" width="400" height="400"></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.util.Hashtable;
    import javax.swing.*;
    public class TestLayoutRx extends JApplet
        private Drawing drawing;
        private JComboBox comb;
        private JComboBox alfa;
        private JSlider dJSlider;
        private Container panel;
        private String af[] = {"Paint","No Paint"};
        private String e[] = {"One","Two"};
        private GridBagLayout layout;
        private GridBagConstraints gbc;
        public void init()
            panel = getContentPane();
            drawing = new Drawing();
            panel.add(drawing,BorderLayout.CENTER);
            panel.add(new getSlider(),BorderLayout.NORTH);
            panel.add(new getComboPanel(),BorderLayout.EAST);
        class getComboPanel extends JPanel {
            public getComboPanel() {
                comb = new JComboBox(e);
                layout = new GridBagLayout();
                setLayout(layout);
                gbc = new GridBagConstraints();
                addComponent(this,comb,0,0,1,1,GridBagConstraints.NONE,
                             GridBagConstraints.CENTER);
                comb.setEditable(false);
                comb.setForeground (Color.BLUE);
                comb.setBackground (Color.GREEN);
                comb.setAlignmentX(Component.LEFT_ALIGNMENT);
                alfa = new JComboBox(af);
                gbc.weightx = 100;
                gbc.weighty = 0;
                addComponent(this,alfa,1,0,20000,1,GridBagConstraints.NONE,
                             GridBagConstraints.CENTER);
                alfa.setEditable(false);
                alfa.setForeground(Color.BLUE);
                alfa.setBackground(Color.GREEN);
                alfa.setAlignmentX(Component.LEFT_ALIGNMENT);
                add(comb);
                add(alfa);
            private void addComponent(Container container, Component component,
                     int row, int column, int width, int height, int fill, int anchor)
                gbc.gridx = column;
                gbc.gridy = row;
                gbc.gridwidth = width;
                gbc.gridheight = height;
                gbc.fill = fill;
                gbc.anchor = anchor;
                layout.setConstraints(component,gbc);
                container.add(component);
        class getSlider extends JPanel {
            public getSlider() {
                int min = 1, max = 16, inc = 5;
                dJSlider = new JSlider(min, max, 6);
                dJSlider.setMajorTickSpacing(5);
                dJSlider.setMinorTickSpacing(1);
                dJSlider.setSnapToTicks(true);
                dJSlider.setLabelTable(getLabelTable(min, max, inc));
                dJSlider.setPaintTicks(true);
                dJSlider.setPaintLabels(true);
                // Add this component to the panel.
                setLayout(new BorderLayout());
                add(dJSlider);
            private Hashtable getLabelTable(int min, int max, int inc) {
                Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
                for(int j = min; j <= max; j+=inc) {
                    String s = String.format("%.1f", (j+4)/10.0);
                    table.put(Integer.valueOf(j), new JLabel(s));
                return table;
        class Drawing extends JPanel
            public Drawing() {
                // You only need to call/set this once.
                this.setBackground( Color.WHITE );
            public void paintComponent(Graphics g)
                super.paintComponent( g ); // call superclass's paint method
                // This goes in a constructor, not here.
    //            this.setBackground( Color.WHITE );
                // Look how many times this method is called.
                System.out.println("paintComponent");
                Graphics2D g2d = (Graphics2D)g;
                g2d.draw3DRect(3,100,90,55,true);
        public static void main(String[] args)
            JFrame appletFrame = new JFrame("My Applet");
            Applet theApplet = new TestLayoutRx();
            appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            appletFrame.setSize(400,400);
            appletFrame.add(theApplet,"Center");
            theApplet.init();
            appletFrame.setVisible(true);
    }

  • JSlider or similar for controlling position in sound files help please.

    You know how winamp, wm player , etc, and JMStudio all have a "slider" bar to control position, along with a "numeric readout" of the current position.
    Fairly new to Java, but know this isn't a difficult thing to do, just haven't found out how to do it.
    Do not care if it is with JavaSound API or JMF, either is fine, just learning.
    Have read up on Sliders at:
    http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html#labels
    But do not understand how to implement it with Audio file usage.
    My goal as a new programmer is to create a simple audio playing Applet:
    For a GREAT idea on what it will basically do check out:
    JMStudio:
    http://java.sun.com/products/java-media/jmf/2.1.1/jmstudio/jmstudio.html
    My goals are to have:
    Applet
    GUI with: Pause/Play button and a SLIDER to control position, just like described.
    Have it load and play an audio file when a link is clicked on using the PARAM tag from html
    I know about the open source/commercial products,,,, but I'm trying to learn, and those are too in depth to try and learn from right now, trying to take baby steps, and having difficulty finding anything beyond play()
    loop() and stop()
    searched google, groups, yahoo with various phrases.
    Thank you!

    You have to use a JSlider and combine its events with the setMediaTime method of the player.

  • Using JSlider as table component

    Ive been having some problems using JSlider in tables... Ive written several cell editors and renderers but this is the only one that gives me problems. Here is simplified code that i think should work but doesn't:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.JSlider;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.AbstractTableModel;
    import java.awt.FlowLayout;
    import java.awt.Component;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowAdapter;
    * A main frame with a table in it... wish it had sliders...
    public class TestSliderTable extends JFrame
        /** Creates new TestSliderTable */
        public TestSliderTable()
            addWindowListener(new WindowAdapter()
                public void windowClosing()
                    System.exit(0);
            setUpTable();
        private void setUpTable()
            JTable table = new JTable(new SomeDumbTableModel())
                public TableCellRenderer getCellRenderer(int row, int column)
                    return new JSliderTableCellRenderer();
            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout());
            panel.add(table);
            setContentPane(panel);
            pack();
        public static void main(String[] args)
            TestSliderTable frame = new TestSliderTable();
            frame.setVisible(true);
    * A simple cell renderer that should render sliders...
    class JSliderTableCellRenderer extends JSlider
        implements TableCellRenderer
        /** Creates new JSliderTableCellRenderer */
        public JSliderTableCellRenderer()
            super(0, 10, 5);
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                        boolean p3, boolean p4,
                                                        int row, int column)
            int newValue = ((Integer)value).intValue();
            setValue(newValue);
            return this;
    * a simple table model with Integers as values...
    class SomeDumbTableModel extends AbstractTableModel
        public SomeDumbTableModel() { }
        public int getRowCount() { return 10; }
        public int getColumnCount() { return 1; }
        public Object getValueAt(int row, int column) { return new Integer(row); }
    }Anyone have similar problems or know anyway around this?

    Have you tried this:
    http://www.esus.com/javaindex/j2se/jdk1.2/javaxswing/editableatomiccontrols/jtable/jtableslidereditor.html
    Cheers,
    Joris

Maybe you are looking for

  • Sound output levels from headphone jack

    Has anyone else noticed that the output volume levels from the headphone / external jack are quite low? With my old iMac G5 (rev b) I could have the iMac's volume at 2 bars and it was plenty loud. Now I'm at 6 bars to get close to the same volume wit

  • List Display using Factort Method

    Hi All, I am using factory method of the class cl_salv_table to get the output in two dimension list display. Now going further my requirement is that the user may select one or multiple rows and based on that further data is fetched and displayed in

  • Project Name - Internal Number Range

    Dear All, When we create a solman project, manually we give the project name. Want to know whether it is possible to give a internal number range from which the project number should be picked automatically when we create a project. Thanks and Regard

  • Creating PDFs at run-time

    I am developing an AIR app for Windows desktop and iOS. The app needs to be able to create PDFs from information inputted by the user, and then save the PDF to the device. Ideally, I need to do this 100% client-side, and not rely on a server to creat

  • Problem in scripts!!!!!!!!!!! please help...

    Hello Friends, I have an issue in script. Can anyone suggest me what does this mean in a script. &WIPRT-COPY_TEXT& Is this a text symbol ? Actually the value comes form the program and gets printed in the script Output. But if i need to translate the