Hi,

I am trying to develop an application that has got a Jbutton and allows to search a file and then loads selected line of that file in the JTable. I am able to do that when I hard code it inside the code but when I try to let the user do it by adding FileChooser I am able getting runtime error like this:-

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Testing.TableCreator$1.actionPerformed(TableCreator.java:69)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6041)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5806)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4413)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2440)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Here is the line the error report is referring to :-

File file = fc.getSelectedFile();

Can somebody guide me where am I making mistakes ?
Here is my code below:-

public class TableCreator {
     public static JFrame fr = new JFrame("Item Menu");
     Container c = new JPanel();
     public JFileChooser fc;
     public JTable table;
  public TableCreator() {
    JLabel jLabel1= new JLabel("Item Name");
    JLabel jLabel4= new JLabel("File Name");

    JLabel jLabel7 = new javax.swing.JLabel();
    JButton jButton3 = new javax.swing.JButton();
    jLabel7.setText("List :");
    jButton3.setText("Browse");

    //jButton3.addMouseListener(new FileOpenMouseListener());
    jButton3.addActionListener(
         new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               JFileChooser fileChooser = new JFileChooser(".");
               int status = fileChooser.showOpenDialog(c);
             if (status == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                constructTable(file);
                JScrollPane scroll = new JScrollPane(table);
                c.add(scroll);
                //c.pack();
             } else if (status == JFileChooser.CANCEL_OPTION) {
               System.out.println("canceled");
             }
           }
        }
       );

      // Check Boxes start here
      JCheckBox checkFixed = new JCheckBox("Pepsi");
      JCheckBox dontFix = new JCheckBox("Coke");

      checkFixed.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
             System.out.println("coke");
          }
        }
      );

      dontFix.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
             System.out.println("pepsi");
          }
        }
      );
      // Check Boxes End here

      // Text Fields start here
    JTextField jTextField1 = new JTextField();
    JTextField jTextField2 = new JTextField();
    jTextField1.setText("Item Name");

    System.out.println(jTextField1.getText()); // get filename for C / C++ program
    System.out.println(jTextField2.getText());

    jTextField1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("action");
      }
    });
    jTextField2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("action");
      }
    });
     // Text Fields end here

     // define ScrollPane, Buttons and Mouse Listners
    JScrollPane scroll = new JScrollPane(table);
    JButton button1 = new JButton("Order");
    JButton button2 = new JButton("No Thanks");
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("call order program");
      }
    });
    button2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
                System.exit(0);
      }
    });

    JPanel three = new JPanel();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(three);
        three.setLayout(layout);


        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    //.addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    //.addComponent(comboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(498, Short.MAX_VALUE)
                .addComponent(button1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(button2)
                .addGap(30, 30, 30))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    //.addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    //.addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(27, 27, 27))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    //.addComponent(jLabel6)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton3)
                            //.addComponent(comboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dontFix)
                            .addComponent(checkFixed))))
                .addContainerGap(370, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    //.addComponent(jLabel2)
                    //.addComponent(comboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                )
                .addGap(33, 33, 33)
                //.addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    //.addComponent(comboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    //.addComponent(jLabel3)
                )
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jLabel4))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(dontFix)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkFixed))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)))
                .addGap(1, 1, 1)
                //.addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button2)
                    .addComponent(button1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

    c.add(three);
    fr.add(c);

  }
              private void constructTable(File file) {
                      final DefaultTableModel dm = new DefaultTableModel();
                         try {

                            Scanner scan = new Scanner(file);
                         if (scan.hasNextLine()) {
                            final String line = scan.nextLine();
                            String col0 = "Field#";
                            String col1 = "Field Name";
                            final String col2 = "Menu";
                            final String col3 = "Item";
                                   dm.addColumn(col0);
                                   dm.addColumn(col1);
                                   dm.addColumn(col2);
                                   dm.addColumn(col3);

                            String arr[] = line.split("\t");
                         for(int i=0; i<arr.length;i++){
                                   dm.insertRow(i,new Object[]{i+1,arr[i]});
                                                System.out.println(arr[i]);
                         }
                           TableColumn column0 =  table.getColumnModel().getColumn(0);
                                        column0.setPreferredWidth(1);
                            TableColumn column1 =  table.getColumnModel().getColumn(1);
                                        column1.setPreferredWidth(160);
                            TableColumn column2 =  table.getColumnModel().getColumn(2);
                                        column2.setPreferredWidth(240);
                            TableColumn column3 =  table.getColumnModel().getColumn(3);
                                        column3.setPreferredWidth(1);

                          table = new JTable(dm);
                          table.setPreferredScrollableViewportSize(new Dimension(800,400));
                          table.setFillsViewportHeight(true);

                          String[] answer = { "Item"};
                          String[] keyword = {"Detail"};

        int count = dm.getRowCount();

	        table.getColumn(col2).setCellRenderer(new RadioButtonRenderer(answer));
		table.getColumn(col2).setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer)));

                table.getColumn(col3).setCellRenderer(new CheckBoxRenderer(keyword));
		table.getColumn(col3).setCellEditor(new CheckBoxEditor(new CheckBoxPanel(keyword)));
                           }
                    } catch (FileNotFoundException ev) {
                                  ev.printStackTrace();
                    }
            }

  // Cell base
  class RadioButtonPanel extends JPanel {
		private JRadioButton[] buttons;
		private JRadioButton noSelectionButton;

		RadioButtonPanel(String[] str) {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			buttons = new JRadioButton[str.length];
                        ButtonGroup verticalGroup = new ButtonGroup();
                        ButtonGroup horizontalGroup = new ButtonGroup();

			noSelectionButton = new JRadioButton();
                        verticalGroup.add(noSelectionButton);
                        horizontalGroup.add(noSelectionButton);
			for (int i = 0; i < buttons.length; i++) {
				buttons[i] = new JRadioButton(str[i]);
				buttons[i].setFocusPainted(false);
				add(buttons[i]);
                                verticalGroup.add(buttons[i]);
			}
			for (int j = 0; j < buttons.length; j++) {
				buttons[j] = new JRadioButton(str[j]);
				buttons[j].setFocusPainted(false);
                                horizontalGroup.add(buttons[j]);
			}
		}

		public void addActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].addActionListener(anActionListener);
			}
		}

		public void removeActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].removeActionListener(anActionListener);
			}
		}

		public void setSelectedIndex(int index) {
			if (index < 0 || index >= buttons.length) {
				noSelectionButton.setSelected(true);
				return;
			}
			for (int i = 0; i < buttons.length; i++) {
				if (i == index) {
					buttons[i].setSelected(true);
					return;
				}
			}
		}

		public int getSelectedIndex() {
			for (int i = 0; i < buttons.length; i++) {
				if (buttons[i].isSelected()) {
					return i;
				}
			}
			return -1;
		}

		public JRadioButton[] getButtons() {
			return buttons;
		}
	}

class CheckBoxPanel extends JPanel {
		private JCheckBox[] buttons;
		private JRadioButton noSelectionButton;

		CheckBoxPanel(String[] str) {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			buttons = new JCheckBox[str.length];
			ButtonGroup group = new ButtonGroup();

			noSelectionButton = new JRadioButton();
			for (int i = 0; i < buttons.length; i++) {
				buttons[i] = new JCheckBox(str[i]);
				buttons[i].setFocusPainted(false);
				add(buttons[i]);
				group.add(buttons[i]);
			}
		}

		public void addActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].addActionListener(anActionListener);
			}
		}

		public void removeActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].removeActionListener(anActionListener);
			}
		}

		public void setSelectedIndex(int index) {
			if (index < 0 || index >= buttons.length) {
				noSelectionButton.setSelected(true);
				return;
			}
			for (int i = 0; i < buttons.length; i++) {
				if (i == index) {
					buttons[i].setSelected(true);
					return;
				}
			}
		}

		public int getSelectedIndex() {
			for (int i = 0; i < buttons.length; i++) {
				if (buttons[i].isSelected()) {
					return i;
				}
			}
			return -1;
		}

		public JCheckBox[] getButtons() {
			return buttons;
		}
	}

 	class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer {
		RadioButtonRenderer(String[] strs) {
			super(strs);
		}

		public Component getTableCellRendererComponent(JTable table,
		 Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
			if (value instanceof Integer) {
				setSelectedIndex(((Integer)value).intValue());
			}
			return this;
		}
	}

	class CheckBoxRenderer extends CheckBoxPanel implements TableCellRenderer {
		CheckBoxRenderer(String[] strs) {
			super(strs);
		}

		public Component getTableCellRendererComponent(JTable table,
		 Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
			if (value instanceof Integer) {
				setSelectedIndex(((Integer)value).intValue());
			}
			return this;
		}
	}

	class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
		private RadioButtonPanel theRadioButtonPanel;

		public RadioButtonEditor(RadioButtonPanel aRadioButtonPanel) {
			theRadioButtonPanel = aRadioButtonPanel;
			theRadioButtonPanel.addActionListener(this);
		}

		public Component getTableCellEditorComponent(JTable table,
		 Object value, boolean isSelected, int row, int column) {
			if (value instanceof Integer) {
				theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
			}
			return theRadioButtonPanel;
		}

		public Object getCellEditorValue() {
			return new Integer(theRadioButtonPanel.getSelectedIndex());
		}

		public void actionPerformed(ActionEvent e) {
			fireEditingStopped();
		}
	}

	class CheckBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
		private CheckBoxPanel theRadioButtonPanel;

		public CheckBoxEditor(CheckBoxPanel aRadioButtonPanel) {
			theRadioButtonPanel = aRadioButtonPanel;
			theRadioButtonPanel.addActionListener(this);
		}

		public Component getTableCellEditorComponent(JTable table,
		 Object value, boolean isSelected, int row, int column) {
			if (value instanceof Integer) {
				theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
			}
			return theRadioButtonPanel;
		}

		public Object getCellEditorValue() {
			return new Integer(theRadioButtonPanel.getSelectedIndex());
		}

		public void actionPerformed(ActionEvent e) {
			fireEditingStopped();
		}
	}

  public static void main(String[] args) {
      TableCreator wow = new TableCreator();
      TableCreator.fr.addWindowListener(new WindowAdapter() {
            @Override
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    TableCreator.fr.pack();
    TableCreator.fr.setVisible(true);

 }
}

Thanks

When you get that error it means that something is null. At that line it means that 'fc' is null. Did you initialize it? Because I didn't see it in your code.

Ya I corrected that initialization problem. Now I am not getting any runtime errors but my table is still not been filled after selecting the file. I think there is some minor problem that I am not looking at. Can you please help me to point it out. Here is the modified code:-

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.AbstractCellEditor;
import javax.swing.JFileChooser;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

/**
 * 
 */
public class TEST {
     public static JFrame fr = new JFrame("Item Menu");
     Container c = new JPanel();
     //public JFileChooser fc;
     JTable table;// = new JTable();
     JScrollPane scroll = new JScrollPane();
     
     public JTable getTable(){
         return table;
     }
     public void setTable(JTable table1){
          table1 = table;
     }
   
     public JScrollPane getScrollPane(){
         return scroll;
     }
     public void setScrollPane(JScrollPane scroll1){
          scroll1 = scroll;
     }
     
  public TEST() {
    JLabel jLabel1= new JLabel("Item Name");
    JLabel jLabel4= new JLabel("File Name");
 
    JLabel jLabel7 = new javax.swing.JLabel();
    JButton jButton3 = new javax.swing.JButton();
    jLabel7.setText("List :");
    jButton3.setText("Browse");
 
    //jButton3.addMouseListener(new FileOpenMouseListener());
    jButton3.addActionListener(
         new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               JFileChooser fileChooser = new JFileChooser(".");
               int status = fileChooser.showOpenDialog(c);
             if (status == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                constructTable(file);
                //JTable table = new JTable(); 
                JScrollPane scroll = new JScrollPane(getTable());
                setScrollPane(scroll);
                c.add(scroll);
                c.setVisible(true);
             } else if (status == JFileChooser.CANCEL_OPTION) {
               System.out.println("canceled");
             }
           }
        }
       );
 
      // Check Boxes start here
      JCheckBox checkFixed = new JCheckBox("Pepsi");
      JCheckBox dontFix = new JCheckBox("Coke");
 
      checkFixed.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
             System.out.println("coke");
          }
        }
      );
 
      dontFix.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
             System.out.println("pepsi");
          }
        }
      );
      // Check Boxes End here
 
      // Text Fields start here
    JTextField jTextField1 = new JTextField();
    JTextField jTextField2 = new JTextField();
    jTextField1.setText("Item Name");
 
    System.out.println(jTextField1.getText()); // get filename for C / C++ program
    System.out.println(jTextField2.getText());
 
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("action");
      }
    });
    jTextField2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("action");
      }
    });
     // Text Fields end here
 
     // define ScrollPane, Buttons and Mouse Listners
    scroll = new JScrollPane(getTable());
    JButton button1 = new JButton("Order");
    JButton button2 = new JButton("No Thanks");
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("call order program");
      }
    });
    button2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
                System.exit(0);
      }
    });
 
    JPanel three = new JPanel();
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(three);
        three.setLayout(layout);
 
 
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    //.addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    //.addComponent(comboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(498, Short.MAX_VALUE)
                .addComponent(button1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(button2)
                .addGap(30, 30, 30))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(getScrollPane(), javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    //.addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    //.addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(27, 27, 27))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    //.addComponent(jLabel6)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton3)
                            //.addComponent(comboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dontFix)
                            .addComponent(checkFixed))))
                .addContainerGap(370, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    //.addComponent(jLabel2)
                    //.addComponent(comboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                )
                .addGap(33, 33, 33)
                //.addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    //.addComponent(comboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    //.addComponent(jLabel3)
                )
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jLabel4))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(dontFix)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkFixed))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)))
                .addGap(1, 1, 1)
                //.addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addComponent(getScrollPane(), javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button2)
                    .addComponent(button1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
    c.add(three);
    fr.add(c);
 
  }
              private void constructTable(File file) {
                      final DefaultTableModel dm = new DefaultTableModel();
                         try {
 
                            Scanner scan = new Scanner(file);
                         if (scan.hasNextLine()) {
                            final String line = scan.nextLine();
                            String col0 = "Field#";
                            String col1 = "Field Name";
                            final String col2 = "Menu";
                            final String col3 = "Item";
                                   dm.addColumn(col0);
                                   dm.addColumn(col1);
                                   dm.addColumn(col2);
                                   dm.addColumn(col3);
 
                            String arr[] = line.split("\t");
                         for(int i=0; i<arr.length;i++){
                                   dm.insertRow(i,new Object[]{i+1,arr[i]});
                                                System.out.println(arr[i]);
                         }
/*                           TableColumn column0 =  table.getColumnModel().getColumn(0);
                                        column0.setPreferredWidth(1);
                            TableColumn column1 =  table.getColumnModel().getColumn(1);
                                        column1.setPreferredWidth(160);
                            TableColumn column2 =  table.getColumnModel().getColumn(2);
                                        column2.setPreferredWidth(240);
                            TableColumn column3 =  table.getColumnModel().getColumn(3);
                                        column3.setPreferredWidth(1);
*/ 
                          table = new JTable(dm);
                          setTable(table);
                          table.setPreferredScrollableViewportSize(new Dimension(800,400));
                          table.setFillsViewportHeight(true);
 
                          String[] answer = { "Item"};
                          String[] keyword = {"Detail"};
 
        int count = dm.getRowCount();
 
	        table.getColumn(col2).setCellRenderer(new RadioButtonRenderer(answer));
		table.getColumn(col2).setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer)));
 
                table.getColumn(col3).setCellRenderer(new CheckBoxRenderer(keyword));
		table.getColumn(col3).setCellEditor(new CheckBoxEditor(new CheckBoxPanel(keyword)));
                           }
                    } catch (FileNotFoundException ev) {
                                  ev.printStackTrace();
                    }
            }
 
  // Cell base
  class RadioButtonPanel extends JPanel {
		private JRadioButton[] buttons;
		private JRadioButton noSelectionButton;
 
		RadioButtonPanel(String[] str) {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			buttons = new JRadioButton[str.length];
                        ButtonGroup verticalGroup = new ButtonGroup();
                        ButtonGroup horizontalGroup = new ButtonGroup();
 
			noSelectionButton = new JRadioButton();
                        verticalGroup.add(noSelectionButton);
                        horizontalGroup.add(noSelectionButton);
			for (int i = 0; i < buttons.length; i++) {
				buttons[i] = new JRadioButton(str[i]);
				buttons[i].setFocusPainted(false);
				add(buttons[i]);
                                verticalGroup.add(buttons[i]);
			}
			for (int j = 0; j < buttons.length; j++) {
				buttons[j] = new JRadioButton(str[j]);
				buttons[j].setFocusPainted(false);
                                horizontalGroup.add(buttons[j]);
			}
		}
 
		public void addActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].addActionListener(anActionListener);
			}
		}
 
		public void removeActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].removeActionListener(anActionListener);
			}
		}
 
		public void setSelectedIndex(int index) {
			if (index < 0 || index >= buttons.length) {
				noSelectionButton.setSelected(true);
				return;
			}
			for (int i = 0; i < buttons.length; i++) {
				if (i == index) {
					buttons[i].setSelected(true);
					return;
				}
			}
		}
 
		public int getSelectedIndex() {
			for (int i = 0; i < buttons.length; i++) {
				if (buttons[i].isSelected()) {
					return i;
				}
			}
			return -1;
		}
 
		public JRadioButton[] getButtons() {
			return buttons;
		}
	}
 
class CheckBoxPanel extends JPanel {
		private JCheckBox[] buttons;
		private JRadioButton noSelectionButton;
 
		CheckBoxPanel(String[] str) {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			buttons = new JCheckBox[str.length];
			ButtonGroup group = new ButtonGroup();
 
			noSelectionButton = new JRadioButton();
			for (int i = 0; i < buttons.length; i++) {
				buttons[i] = new JCheckBox(str[i]);
				buttons[i].setFocusPainted(false);
				add(buttons[i]);
				group.add(buttons[i]);
			}
		}
 
		public void addActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].addActionListener(anActionListener);
			}
		}
 
		public void removeActionListener(ActionListener anActionListener) {
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].removeActionListener(anActionListener);
			}
		}
 
		public void setSelectedIndex(int index) {
			if (index < 0 || index >= buttons.length) {
				noSelectionButton.setSelected(true);
				return;
			}
			for (int i = 0; i < buttons.length; i++) {
				if (i == index) {
					buttons[i].setSelected(true);
					return;
				}
			}
		}
 
		public int getSelectedIndex() {
			for (int i = 0; i < buttons.length; i++) {
				if (buttons[i].isSelected()) {
					return i;
				}
			}
			return -1;
		}
 
		public JCheckBox[] getButtons() {
			return buttons;
		}
	}
 
 	class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer {
		RadioButtonRenderer(String[] strs) {
			super(strs);
		}
 
		public Component getTableCellRendererComponent(JTable table,
		 Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
			if (value instanceof Integer) {
				setSelectedIndex(((Integer)value).intValue());
			}
			return this;
		}
	}
 
	class CheckBoxRenderer extends CheckBoxPanel implements TableCellRenderer {
		CheckBoxRenderer(String[] strs) {
			super(strs);
		}
 
		public Component getTableCellRendererComponent(JTable table,
		 Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
			if (value instanceof Integer) {
				setSelectedIndex(((Integer)value).intValue());
			}
			return this;
		}
	}
 
	class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
		private RadioButtonPanel theRadioButtonPanel;
 
		public RadioButtonEditor(RadioButtonPanel aRadioButtonPanel) {
			theRadioButtonPanel = aRadioButtonPanel;
			theRadioButtonPanel.addActionListener(this);
		}
 
		public Component getTableCellEditorComponent(JTable table,
		 Object value, boolean isSelected, int row, int column) {
			if (value instanceof Integer) {
				theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
			}
			return theRadioButtonPanel;
		}
 
		public Object getCellEditorValue() {
			return new Integer(theRadioButtonPanel.getSelectedIndex());
		}
 
		public void actionPerformed(ActionEvent e) {
			fireEditingStopped();
		}
	}
 
	class CheckBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
		private CheckBoxPanel theRadioButtonPanel;
 
		public CheckBoxEditor(CheckBoxPanel aRadioButtonPanel) {
			theRadioButtonPanel = aRadioButtonPanel;
			theRadioButtonPanel.addActionListener(this);
		}
 
		public Component getTableCellEditorComponent(JTable table,
		 Object value, boolean isSelected, int row, int column) {
			if (value instanceof Integer) {
				theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
			}
			return theRadioButtonPanel;
		}
 
		public Object getCellEditorValue() {
			return new Integer(theRadioButtonPanel.getSelectedIndex());
		}
 
		public void actionPerformed(ActionEvent e) {
			fireEditingStopped();
		}
	}
 
  public static void main(String[] args) {
      TEST wow = new TEST();
      TEST.fr.addWindowListener(new WindowAdapter() {
            @Override
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    TEST.fr.pack();
    TEST.fr.setVisible(true);
 
 }
}

How many lines does the file has because you are not reading it correctly.
If the line you read is like this:
> Field_Name Menu Item
Then the array will have the elements of ONE row and you need to put all of them in ONE row:

String arr[] = line.split("\t");

dm.insertRow(i,new Object[]{i+1,arr[0], arr[1], arr[2]});

Try this first.

Then instead of an if (scanner.next()) you must have a while in order to read all the lines.

Also, why the DefaultTableModel has to be final?

And this:

String col0 = "Field#";
String col1 = "Field Name";
final String col2 = "Menu";
final String col3 = "Item";
dm.addColumn(col0);
dm.addColumn(col1);
dm.addColumn(col2);
dm.addColumn(col3);

has to be outside the while. You add the columns only once. then in while loop you insert as many rows as needed

Great !! It helped me a lot to organize my code ... really helpful.
One more thing I just want to show the first line of a file in JTable thats why I am using if loop with scanner. I did all the changes you said but still not able to make the data appear in JTable. The System.out.println is printing all my fields correctly but not able to fill the table. What could be the reason.

Thanks in advance

Check this method and tell me what is wrong:

public void setTable(JTable table1){
  table1 = table;
}

Well have you fixed the method?
Also for the example, I would suggest to go and just add the radioButtons in the table:

dm.insertRow(i,new Object[]{i+1,arr[0], arr[1], arr[2], JRadioButton, JButton,  . . . });

With the appropriate columns of course.

Have you removed the final from the DefaultTableModel. I don't think it's needed.

Hi I changed my getter and setter methods like this but still no success :(

public JTable getTable(){
         return this.table;
     }
     public void setTable(JTable table){
          this.table = table;
     }

Yes I removed final from DefaultTableModel...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.