0

Probelm: I have 2 panels in my main container. The jplTop Panel is what with I am concerned.

I have set its layout as jplTop.setLayout(new GridLayout(2,4));.

So as per the layout setting I must have 4 components on the 1st row and 2 components on the second row. But somehow my Textfield is appearing in the second row. What do I do to make it appear in the first row and also How do I provide spacing between components.

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

public class JProgressBarDemo extends JFrame{

    JButton jbnStart, jbnStop;
    JTextField jtfInput, jtfOutput;
    JProgressBar pBar = null;
    Timer timer = null;

    int sum = 0;
    int counter = 0;

    JProgressBarDemo() {
        Container container = null;
        container = getContentPane();
        container.setLayout(new GridLayout(2,1));
        
        JPanel jplTop = new JPanel();
        jplTop.setLayout(new GridLayout(2,4));

        JLabel jlb1 = new JLabel("Sum of first ", JLabel.LEFT);
        jlb1.setFont(new Font("Dialog", Font.PLAIN, 12));
        jplTop.add(jlb1);		//Added 1st component

        jtfInput = new JTextField("100", 4);
        jplTop.add(jtfInput);	//Added 2nd component

        JLabel jlb2 = new JLabel(" numbers is ", JLabel.LEFT);
        jlb2.setFont(new Font("Dialog", Font.PLAIN, 12));
        jplTop.add(jlb2);		//Added 3rd component

        jtfOutput = new JTextField(10);
        jplTop.add(jtfOutput);	//Added 4th component

        jbnStart = new JButton("Start");
        jbnStart.addActionListener(new ButtonListener());
        jplTop.add(jbnStart);	//Added 5th component

        jbnStop = new JButton("Stop");
        jbnStop.addActionListener(new ButtonListener());	//Added 6th component
        jplTop.add(jbnStop);

        pBar = new JProgressBar();
        pBar.setStringPainted(true);
        Border border = BorderFactory.createLineBorder(Color.red, 2);
        pBar.setBorder(border);
        pBar.setBackground(Color.white);
        pBar.setForeground(Color.blue);
        pBar.setMinimum(0);

        pBar.setMaximum(Integer.parseInt(jtfInput.getText()));
        container.add(jplTop);
        container.add(pBar);

        // 7. Create a timer object.
        timer = new Timer(0, new TimerListener());

        pack();
        setVisible(true);
    }

    // 8. Timer listener that computes the sum of natural numbers,
    //    indicates the computation progress, and displays the result.
    class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (Integer.parseInt(jtfInput.getText())> 0){
            counter++;
            sum = sum+counter;
            pBar.setValue(counter);
            jtfOutput.setText(Integer.toString(sum));
            }
            else {
            	jtfOutput.setText("0");
            }

            if (counter >= Integer.parseInt(jtfInput.getText()))
               timer.stop();
        }
    }

    // 9. Button listener that actually starts or stops the
    //    process.
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();

            if (button.getText() == "Start") {
            	jtfOutput.setText("");
               if (jtfInput.getText() != " ") {
                    pBar.setMaximum(Integer.parseInt(
                               jtfInput.getText()));
                sum = 0;
                counter = 0;
                timer.start();
               }
            }
            else if (button.getText() == "Stop") {
                timer.stop();
                jtfOutput.setText("");
                sum = 0;
                counter = 0;
                pBar.setValue(0);
            }
        }
    }
    
    public static void main(String args[]){
    	new JProgressBarDemo();
    }
}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by DavidRyan
0

Firstly, the replace the following:

JProgressBarDemo() {

with

JProgressBarDemo() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

Otherwise the program will create a new process each time you run the program, and will not close that process when you close the window.

Now to display properly, you need to fill from the left of each row, so replace

jplTop.add(jtfOutput);    //Added 4th component

        jbnStart = new JButton("Start");

with

jplTop.add(jtfOutput);    //Added 4th component
        
        jplTop.add(Box.createHorizontalGlue());

        jbnStart = new JButton("Start");

to add a non-visable glue object in the first column, second row slot.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.