Results 1 to 6 of 6
  1. #1
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Resize JPanel in JScrollBar

    Hello,

    I am work on creating a survey using swing. I have a working model however there is one problem which I cannot solve. I have included a JPanel in a JScrollPane. The JScroll pain is the main container for my entire survey. The problem I have is that if the JScroll pain is stretched out horizontally, the JPanel inside it will also stretch out, however if I compress the JScroll pain horizontally, the JPanel will not compress; it starts scrolling horizontally.

    I was wonder if there was a way to be able to compress the JScroll Pain slightly so that the JPanel inside it will be compressed to a certain point and then the JScrollPain wil begine scrolling.

    This question was very difficult for me to word; sorry for any confusion.

    Thanks for the help. I have included my code below if you would like to take a look.

    Java Code:
    package src;
    
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import javax.swing.border.EtchedBorder;
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import java.net.URL;
    
    public class myForm {
    
        private static void createGUI(){
    
    //Class row represent a question asked along with 7 answers to choose from (radio buttons)
            class Row extends JPanel{
    
    //possible answers
            public JRadioButton[] buttons = new JRadioButton[7];
    
    //question
            public JLabel question;
    
    //only one answer allowed per group
            ButtonGroup group = new ButtonGroup();
    
            Row(String inQuestion){
    
            //setBorder
            this.setBorder(new EtchedBorder());
            question = new JLabel(inQuestion);
    
            this.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
            c = setConstraints(c,0,0,1,1,0,0,GridBagConstraints.WEST,
                    7,1);
    
            this.add(question,c);
    
            try{
             for(int i=0;i<7;i++){
    
                buttons[i] = new JRadioButton(""+(i+1));
                c = setConstraints(c,i,1,1,2,0,0,GridBagConstraints.CENTER,1,1);
                c.insets = new Insets(20,0,20,0);
                c.fill=GridBagConstraints.HORIZONTAL;
                group.add(buttons[i]);
                //innerPanel.add(buttons[i]);
                this.add(buttons[i],c);
              }
            }catch(Exception e){
                System.out.println(e.toString());
            }
    
            }
    
        }
    
            JPanel[] panels = new Row[16];
            String[] questions = new String[16];
           // JTextArea instructions = new JTextArea();
    
            JPanel bigPanel = new JPanel();
            bigPanel.setLayout(new BoxLayout(bigPanel,BoxLayout.Y_AXIS));
    
            JEditorPane editorPane = new JEditorPane();
            editorPane.setEditable(false);
    
            URL helpURL = myForm.class.getResource("input.txt");
            try{
    
            editorPane.setPage(helpURL);
    
            }catch(Exception e){
            System.out.println(e.toString());
            }
    
            questions[0] = "1.  An expert who doesn't come up with a definite answer probably doesn't know too much.";
            questions[1] = "2.  There is really no such thing as a problem that can't be solved.";
            questions[2] = "3.  I would like to live in a foreign country for a while.";
            questions[3] = "4.  People who fit their lives to a schedule probably miss the joy of living.";
            questions[4] = "5.  A good job is one where what is to be done and how it is to be done is always clear.";
            questions[5] = "6.  In the long run it is possible to get more done by tackling small, simple problems rather than large, complicated ones.";
            questions[6] = "7.  It is more fun to tackle a complicated problem than to solve a simple one.";
            questions[7] = "8.  Often the most interesting and stimulating people are those who don't mind being different and original.";
            questions[8] = "9.  What we are used to is always preferable to what is unfamiliar.";
            questions[9] = "10.  A person who leads an even, regular life in which few surprises or unexpected happenings arise, really has a lot to be grateful for.";
            questions[10] = "11.  People who insists upon a yes or no answer just don't know how complicated things are.";
            questions[11] = "12.  Many of our most important decisions are based on insufficient information.";
            questions[12]= "13.  I like parties where I know most of the people more than ones where most of the people are complete strangers.";
            questions[13] = "14.  The sooner we all acquire ideals, the better.";
            questions[14] = "15.  Teachers or supervisors who hand out vague assignments give a chance for one to show initiative and originality.";
            questions[15] = "16. A good teacher makes you wonder about your way of looking at things.";
    
            JFrame frame = new JFrame("Question Form");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel myPanel = new JPanel();
            GridLayout formLayout = new GridLayout(16,1,0,30);
    
            myPanel.setLayout(formLayout);
            myPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    
            for(int a=0;a<16;a++){
              panels[a] = new Row(questions[a]);
              myPanel.add(panels[a]);
            }
    
            editorPane.setBackground(new Color(238,238,238));
    
            bigPanel.add(editorPane);
            bigPanel.add(myPanel);
    
           JScrollPane scrollPane = new JScrollPane(bigPanel);
    
            frame.getContentPane().add(scrollPane);
    
            frame.setVisible(true);
            frame.setSize(850,910);
    
        }
    
         private static GridBagConstraints setConstraints(GridBagConstraints g, int gridx, int gridy,
                 int weightx, int weighty, int ipadx, int ipady, int anchor, int gridWidth, int gridHeight){
    
             g.gridx=gridx;
             g.gridy = gridy;
             g.weightx = weightx;
             g.weighty=weighty;
             g.ipadx=ipadx;
             g.ipady=ipady;
             g.anchor=anchor;
             g.gridwidth=gridWidth;
             g.gridheight=gridHeight;
    
            return g;
        }
    
        public static void main(String args[]){
            javax.swing.SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    createGUI();
                }
            });
    
        }
    
    }

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

    Default

    I've been playing with your code, and while I don't have a solution, I think I know where the problem is -- the JEditorPane. Look at what happens when you don't add it:
    Java Code:
            //bigPanel.add(editorPane); // [color="red"]***try this and re-run your app ***[/color]
            bigPanel.add(myPanel);

    You'll probably have to await some of the Swing heavy-weights such as Darryl or Rob for an explanation or work around.

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

    Default

    Hm, but when I tried to reproduce it in a somewhat similar program, no dice,...
    Java Code:
    import java.awt.*;
    import java.io.IOException;
    import java.net.URL;
    
    import javax.swing.*;
    
    public class MyForm2 extends JPanel {
        public static final String[] QUESTIONS = {
            "1.  An expert who doesn't come up with a definite answer probably doesn't know too much.",
            "2.  There is really no such thing as a problem that can't be solved.",
            "3.  I would like to live in a foreign country for a while.",
            "4.  People who fit their lives to a schedule probably miss the joy of living.",
            "5.  A good job is one where what is to be done and how it is to be done is always clear.",
            "<html>6.  In the long run it is possible to get more done by tackling small, " +
            "<br>simple problems rather than large, complicated ones.</html>",
            "7.  It is more fun to tackle a complicated problem than to solve a simple one.",
            "8.  Often the most interesting and stimulating people are those who don't mind being different and original.",
            "9.  What we are used to is always preferable to what is unfamiliar.",
            "<html>10.  A person who leads an even, regular life in which few surprises or unexpected happenings arise, " +
            "<br>really has a lot to be grateful for.</html>",
            "11.  People who insists upon a yes or no answer just don't know how complicated things are.",
            "12.  Many of our most important decisions are based on insufficient information.",
            "<html>13.  I like parties where I know most of the people more than ones " +
            "<br>where most of the people are complete strangers.</html>",
            "14.  The sooner we all acquire ideals, the better.",
            "<html>15.  Teachers or supervisors who hand out vague assignments " +
            "<br>give a chance for one to show initiative and originality.</html>",
            "16. A good teacher makes you wonder about your way of looking at things."
        };
        public static final String[] ITEMS = {"1", "2", "3", "4", "5", "6", "7"};
        private static final Dimension VIEW_SIZE = new Dimension(800, 500);
        
        
        public MyForm2() {
            int eb = 20;
            JPanel innerScrollPanel = new JPanel(new GridLayout(0, 1, 0, eb));
            innerScrollPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
            
            for (int row = 0; row < QUESTIONS.length; row++) {
                JPanel radioPanel = new JPanel(new GridLayout(1, 0));
                ButtonGroup btnGroup = new ButtonGroup();
                for (int j = 0; j < ITEMS.length; j++) {
                    JRadioButton rButton = new JRadioButton(ITEMS[j]);
                    radioPanel.add(rButton);
                    btnGroup.add(rButton);
                }
                JPanel rowPanel = new JPanel(new BorderLayout(10, 10));
                rowPanel.add(new JLabel(QUESTIONS[row]), BorderLayout.NORTH);
                rowPanel.add(radioPanel, BorderLayout.CENTER);
                eb = 10;
                rowPanel.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createEtchedBorder(),
                        BorderFactory.createEmptyBorder(eb, eb, eb, eb)));
                innerScrollPanel.add(rowPanel);
            }
            JScrollPane scroll = new JScrollPane(innerScrollPanel);
            scroll.getViewport().setPreferredSize(VIEW_SIZE);
            
            JEditorPane editorPane = new JEditorPane();
            editorPane.setEditable(false);
            editorPane.setBackground(new Color(238, 238, 238));
            URL helpURL = MyFormB.class.getResource("input.txt");
            try {
    
                editorPane.setPage(helpURL);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            setLayout(new BorderLayout());
            add(editorPane, BorderLayout.NORTH);
            add(scroll, BorderLayout.CENTER);
        }
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGui();
                }
            });
        }
    
        private static void createGui() {
            JFrame frame = new JFrame("My Form 2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MyForm2(), BorderLayout.CENTER);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    The 'bigPanel' -- the one contained in the scroll pane (or, more precisely, the JScrollPane's JViewPort) needs to provide a reasonable implementation of Scrollable.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EtchedBorder;
    
    public class ScrollWidthProblem {
    
      private static void createGUI() {
    
        class Row extends JPanel {
    
          public JRadioButton[] buttons = new JRadioButton[7];
          public JLabel question;
          ButtonGroup group = new ButtonGroup();
    
          Row(String inQuestion) {
            this.setBorder(new EtchedBorder());
            question = new JLabel(inQuestion);
    
            this.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
            c = setConstraints(c, 0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, 7, 1);
    
            this.add(question, c);
    
            try {
              for (int i = 0; i < 7; i++) {
    
                buttons[i] = new JRadioButton("" + (i + 1));
                c = setConstraints(c, i, 1, 1, 2, 0, 0, GridBagConstraints.CENTER, 1, 1);
                c.insets = new Insets(20, 0, 20, 0);
                c.fill = GridBagConstraints.HORIZONTAL;
                group.add(buttons[i]);
                //innerPanel.add(buttons[i]);
                this.add(buttons[i], c);
              }
            } catch (Exception e) {
              System.out.println(e.toString());
            }
    
          }
        }
    
        JPanel[] panels = new Row[16];
        String[] questions = new String[16];
    
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
        editorPane.setBackground(new Color(238, 238, 238));
    
        questions[0] = "1.  An expert who doesn't come up with a definite answer probably doesn't know too much.";
        questions[1] = "2.  There is really no such thing as a problem that can't be solved.";
        questions[2] = "3.  I would like to live in a foreign country for a while.";
        questions[3] = "4.  People who fit their lives to a schedule probably miss the joy of living.";
        questions[4] = "5.  A good job is one where what is to be done and how it is to be done is always clear.";
        questions[5] = "6.  In the long run it is possible to get more done by tackling small, simple problems rather than large, complicated ones.";
        questions[6] = "7.  It is more fun to tackle a complicated problem than to solve a simple one.";
        questions[7] = "8.  Often the most interesting and stimulating people are those who don't mind being different and original.";
        questions[8] = "9.  What we are used to is always preferable to what is unfamiliar.";
        questions[9] = "10.  A person who leads an even, regular life in which few surprises or unexpected happenings arise, really has a lot to be grateful for.";
        questions[10] = "11.  People who insists upon a yes or no answer just don't know how complicated things are.";
        questions[11] = "12.  Many of our most important decisions are based on insufficient information.";
        questions[12] = "13.  I like parties where I know most of the people more than ones where most of the people are complete strangers.";
        questions[13] = "14.  The sooner we all acquire ideals, the better.";
        questions[14] = "15.  Teachers or supervisors who hand out vague assignments give a chance for one to show initiative and originality.";
        questions[15] = "16. A good teacher makes you wonder about your way of looking at things.";
    
        JFrame frame = new JFrame("Question Form");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        JPanel myPanel = new JPanel();
        GridLayout formLayout = new GridLayout(16, 1, 0, 30);
    
        myPanel.setLayout(formLayout);
        myPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    
        for (int a = 0; a < 16; a++) {
          panels[a] = new Row(questions[a]);
          myPanel.add(panels[a]);
        }
    
        class BigPanel extends JPanel implements Scrollable {
    
          private int unitHeight;
    
          public BigPanel(int unitHeight) {
            this.unitHeight = unitHeight;
          }
    
          @Override
          public Dimension getPreferredScrollableViewportSize() {
            return null;
          }
    
          @Override
          public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return unitHeight;
          }
    
          @Override
          public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return orientation == SwingConstants.VERTICAL ? visibleRect.height : visibleRect.width;
          }
    
          @Override
          public boolean getScrollableTracksViewportWidth() {
            return true;
          }
    
          @Override
          public boolean getScrollableTracksViewportHeight() {
            return false;
          }
        }
    
        int unitScroll = (int) ((double) myPanel.getPreferredSize().height) / questions.length;
        JPanel bigPanel = new BigPanel(unitScroll); //JPanel();
        bigPanel.setLayout(new BoxLayout(bigPanel, BoxLayout.Y_AXIS));
        bigPanel.add(editorPane);
        bigPanel.add(myPanel);
    
        JScrollPane scrollPane = new JScrollPane(bigPanel);
        frame.getContentPane().add(scrollPane);
    
        frame.setVisible(true);
        frame.setSize(850, 910);
    
      }
    
      private static GridBagConstraints setConstraints(GridBagConstraints g, int gridx, int gridy,
              int weightx, int weighty, int ipadx, int ipady, int anchor, int gridWidth, int gridHeight) {
    
        g.gridx = gridx;
        g.gridy = gridy;
        g.weightx = weightx;
        g.weighty = weighty;
        g.ipadx = ipadx;
        g.ipady = ipady;
        g.anchor = anchor;
        g.gridwidth = gridWidth;
        g.gridheight = gridHeight;
    
        return g;
      }
    
      public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
          public void run() {
            createGUI();
          }
        });
    
      }
    }
    db
    Last edited by DarrylBurke; 10-16-2010 at 07:40 AM. Reason: Updated with improved code (twice)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Posting this as a separate response so it doesn't get lost in the plethora of code:

    The important part is returning true from getScrollableTracksViewpostWidth(). That's what makes the Scrollable, um, track the viewport width at all times.

    db

    edit Made some changes in the code after taking a look at the Scrollable implementation in JTable

    getScrollableUnitIncrement could also be better implemented to scroll up/down by the height of one 'question'

    edit2 Incorporated that too. May not necessarily be what's desired though, and a more rigorous implementation would scroll to the 'boundary' between two questions. I foresee lots of tight coupling in such an implementation (that doesn't mean it shouldn't be done, just that I'm not in the mood to take it up ;) )
    Last edited by DarrylBurke; 10-16-2010 at 07:44 AM.

  6. #6
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Problem with JScrollBar
    By Arthur in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2010, 02:59 AM
  2. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 03:13 AM
  3. JScrollBar
    By solomon_13000 in forum AWT / Swing
    Replies: 1
    Last Post: 07-01-2009, 07:46 AM
  4. Can't resize textarea in JPanel Form
    By wiguy in forum NetBeans
    Replies: 0
    Last Post: 04-02-2009, 10:58 AM
  5. how to resize a shape on a jpanel, using mouse
    By New to JAVA in forum New To Java
    Replies: 1
    Last Post: 12-26-2008, 07:43 PM

Posting Permissions

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