Results 1 to 6 of 6
  1. #1
    chiragkini is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default Jtable Jspinner help

    Hi friends,
    I am working on small payroll project, where i need to implement Jtable having 6 columns for daily attendance . columns are i) employeeID, ii) Name iii) Present (yes/no--- combobox) iv) In time v) out time vi) OT hours (combobox)..

    Now I need to have Jspinner with TimeFormatter so the employee can select in and out time.

    I tried lots of code from google search results but alas! didnt succeed to get result.

    So please guys if anyone can help me out it would be great as I am running out of time...
    Please help me.....

    Thanks in Advance...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I think that at this point you'll have to post some more code. I recommend that you create and post the smallest program possible that is compilable, runnable, demonstrates your problem, and has no extraneous code not related to the problem, an Short, Self Contained, Correct (Compilable), Example Please see the link for details on how to create one of these because trust me, if your SSCCE is compliant with the specs, you'll likely get a very helpful answer from someone here quickly.

  3. #3
    chiragkini is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default Jspinner Help

    Thanks Fubarable for addressing my issue...

    Below is the code of table please consider the declaration


    Java Code:
    private void dispTable()
    	{
    		//FUNCTION DISPLAYS ATTENDANCE ENTRY SHEET
    
    			//TABLE
    		dtm= new DefaultTableModel(new String[][] {},new String[] {"ID","NAME","PRESENT","IN","O.T.","REASON"});
    
    		tableView = new JTable(dtm);
    		tableView.setGridColor(Color.RED);
    		tableView.setRowHeight(30);
            tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    		txtEmpNo=new JTextField("");
    		txtEmpNo.setEditable(false);
    		  TableColumn pCol0 = tableView.getColumn("ID");
            pCol0.setCellEditor(new DefaultCellEditor(txtEmpNo));
            pCol0.setPreferredWidth(100);
    
            JTextField txtname=new JTextField("");
            txtname.setEditable(false);
            TableColumn pCol1 = tableView.getColumn("NAME");
            pCol1.setCellEditor(new DefaultCellEditor(txtname));
            pCol1.setPreferredWidth(300);
    
    
    		cmbPresent = new JComboBox(new Object[]{"YES","NO","HALF DAY"});
    		cmbOT = new JComboBox(new Object[]{0,1,2,3,4,5,6,7,8});
            cmbOT.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmbPresentActionPerformed(evt);
                }
            });
    
            JTextField txt1=new JTextField("22:33:00 AM");// instead of this i want to use JSpiner with time Formatter
            TableColumn colINTime=tableView.getColumn("IN");
               
           colINTime.setCellEditor(new DefaultCellEditor(txt1));
    
     cmbReason = new JComboBox(new Object[]{"SELECT","C.L.","P.L."});
    		cmbReason.setSelectedIndex(1);
    
         cmbPresent.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmbPresentActionPerformed(evt);
                }
            });
    
    		TableColumn pCol2 = tableView.getColumn("PRESENT");
            pCol2.setCellEditor(new DefaultCellEditor(cmbPresent));
            pCol2.setPreferredWidth(150);
    
    
    		TableColumn pCol3 = tableView.getColumn("O.T.");
            pCol3.setCellEditor(new DefaultCellEditor(cmbOT));
            pCol3.setPreferredWidth(150);
    
    		TableColumn pCol4 = tableView.getColumn("REASON");
            pCol4.setCellEditor(new DefaultCellEditor(cmbReason));
    
            pCol4.setPreferredWidth(180);
           
    		scrollPane = new JScrollPane(tableView);
            scrollPane.setBounds(50, 50, 900, 480);
         getContentPane().add(scrollPane);
    
    
    		//add
    		//panel1.add(scrollPane, BorderLayout.CENTER);
    
    		//ADD ROWS TO THE TABLE CREATED ABOVE
    		displayAttendance();
    
    
    	}

    and this code i used to create rows depending on employees in database table

    Java Code:
     private void displayAttendance()
    	{
    		//FUNCTION DISPLAYS EMPLOYEE NUMBERS IN THE ATTENDANCE ENTRY SHEET
    
    		try
    		{
    
    
    			Object	rowData[]=new Object[5];
                String	sql="SELECT * FROM Employee_Profile";// WHERE Mth="+(calendar.get(Calendar.MONTH)+1)+"";
    			st=con.createStatement();
                rs=st.executeQuery(sql);
    
    			while(rs.next()){
    
    				rowData[0]=rs.getString("empCode");
                    rowData[1]=rs.getString("empFirstname")+" "+rs.getString("empmiddlename")+" "+rs.getString("empsurname");
                    rowData[2]="YES";
                    rowData[3]="10:10:10 AM";
    				rowData[4]="SELECT";
    
    			
    				dtm.addRow(rowData);
    			}
    		}
            catch(SQLException se)
            {
              System.out.print("Exception qwa+ "  +se.getMessage() );
            }
    
    		catch(Exception e)
    		{
    			JOptionPane.showMessageDialog(null,"Error While Reading Employee ID's"+e.getMessage());
    			e.printStackTrace();
    		}
    	}

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're asking a fairly complex question and presenting us with some complex code, but since that's not an SSCCE (please see the link above), it will be difficult and quite time consuming to be able to help you. Hopefully someone will be willing help you soon, but again if you want a greater chance of this occurring sooner, that someone will be willing to put in the time and effort to go through your code, you would be well advised to put in the time and effort to create an SSCCE.

    As always, best of luck!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Here is my SSCCE code of a table that uses a JSpinner to edit time. Please let me know if any of this makes sense or helps. If any Swing guru finds an error or knows of a better way to do this, please let me know.

    Java Code:
    import java.awt.Component;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellEditor;
    
    public class TableTimeSpinner {
      private Object[][] data = {{"John Smtih", new Date()},
        {"Mickey Mouse", new Date()}, {"Frank Stein", new Date()},
        {"Lizzy Borden", new Date()}};
    
      private JPanel mainPanel = new JPanel();
      private DefaultTableModel tableModel = new MyTableModel();
      private JTable table = new JTable(tableModel);
    
      public TableTimeSpinner() {
        try {
          setUpTable();
        } catch (ParseException e) {
          e.printStackTrace();
        }
    
        mainPanel.add(new JScrollPane(table));
      }
    
      private void setUpTable() throws ParseException {
        table.getColumnModel().getColumn(1).setCellRenderer(
            new MyTimeCellRenderer());
        table.getColumnModel().getColumn(1).setCellEditor(
            new MyTimeCellEditor());
        for (Object[] objArray : data) {
          tableModel.addRow(objArray);
        }
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
      private static void createAndShowUI() {
        JFrame frame = new JFrame("TableTimeSpinner");
        frame.getContentPane().add(new TableTimeSpinner().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    
    @SuppressWarnings("serial")
    class MyTableModel extends DefaultTableModel {
      public static final String[] COLUMN_NAMES = {"Name", "Time"};
    
      public MyTableModel() {
        super(COLUMN_NAMES, 0);
      }
    
      public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 1) {
          return Date.class;
        }
        return super.getColumnClass(columnIndex);
      }
    }
    
    @SuppressWarnings("serial")
    class MyTimeCellRenderer extends DefaultTableCellRenderer {
      DateFormat formatter = new SimpleDateFormat("hh:mm a");
    
      public void setValue(Object value) {
        setText((value == null) ? "" : formatter.format(value));
      }
    }
    
    @SuppressWarnings("serial")
    class MyTimeCellEditor extends AbstractCellEditor implements TableCellEditor {
      private static final String MID_TIME = "12:01 PM";
      private static final String BOTTOM_TIME = "00:01 AM";
      private static final String TOP_TIME = "11:59 PM";
      
      private SpinnerDateModel spinnerModel;
      private JSpinner dateSpinner;
      
      public MyTimeCellEditor() throws ParseException {
        SimpleDateFormat sdfWithDefaultYear = new SimpleDateFormat("hh:mm a");
        Date midDate = sdfWithDefaultYear.parse(MID_TIME);
        Date bottomDate = sdfWithDefaultYear.parse(BOTTOM_TIME);
        Date topDate = sdfWithDefaultYear.parse(TOP_TIME);
        spinnerModel = new SpinnerDateModel(midDate, bottomDate, topDate, Calendar.MINUTE);
        dateSpinner = new JSpinner(spinnerModel);
        dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "hh:mm a"));
      }
      
      public Object getCellEditorValue() {
        return spinnerModel.getValue();
      }
    
      public Component getTableCellEditorComponent(JTable table, Object value,
          boolean isSelected, int row, int column) {
        dateSpinner.setValue(value);
        return dateSpinner;
      }
    
    }

  6. #6
    chiragkini is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default Thanks .......

    Hey thanks dude.....

    I got my problem solved....
    Sorry for my complex code.....
    Next time i will follow the rules of SSCCE code...

    Thanks again....

Similar Threads

  1. [SOLVED] JSpinner in JTable column
    By nehaa in forum AWT / Swing
    Replies: 10
    Last Post: 05-11-2009, 01:34 PM
  2. Add a row in JTable
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 04-15-2009, 08:48 PM
  3. Regarding JTable
    By adeeb in forum SWT / JFace
    Replies: 0
    Last Post: 06-18-2008, 06:13 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 12:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •