Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I make my JTable non editable? import java.awt.*; import javax.swing.tabl

ID: 3780161 • Letter: H

Question

How do I make my JTable non editable?

import java.awt.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.nio.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class SwingDemo implements ActionListener, KeyListener
{

JLabel search, output;
JFrame viewer;
JTable table = new JTable(new DefaultTableModel(null, new Object []{"Row ID", "First Name", "Last Name", "EMPLID", "GPA", "Venus Login"}));

JFrame viewerAdd;

DefaultTableModel model = (DefaultTableModel) table.getModel();


JTextField first = new JTextField(20);
JTextField last = new JTextField(20);
JTextField emplid = new JTextField(20);
JTextField gpa = new JTextField(20);
JTextField venus = new JTextField(10);

String Venus;

public static int rowCount = 1;
public final static int LOAD = 0;
    public final static int SAVE = 1;
public SwingDemo()
{
  

  //Creates Java Frame
  viewer = new JFrame("Final Project");
  viewer.setSize(570, 650);
  viewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  viewer.setLayout(new FlowLayout());
  viewer.setLocationRelativeTo(viewer);
  

  //Creates Drop Down Menu
  String[] option = { "Row ID", "First Name", "Last Name", "EMPLID", "GPA", "Venus Login" };
  JComboBox options = new JComboBox(option);
  options.setSelectedIndex(0);
  
  //Creates Button and Labels
  search = new JLabel("Search by: ");
  JButton addButton = new JButton("Add");
  JButton deleteButton = new JButton("Delete");
  JButton exportButton = new JButton("Export Data");
  
  //Creates JTable
  JTextField textBox = new JTextField(20);
  table = new JTable(model);
  JScrollPane scrollPane = new JScrollPane(table);
  table.setFillsViewportHeight(true);
  
  //Creates Menu Bar
  JMenuBar menuBar = new JMenuBar();
  JMenu fileMenu = new JMenu("File");
  JMenu helpMenu = new JMenu("Help");
  menuBar.add(fileMenu);
  menuBar.add(helpMenu);
  JMenuItem openAction = new JMenuItem("Open");
  JMenuItem exitAction = new JMenuItem("Exit");
  JMenuItem exportAction = new JMenuItem("Export");
  JMenuItem aboutAction = new JMenuItem("About");
  fileMenu.add(openAction);
  fileMenu.add(exportAction);
  fileMenu.add(exitAction);
  helpMenu.add(aboutAction);
     //fileChooser.setDialogTitle("Choose a file");
    
     //ActionListener
     openAction.addActionListener(this);
     exitAction.addActionListener(this);
     aboutAction.addActionListener(this);
     addButton.addActionListener(this);
     //Action Events
    
    
  //Adds Elements to Java Frame
     viewer.setJMenuBar(menuBar);
  viewer.add(search);
  viewer.add(options);
  viewer.add(textBox);
  viewer.add(addButton);
  viewer.add(deleteButton);
  viewer.add(scrollPane);
  viewer.add(exportButton);
  viewer.setVisible(true);
}
public void addUser()
{
  JButton OK = new JButton("OK");
  JButton CANCEL = new JButton("CANCEL");
  //JTextField
  
  
  //Strings

  
  JLabel firstName = new JLabel("First Name");
  JLabel lastName = new JLabel("Last Name");
  JLabel EMPLID = new JLabel("EMPLID");
  JLabel GPA = new JLabel("GPA");
  JLabel venusLogin = new JLabel("Venus Login");
  
  //ActionListener
  first.addActionListener(this);
  last.addActionListener(this);
  emplid.addActionListener(this);
  gpa.addActionListener(this);
  first.addKeyListener(this);
  last.addKeyListener(this);
  emplid.addKeyListener(this);
  
  viewerAdd = new JFrame("Add User");
  viewerAdd.setSize(300, 200);
  viewerAdd.setDefaultCloseOperation(viewerAdd.HIDE_ON_CLOSE);
  viewerAdd.setLayout(new GridLayout(6,2));
  viewerAdd.setLocationRelativeTo(viewerAdd);
  viewerAdd.add(firstName);
  viewerAdd.add(first);
  viewerAdd.add(lastName);
  viewerAdd.add(last);
  viewerAdd.add(EMPLID);
  viewerAdd.add(emplid);
  viewerAdd.add(GPA);
  viewerAdd.add(gpa);
  viewerAdd.add(venusLogin);
  viewerAdd.add(venus);
  viewerAdd.add(OK);
  viewerAdd.add(CANCEL);
  OK.addActionListener(this);
     CANCEL.addActionListener(this);
     venus.setEditable(false);
  viewerAdd.setVisible(true);
  
  
}



@Override
public void actionPerformed(ActionEvent e) throws NumberFormatException
{
  switch(e.getActionCommand())
  {
   case "Exit":
    System.exit(0);
   break;
   
   case "Open":
    

    FileDialog fileChooser = new FileDialog(viewerAdd,"Select file",FileDialog.LOAD);
    fileChooser.setVisible(true);
    JFileChooser a = new JFileChooser(fileChooser.getFile());
    
    File[] file;
    file = fileChooser.getFiles();
    if(fileChooser.getFile() != null)
     {
      try
       {
           BufferedReader br = new BufferedReader(new FileReader(file[0]));
        String line;
        while ((line = br.readLine()) != null)
         {
          StringTokenizer st = new StringTokenizer(line, "," +" ");
              String f = st.nextToken();
              String l = st.nextToken();
              String em = st.nextToken();
              String g = st.nextToken();
              String v = st.nextToken();
              model.addRow(new Object[]{rowCount++,f, l, em, g, v});
            }

       }
      catch (FileNotFoundException ex)
       {
        System.out.println("o");
       }
      catch (IOException e1)
       {
        JOptionPane.showMessageDialog(fileChooser, "There was an IO Exception that was caught. Error: "+ e1.getMessage(), null, JOptionPane.ERROR_MESSAGE);
        e1.printStackTrace();
       }
     }
    else
     {
      JOptionPane.showMessageDialog(null, "File Not Selected", null, JOptionPane.ERROR_MESSAGE);
     }
   break;
   
   case "About":
    JOptionPane.showMessageDialog(viewer, "This is an App made by ");
   break;
   
   case "Add":
    addUser();
    break;
    
   case "OK":
    int userAction1 = JOptionPane.showConfirmDialog(null, "Are You Sure You Want To Insert this Record?");
    int number;
    double number1;
    boolean emp, ga = false;
    if(userAction1 == JOptionPane.YES_OPTION)
    {
     String firstName = first.getText();
     String lastName = last.getText();
     String Emplid = emplid.getText();
     String Gpa = gpa.getText();
    
     if (firstName.length() < 2)
      JOptionPane.showMessageDialog(null, "First Name must contain at least 2 characters", null, JOptionPane.ERROR_MESSAGE);
     else if (lastName.length() < 2)
      JOptionPane.showMessageDialog(null, "Last Name must contain at least 2 characters", null, JOptionPane.ERROR_MESSAGE);
     else if (Emplid.length()!=8)
      JOptionPane.showMessageDialog(null, "EMPLID must contain 8 numbers", null, JOptionPane.ERROR_MESSAGE);   
      try
      {
       number = Integer.parseInt(Emplid);
       try
       {
        number1 = Double.parseDouble(Gpa);
        if(number1 < 0 || number1 > 4)
         JOptionPane.showMessageDialog(null, "GPA must be between 0.0 - 4.0", null, JOptionPane.ERROR_MESSAGE);
        else
         ga = true;      
       }
       catch (Exception s)
       {
        JOptionPane.showMessageDialog(null, "GPA must be between 0.0 - 4.0", null, JOptionPane.ERROR_MESSAGE);
       }
       
      }
      catch (Exception s)
      {
       JOptionPane.showMessageDialog(null, "EMPLID must contain 8 numbers only", null, JOptionPane.ERROR_MESSAGE);
      }

    
     
     if (ga == true)
     {
      model.addRow(new Object[]{rowCount++,firstName, lastName, Emplid, Gpa, Venus});
          
      viewerAdd.setVisible(false);
      first = new JTextField(20);
      last = new JTextField(20);
      emplid = new JTextField(20);
      gpa = new JTextField(20);
      venus = new JTextField(10);
     }
    }
    break;
    
   case "CANCEL":
    viewerAdd.setVisible(false);
   break;
  }
  
}
@Override
public void keyTyped(KeyEvent e)
{
  try
  {
   venus.setText(last.getText().substring(0,2)+first.getText().substring(0,2)+emplid.getText().substring(4,8));
   
  }
  catch(Exception s)
  {
   
  }
  Venus = venus.getText();
}
@Override
public void keyPressed(KeyEvent e)
{
  try
  {
   venus.setText(last.getText().substring(0,2)+first.getText().substring(0,2)+emplid.getText().substring(4,8));
   
  }
  catch(Exception s)
  {
   
  }
  Venus = venus.getText();


}
@Override
public void keyReleased(KeyEvent e)
{
  try
  {
   venus.setText(last.getText().substring(0,2)+first.getText().substring(0,2)+emplid.getText().substring(4,8));
   
  }
  catch(Exception s)
  {
   
  }
  Venus = venus.getText();

}


}

Explanation / Answer

step 1:Go to the Navigation Pane, expand JScrollPane option

step 2:In JSCROLLPne, right-click on JTable and Select Customize Code as shown below:

step 3:On the code customizer option, choose custom property in the second drop down

step 4: Insert the code written below ,before the last bracket);code is given below

step5:{public boolean isCellEditable(int row, int column){return false;}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote