CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    12

    Changing Jpanel Sizes

    Hi to all,
    Hope you all will be fine.I have a recorder for record sound, now i want to add slider to the recorder so i know for how much time i record a voice but the problem is after adding Jslider to the panel the panel sizes are disturbed and i don't know how i change the panel size. First i try to see whether something like panel.setsize() method is exist then i try to adjust by using Layouts but i was unable to fix this problem.
    when you'll open the attachment you'll see four panels with four different colors.It is for easiness that for which panel i am talking about.The panel in green i want that green panel increase its height and the panel in red which include two sub panel one in blue contain Jslider and the other in yellow contain Done button shrink their heights accordingly and the buttonPanel which includes three buttons remain as it is.
    Also i want slider shows 0, 1, 2 and 3.I want it also shows 1 and 2 between 0 and 3 on their respective ticks and want to add seconds ticks between 0 and 1 and so on. How can i do this
    Thanks in advance.
    Here is the code
    Code:
     public CapturePlayback1() {
            setLayout(new BorderLayout());
            EmptyBorder eb = new EmptyBorder(5,5,5,5);
            SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED);
          
            JPanel p1 = new JPanel();
            p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
            
            JPanel p2 = new JPanel();
            p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
    
            JPanel buttonsPanel = new JPanel();
            playButton = addButton("Play", buttonsPanel, false);
            recordButton = addButton("Record", buttonsPanel, true);
            pauseButton = addButton("Pause", buttonsPanel, false);
            p2.add(buttonsPanel);
    
            JPanel samplingPanel = new JPanel(new BorderLayout());
            samplingPanel.setBorder(new LineBorder(Color.GREEN, 2, true));
            eb = new EmptyBorder(10, 20, 25, 20);
            samplingPanel.add(samplingGraph = new SamplingGraph());
            p2.add(samplingPanel);
    
            JPanel savePanel = new JPanel();
            savePanel.setBorder(new LineBorder(Color.RED, 2, true));
            savePanel.setLayout(new BoxLayout(savePanel, BoxLayout.Y_AXIS));
    
            JPanel saveTFpanel = new JPanel();
            saveTFpanel.setBorder(new LineBorder(Color.BLUE, 2, true));
            saveTFpanel.setLayout(new BorderLayout());
            progressSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 3, 0);
            progressSlider.setMajorTickSpacing(3);
            progressSlider.setMinorTickSpacing(1);
            progressSlider.setPaintTicks(true);
            progressSlider.setPaintLabels(true);
            saveTFpanel.add(progressSlider);
            savePanel.add(saveTFpanel);
    
            JPanel saveBpanel = new JPanel();
            saveBpanel.setBorder(new LineBorder(Color.yellow, 2, true));
            doneButton = addButton("Done", saveBpanel, false);
            savePanel.add(saveBpanel);
    
            p2.add(savePanel);
    
            p1.add(p2);
            add(p1);
        }
    Attached Images Attached Images

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Changing Jpanel Sizes

    Generally, if you want a layout to respect a particular component's size, you should set the component preferred size (e.g. panel.setPreferredSize(..)).

    Assuming the major ticks are minutes, making the slider display 60 minor ticks between minute labels means you'll have to provide your own labels. The slider only understands a single level of units, so its max value in this case will be 3 x 60 = 180 seconds. If you want major ticks every minute you must set the major tick spacing to 60. To display minute labels every major tick, you need to provide your own Dictionary of labels, set to display every 60 units. So the slider is set up something like this:
    Code:
    final int TOTAL_MINS = 3;
    
    // create slider with seconds units
    JSlider progressSlider = new JSlider(SwingConstants.HORIZONTAL, 0, TOTAL_MINS * 60, 0);
    
    // create label dictionary for minute labels (put one more than the total number of mins so we get the last label)
    Dictionary<Integer, Component> labels = new Hashtable<Integer, Component>(TOTAL_MINS + 1);
    
    for (int i=0; i < TOTAL_MINS + 1; i++) {
        labels.put(i*60, new JLabel(String.valueOf(i)));
    }
    
    progressSlider.setLabelTable(labels);
    progressSlider.setMajorTickSpacing(60);  // every minute
    progressSlider.setMinorTickSpacing(1);   // every 1 second
    If the seconds divisions are too fine, try setting the minor tick spacing to 5 or 10, so you get a minor tick every 5 or 10 seconds.

    One of the principal objects of theoretical research in any department of knowledge is to find the point of view from which the subject appears in its greatest simplicity...
    J. W. Gibbs
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Aug 2009
    Posts
    12

    Re: Changing Jpanel Sizes

    Hi,
    Thank you. It works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured