1

I am writing a simple application, an equation system solver. The basic idea behind it is to get the number of variables in the system from user and create that many JTextFields dynamically followed by a button, to solve the system.

In my endeavor to achive it, I am struggling to set the vertical scroll bar.

What happens is, the layout gets all messed up after the JScrollPane is used.

I've used this as a guide and tried doing it this way, with VERTICAL_ALWAYS AND HORIZONTAL_NEVER, but it doesn't work.

The controls are placed side ways, instead of one-after-the-other from top to bottom.

How to achieve this ?

public class SolverUserInterface implements ActionListener{

    private JFrame solverUI;
    private JPanel panel;
    private JLabel lbl[];
    private JTextField eq_Fields[];

    public SolverUserInterface(int Count) {
        // TODO Auto-generated constructor stub
        solverUI = new JFrame("Solver");
        solverUI.setSize(405, 137);
        solverUI.setResizable(false);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        solverUI.setLocation(dim.width/2-solverUI.getSize().width/2, dim.height/2-solverUI.getSize().height/2);
        solverUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        lbl = new JLabel[Count];
        eq_Fields = new JTextField[Count];
        for(int i = 0; i < Count; i++){
            lbl[i] = new JLabel("Equation "+(i+1)+" : ");
            eq_Fields[i] = new JTextField(26);
            panel.add(lbl[i]);
            panel.add(eq_Fields[i]);
        }
        JButton btnSolve = new JButton("Solve Equations!");
        panel.add(btnSolve);
        btnSolve.addActionListener(this);    
                 /*This is the part concerned*/
        JScrollPane jp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        solverUI.add(jp);    
        solverUI.revalidate();
    }

    public void ShowUserInterface(){
        solverUI.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub          
    }    
}
Community
  • 1
  • 1
Priyabrata
  • 1,202
  • 3
  • 19
  • 57
  • 1
    Usually, with this kind of question, you have better to insert the codes (not all but the specific code that you have a problem with it). See the [SSCCE](http://sscce.org/) and this [question checklist](http://msmvps.com/blogs/jon_skeet/archive/2012/11/24/stack-overflow-question-checklist.aspx). – Azad Jun 27 '13 at 11:39
  • Sorry for the inconvenience. Post edited. – Priyabrata Jun 27 '13 at 11:49
  • Try `panel.setPreferredSize(new Dimension(800, 600));` – Azad Jun 27 '13 at 11:58
  • The values 800 and 600 were not apt, but the method is perfectly is file :) Thank You very much for your help. :) – Priyabrata Jun 27 '13 at 12:06
  • I strongly disagree that the solution offered by @Azad is the right way to go on this. If you are setting the size *or* preferred size of components, it is usually a sign of a poor, fragile GUI. – Andrew Thompson Jun 27 '13 at 13:37
  • Oh, nearly forgot. For better help sooner, post an **[SSCCE](http://sscce.org/)** (which is what I posted, but not what you posted). – Andrew Thompson Jun 27 '13 at 13:55

1 Answers1

4

This is a more sensible and usable GUI based on what you seemed to by attempting. Have a look over it.

Solver User Interface

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

public class SolverUserInterface {

    private JFrame solverUI;
    private JPanel panel;
    private JLabel lbl[];
    private JTextField eq_Fields[];

    public SolverUserInterface(int Count) {
        solverUI = new JFrame("Solver");
        solverUI.setLocationByPlatform(true);
        solverUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        panel = new JPanel(new BorderLayout());
        JPanel labels = new JPanel(new GridLayout(0,1,5,5));
        JPanel fields = new JPanel(new GridLayout(0,1,5,5));
        lbl = new JLabel[Count];
        panel.add(labels, BorderLayout.LINE_START);
        panel.add(fields, BorderLayout.CENTER);
        eq_Fields = new JTextField[Count];
        for(int i = 0; i < Count; i++){
            lbl[i] = new JLabel("Equation "+(i+1)+" : ");
            eq_Fields[i] = new JTextField(26);
            labels.add(lbl[i]);
            fields.add(eq_Fields[i]);
        }
        JButton btnSolve = new JButton("Solve Equations!");
        solverUI.add(btnSolve, BorderLayout.PAGE_END);

        JScrollPane jp = new JScrollPane(
            panel,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        solverUI.add(jp);

        solverUI.pack();
        Dimension d = solverUI.getSize();
        int w = (int)d.getWidth();
        int h = (int)d.getHeight();
        h = (h>200 ? 200 : h);
        Dimension shrinkHeight = new Dimension(w,h);
        solverUI.setSize(shrinkHeight);
    }

    public void showUserInterface(){
        solverUI.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                SolverUserInterface sui = new SolverUserInterface(105);
                sui.showUserInterface();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Alright, and just one clarification about Pack(). When I use it, the size of the window grows with the increase in the number of fields, but thats not the case when I remove the Pack(). It seems using Pack() doesn't make sense while using the scroll bar.. Am I correct with this conclusion ?

A little, but not entirely.

  1. For working reliably, a GUI should be packed or validated.
  2. After a frame is packed, it displays as the minimum size needed to display the components (unless it is taller or wider than the screen size).
  3. ..but then we can take the packed size, use the width it gives us, and set the height shorter.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I learned quite a few things about java layouts from your code.. Thank you very much Andrew :) – Priyabrata Jun 27 '13 at 15:17
  • 1
    It is known as a 'nested layout'. See also the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556) for more ideas about how to combine layouts to create the required GUI. – Andrew Thompson Jun 27 '13 at 15:19
  • Alright, and just one clarification about Pack(). When I use it, the size of the window grows with the increase in the number of fields, but thats not the case when I remove the Pack(). It seems using Pack() doesn't make sense while using the scroll bar.. Am I correct with this conclusion ? – Priyabrata Jun 27 '13 at 15:38
  • See edit to answer (with edited code to demonstrate, as well). – Andrew Thompson Jun 27 '13 at 16:46