Even if no errors on Compilation and Runtime, Program output is not as expected

116 views Asked by At

A Java Swing applet which includes 3 Checkboxes and a TextField. The specified checkbox when clicked displays title assigned to the corresponding checkbox in the given TextField. The problem is that even if no errors on Compilation and Runtime, a blank frame is shown as output..!!

here is the Java code :

SwingAll class :

package swingall;

import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class SwingAll extends JFrame implements ItemListener {

JTextField t1;
JCheckBox c1,c2,c3;

public void init()
{
    Container cp=getContentPane();
    t1=new JTextField(20);
    c1=new JCheckBox("Arts");
    c2=new JCheckBox("Commerce");
    c3=new JCheckBox("Science");
    add(c1);
    add(c2);
    add(c3);
    add(t1);
    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
}

public void itemStateChanged(ItemEvent e) {

    if (e.getSource()==c1)
    {
        t1.setText("Arts");
    }
    if (e.getSource()==c2)
    {
        t1.setText("Commerce");
    }
    if (e.getSource()==c3)
    {
        t1.setText("Science");
    }
}
}

The Main Class is :

package swingall;

public class Main {

public static void main(String[] args) {

    SwingAll sg=new swingAll();
    sg.setSize(500, 500);
    sg.setVisible(true);
}
}
4

There are 4 answers

0
SLaks On BEST ANSWER

You never called your init() method.

Therefore, you never put anything into the frame.

0
Kayz On

You dont call your init-method. Add a constructor

public SwingAll () {
   init();
}
0
alanazi On

you need to make your class init() to constructor which it should be SwingAll try adding private static final long serialVersionUID = 1L; i don't see a title for the window try setting

super("window name"); 

and

 setDefaultCloseOperation(EXIT_ON_CLOSE);
1
Andrew Thompson On

As mentioned in other answers, init() was not being called. OTOH, when it is, only one component is displayed. This variant fixes both problems.

Example GUI

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

public class SwingAll extends JFrame implements ItemListener {

JTextField t1;
JCheckBox c1,c2,c3;

public void init()
{
    // end the JVM when frame is closed.
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Container cp=getContentPane();
    // set a layout that takes many components without a 
    // constraint and displays them.
    cp.setLayout(new FlowLayout());
    t1=new JTextField(20);
    c1=new JCheckBox("Arts");
    c2=new JCheckBox("Commerce");
    c3=new JCheckBox("Science");
    add(c1);
    add(c2);
    add(c3);
    add(t1);
    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
    // pack the GUI to the size needed to display the content
    pack();
}

public void itemStateChanged(ItemEvent e) {

    if (e.getSource()==c1)
    {
        t1.setText("Arts");
    }
    if (e.getSource()==c3)
    {
        t1.setText("Commerce");
    }
    if (e.getSource()==c3)
    {
        t1.setText("Science");
    }
}

public static void main(String[] args) {
    // should be started on the EDT.
    SwingAll sg= new SwingAll();
    sg.init();
    sg.setVisible(true);
}
}

Other tips

  1. // should be started on the EDT. Swing GUIs should be started and updated on the Event Dispatch Thread - left as an exercise for the user.
  2. The default layout of a frame is BorderLayout. This layout has 5 possible areas that can be specified by constraints, each of which can hold exactly one component. By adding the components to the border layout without constraints, they all defaulted to BorderLayout.CENTER.
  3. Don't set the size of the GUI, simply call pack() after the components are added.
  4. If those check-boxes should be 'one only at a time', look to JRadioButton and ButtonGroup.