Judging the Relevance and worth of ideas part 2.pptx
How do I make my JTable non editableimport java.awt.; import j.pdf
1. 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;
2. 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);
3. 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
5. 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();
6. 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;
7. 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);
}
}
8. 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().sub
string(4,8));
}
10. }
}
Solution
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;}}