mga sir bago lang po ako sa programming, meron akong project na GUI program and I need some help,
eto po ang project description
Program able to use file dialog to obtain filename for input JSON file and
place the data into the JTable.
Program able to use file dialog to get filename for output JSON file. The
output file must be properly formatted. A correctly formatted output file
should be readable by your application.
Program must use a custom dialog that extends the JDialog class to obtain
a new Course object and append this to the JTable.
JTable must be correctly sortable by any of the columns.
currently, nagawa ko na po ung JTable at column, ang prob ko eh paano ko ma open ung json file para ma populate ung Jtable,
eto po ang code ko,
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JTable;
class Proj3 extends MyFrame implements ActionListener {
static private final String newline = "\n";
JTextArea log;
public Proj3(String title) {
super(title);
setBounds(10,10,400,300);
setDefaultCloseOperation(Proj3.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu file = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem quit = new JMenuItem("Quit");
file.add(open);
file.add(save);
file.add(quit);
quit.addActionListener(this);
open.addActionListener(this);
JMenu data = new JMenu("Data");
JMenuItem clear = new JMenuItem("Clear Data");
JMenuItem add = new JMenuItem("Add Course");
JMenuItem remove = new JMenuItem("Remove row(s)");
data.add(clear);
data.add(add);
data.add(remove);
menuBar.add(file);
menuBar.add(data);
}
public static void main(String[] args) {
Proj3 myApp =
new Proj3("My Frame");
myApp.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String choice = ae.getActionCommand();
if (choice.equals("Open")) {
JFileChooser fileCh = new JFileChooser(".");
int userChoice = fileCh.showOpenDialog(this);
if (userChoice == JFileChooser.APPROVE_OPTION) {
File file = fileCh.getSelectedFile();
log.append(file.getName());
}
else {
log.append(newline);
}
}
else if (choice.equals("Quit")) {
System.exit(0);
}
}
}
at eto naman ang abstract table model
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
public class MyJTable extends AbstractTableModel {
private String[] columnNames = {"Name","Instructor","Crn","Credits"};
private ArrayList<Course> myList;
public MyJTable(CourseList cList) {
myList = cList.getCourses();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
int size;
if (myList == null) {
size = 0;
}
else {
size = myList.size();
}
return size;
}
public Object getValueAt(int row, int col) {
Object temp = null;
if (col == 0) {
temp = myList.get(row).getName();
}
else if (col == 1) {
temp = myList.get(row).getInstructor();
}
else if (col == 2) {
temp = new Integer(myList.get(row).getCrn());
}
else if (col == 3) {
temp = new Double(myList.get(row).getCredits());
}
return temp;
}
// needed to show column names in JTable
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int col) {
if (col == 2) {
return Integer.class;
}
else if (col == 3) {
return Double.class;
}
else {
return String.class;
}
}
}
eto naman ang JFrame
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class MyFrame extends JFrame implements ActionListener {
private Object[][] data;
private String[] columnNames = {"Name","Instructor","Crn","Credits"};
private MyJTable tableModel;
private JTable table;
private CourseList myList;
public MyFrame(String title) {
super(title);
setBounds(40,40,600,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myList = new CourseList();
//myList.readFromtoJSON("courses.json");
//data = myList.convert2Data();
tableModel = new MyJTable(myList);
table = new JTable(tableModel);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(380,280));
JPanel panel = new JPanel();
panel.add(scrollPane);
add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae) {
}
public static void main(String[] args) {
MyFrame myApp = new MyFrame("JTable");
myApp.setVisible(true);
}
}
salamat poh
eto po ang project description
Program able to use file dialog to obtain filename for input JSON file and
place the data into the JTable.
Program able to use file dialog to get filename for output JSON file. The
output file must be properly formatted. A correctly formatted output file
should be readable by your application.
Program must use a custom dialog that extends the JDialog class to obtain
a new Course object and append this to the JTable.
JTable must be correctly sortable by any of the columns.
currently, nagawa ko na po ung JTable at column, ang prob ko eh paano ko ma open ung json file para ma populate ung Jtable,
eto po ang code ko,
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JTable;
class Proj3 extends MyFrame implements ActionListener {
static private final String newline = "\n";
JTextArea log;
public Proj3(String title) {
super(title);
setBounds(10,10,400,300);
setDefaultCloseOperation(Proj3.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu file = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem quit = new JMenuItem("Quit");
file.add(open);
file.add(save);
file.add(quit);
quit.addActionListener(this);
open.addActionListener(this);
JMenu data = new JMenu("Data");
JMenuItem clear = new JMenuItem("Clear Data");
JMenuItem add = new JMenuItem("Add Course");
JMenuItem remove = new JMenuItem("Remove row(s)");
data.add(clear);
data.add(add);
data.add(remove);
menuBar.add(file);
menuBar.add(data);
}
public static void main(String[] args) {
Proj3 myApp =
new Proj3("My Frame");
myApp.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String choice = ae.getActionCommand();
if (choice.equals("Open")) {
JFileChooser fileCh = new JFileChooser(".");
int userChoice = fileCh.showOpenDialog(this);
if (userChoice == JFileChooser.APPROVE_OPTION) {
File file = fileCh.getSelectedFile();
log.append(file.getName());
}
else {
log.append(newline);
}
}
else if (choice.equals("Quit")) {
System.exit(0);
}
}
}
at eto naman ang abstract table model
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
public class MyJTable extends AbstractTableModel {
private String[] columnNames = {"Name","Instructor","Crn","Credits"};
private ArrayList<Course> myList;
public MyJTable(CourseList cList) {
myList = cList.getCourses();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
int size;
if (myList == null) {
size = 0;
}
else {
size = myList.size();
}
return size;
}
public Object getValueAt(int row, int col) {
Object temp = null;
if (col == 0) {
temp = myList.get(row).getName();
}
else if (col == 1) {
temp = myList.get(row).getInstructor();
}
else if (col == 2) {
temp = new Integer(myList.get(row).getCrn());
}
else if (col == 3) {
temp = new Double(myList.get(row).getCredits());
}
return temp;
}
// needed to show column names in JTable
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int col) {
if (col == 2) {
return Integer.class;
}
else if (col == 3) {
return Double.class;
}
else {
return String.class;
}
}
}
eto naman ang JFrame
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class MyFrame extends JFrame implements ActionListener {
private Object[][] data;
private String[] columnNames = {"Name","Instructor","Crn","Credits"};
private MyJTable tableModel;
private JTable table;
private CourseList myList;
public MyFrame(String title) {
super(title);
setBounds(40,40,600,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myList = new CourseList();
//myList.readFromtoJSON("courses.json");
//data = myList.convert2Data();
tableModel = new MyJTable(myList);
table = new JTable(tableModel);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(380,280));
JPanel panel = new JPanel();
panel.add(scrollPane);
add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae) {
}
public static void main(String[] args) {
MyFrame myApp = new MyFrame("JTable");
myApp.setVisible(true);
}
}
salamat poh