import javax.swing.table.DefaultTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author awseo
*/
public class GUI_To_Do_List extends javax.swing.JFrame {
/**
* Creates new form GUI_To_Do_List
*/
int task=0;
DefaultTableModel model;
public GUI_To_Do_List() {
initComponents();
model=(DefaultTableModel) List.getModel();
}
public static void Dosomething() {
new GUI_To_Do_List().setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Input_Dialog = new javax.swing.JDialog();
Cancel = new javax.swing.JButton();
Ok = new javax.swing.JButton();
Input_Task = new javax.swing.JLabel();
Type = new javax.swing.JLabel();
Priority = new javax.swing.JLabel();
Task_Text = new javax.swing.JTextField();
School = new javax.swing.JRadioButton();
Home = new javax.swing.JRadioButton();
Other = new javax.swing.JRadioButton();
Priority_Number = new javax.swing.JSpinner();
buttonGroup1 = new javax.swing.ButtonGroup();
Task = new javax.swing.JButton();
Edit = new javax.swing.JButton();
Delete = new javax.swing.JButton();
Exit = new javax.swing.JButton();
Task_List = new javax.swing.JScrollPane();
List = new javax.swing.JTable();
TaskProgress = new javax.swing.JProgressBar();
Refresh = new javax.swing.JButton();
Input_Dialog.setTitle("Task");
Input_Dialog.setLocation(new java.awt.Point(300, 120));
Input_Dialog.setMinimumSize(new java.awt.Dimension(400, 205));
Cancel.setText("Cancel");
Cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CancelActionPerformed(evt);
}
});
Ok.setText("OK");
Ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OkActionPerformed(evt);
}
});
Input_Task.setText("Task");
Type.setText("Type");
Priority.setText("Priority");
buttonGroup1.add(School);
School.setText("School");
School.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SchoolActionPerformed(evt);
}
});
buttonGroup1.add(Home);
Home.setText("Home");
buttonGroup1.add(Other);
Other.setText("Others");
javax.swing.GroupLayout Input_DialogLayout = new javax.swing.GroupLayout(Input_Dialog.getContentPane());
Input_Dialog.getContentPane().setLayout(Input_DialogLayout);
Input_DialogLayout.setHorizontalGroup(
Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(Input_DialogLayout.createSequentialGroup()
.addContainerGap()
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(Input_DialogLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Cancel))
.addGroup(Input_DialogLayout.createSequentialGroup()
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Input_Task, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Type, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Priority, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Task_Text)
.addGroup(Input_DialogLayout.createSequentialGroup()
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Priority_Number, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(School, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(Home)
.addGap(18, 18, 18)
.addComponent(Other)
.addGap(0, 126, Short.MAX_VALUE)))))
.addContainerGap())
);
Input_DialogLayout.setVerticalGroup(
Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, Input_DialogLayout.createSequentialGroup()
.addContainerGap()
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Input_Task)
.addComponent(Task_Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Type)
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(School)
.addComponent(Home)
.addComponent(Other)))
.addGap(18, 18, 18)
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Priority)
.addComponent(Priority_Number, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(Input_DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Ok)
.addComponent(Cancel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLocation(new java.awt.Point(300, 120));
Task.setText("New Task");
Task.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TaskActionPerformed(evt);
}
});
Edit.setText("Edit");
Edit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EditActionPerformed(evt);
}
});
Delete.setText("Delete");
Delete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DeleteActionPerformed(evt);
}
});
Exit.setText("Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
List.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Index", "Task", "Type", "Priority", "Status"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Boolean.class
};
boolean[] canEdit = new boolean [] {
false, true, true, true, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
List.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ListMouseClicked(evt);
}
});
Task_List.setViewportView(List);
TaskProgress.setString("Please work");
TaskProgress.setStringPainted(true);
Refresh.setText("Refresh");
Refresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RefreshActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Task)
.addGap(2, 2, 2)
.addComponent(Edit, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(Delete)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Refresh)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addComponent(Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(TaskProgress, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Task_List, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(Task_List, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(TaskProgress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Task)
.addComponent(Edit)
.addComponent(Delete)
.addComponent(Exit)
.addComponent(Refresh))
.addContainerGap())
);
pack();
}// </editor-fold>
private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {
if(List.getSelectedRow() != -1)
model.removeRow(List.getSelectedRow());
task--;
fill();
}
private void EditActionPerformed(java.awt.event.ActionEvent evt) {
if(List.getSelectedRow() != -1)
Edit();
}
void Edit(){
Input_Dialog.setVisible(true);
String tmp="";
Task_Text.setText(model.getValueAt(List.getSelectedRow(),1).toString());
tmp=model.getValueAt(List.getSelectedRow(),2).toString();
if(tmp.equals("School"))
School.setSelected(true);
else
School.setSelected(false);
if(tmp.equals("Home"))
Home.setSelected(true);
else
Home.setSelected(true);
if(tmp.equals("Other"))
Other.setSelected(true);
else
Other.setSelected(false);
Priority_Number.setValue(Integer.parseInt(model.getValueAt(List.getSelectedRow(),3).toString()));
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void OkActionPerformed(java.awt.event.ActionEvent evt) {
String temp="";
if(School.isSelected()){
temp="School";
}
if(Home.isSelected()){
temp="Home";
}
if(Other.isSelected()){
temp="Other";
}
model.addRow(new Object[]{1,Task_Text.getText(),temp,Priority_Number.getValue(),false});
Input_Dialog.dispose();
task++;
fill();
}
private void SchoolActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void CancelActionPerformed(java.awt.event.ActionEvent evt) {
Input_Dialog.dispose();
}
private void TaskActionPerformed(java.awt.event.ActionEvent evt) {
Input_Dialog.setVisible(true);
Task_Text.setText("");
School.setSelected(false);
Home.setSelected(false);
Other.setSelected(false);
Priority_Number.setValue(1);
}
private void ListMouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() == 2)
Edit();
}
private void RefreshActionPerformed(java.awt.event.ActionEvent evt) {
fill();
}
public void fill(){
int row= List.getRowCount();
int h = 0;
int count = 0;
while(h<=row){
Boolean chk = (Boolean) List.getValueAt(h,5);
if (chk==true){
count++;
}
h++;
}
TaskProgress.setValue((count/task)*100);
System.out.println(task);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI_To_Do_List.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI_To_Do_List.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI_To_Do_List.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI_To_Do_List.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new GUI_To_Do_List().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton Cancel;
private javax.swing.JButton Delete;
private javax.swing.JButton Edit;
private javax.swing.JButton Exit;
private javax.swing.JRadioButton Home;
private javax.swing.JDialog Input_Dialog;
private javax.swing.JLabel Input_Task;
private javax.swing.JTable List;
private javax.swing.JButton Ok;
private javax.swing.JRadioButton Other;
private javax.swing.JLabel Priority;
private javax.swing.JSpinner Priority_Number;
private javax.swing.JButton Refresh;
private javax.swing.JRadioButton School;
private javax.swing.JButton Task;
private javax.swing.JProgressBar TaskProgress;
private javax.swing.JScrollPane Task_List;
private javax.swing.JTextField Task_Text;
private javax.swing.JLabel Type;
private javax.swing.ButtonGroup buttonGroup1;
// End of variables declaration
}
This is the full code. I am using Netbeans so most of the GUI was defined there including the JTable and the Progress Bar.
public void fill(){
int row= List.getRowCount();
int h = 0;
int count = 0;
while(h<=row){
Boolean chk = (Boolean) List.getValueAt(h,5);
if (chk==true){
count++;
}
h++;
}
TaskProgress.setValue((count/task)*100);
System.out.println(task);
}
This is the code for changing the Progress bar depending on if Status is true or not. However, the progress bar doesn't respond to any changes.