Results 1 to 3 of 3
  1. #1
    Basit is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default 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
    Java 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 Thumbnails Attached Thumbnails Changing JPanel sizes-problem.jpg  
    Last edited by Basit; 08-12-2009 at 01:32 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Recommendations:
    1. Please post a small compilable program that we can run and modify and that demonstrates your problem.
    2. Show a picture of your current app (this has probably already been done above).
    3. But also show a picture (if possible) of what you want the app to look like.

    Best of luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Edit, you can best suggest the JPanel sizes to your layout via setPreferredSize as most layouts work best with this.

    One way to have small hash marks between ints in your JSlider is to make your slider actually go from 0 to 30, but change the JSlider's label tabel to translate 10 to a JLabel displaying 1, 20 to a jlabel showing 2, etc...

    for example -- compilable code to try out:
    Java Code:
    import java.awt.*;
    import java.util.Dictionary;
    import java.util.Hashtable;
    
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class CapturePlayback1 extends JPanel {
      
      private static final Dimension SAMPLING_GRAPH_SIZE = new Dimension(300, 300);
      private JButton playButton;
      private JButton recordButton;
      private JButton pauseButton;
      private JButton doneButton;
    
      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());
        samplingPanel.add(new JLabel("SamplingGraph", SwingConstants.CENTER));
        samplingPanel.setPreferredSize(SAMPLING_GRAPH_SIZE);
        p2.add(samplingPanel);
    
        JPanel savePanel = new JPanel();
        savePanel.setBorder(new LineBorder(Color.RED, 2, true));
        savePanel.setLayout(new BoxLayout(savePanel, BoxLayout.Y_AXIS));
    
        Dictionary<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
        labelTable.put(0, new JLabel("0"));
        labelTable.put(10, new JLabel("1"));
        labelTable.put(20, new JLabel("2"));
        labelTable.put(30, new JLabel("3"));
        
        JPanel saveTFpanel = new JPanel();
        saveTFpanel.setBorder(new LineBorder(Color.BLUE, 2, true));
        saveTFpanel.setLayout(new BorderLayout());
        JSlider progressSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 30, 0);
        progressSlider.setLabelTable(labelTable);
        progressSlider.setMajorTickSpacing(10);
        progressSlider.setMinorTickSpacing(2);
        progressSlider.setSnapToTicks(true);
        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);
    }
    
      private JButton addButton(String text, JPanel panel, boolean enabled) {
        JButton button = new JButton(text);
        button.setEnabled(enabled);
        panel.add(button);
        return button;
      }
      
      private static void createAndShowGUI() {
        JFrame frame = new JFrame("FuSwing1 Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new CapturePlayback1());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }
    You'll need to translate your slider values by dividing by 10 before using them though.
    Last edited by Fubarable; 08-12-2009 at 07:51 PM.

Similar Threads

  1. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 08:29 AM
  2. its not changing bgcolor
    By javanoobita in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:29 PM
  3. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 04:58 PM
  4. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-23-2008, 12:19 AM
  5. Changing icon of JOptionPane
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-21-2007, 08:01 AM

Posting Permissions

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