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();
}
}