Page 2 of 2 FirstFirst 12
Results 21 to 40 of 40
  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Other problems:
    1) The main class extends JFrame, but it doesn't act like one. Sure you display the app as a JFrame from within the main method, but it's an empty JFrame that does nothing.
    2) Not overriding DefaultTableModel's getColumnClass so that it will return Double.class.
    3) Hard-coding the lastRow variable rather than using the DefaultTableModel's own getRowCount result.
    4) Calling setVisible(true) before you're done setting up your GUI. (as Norm suggests above)

    edit:
    5) You're not adding the JTable directly to a JScrollPane, and this is why the columns are not being displayed.
    Last edited by Fubarable; 08-01-2010 at 01:55 AM.

  2. #22
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    Norm,

    I did what you suggested, but I lose my frame title sine I can't do.
    XML Code:
             public static void main(String[] args)
    	{
    		MoneyNest4You mn4y = new MoneyNest4You("MoneyNest4You");
    		mn4y.setDefaultCloseOperation( EXIT_ON_CLOSE );
    		mn4y.setSize(1010, 300);
    		mn4y.setLocationRelativeTo( null );
    		mn4y.setVisible(true);
    	}

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ninjalord918 View Post
    Norm,

    I did what you suggested, but I lose my frame title sine I can't do.
    XML Code:
             public static void main(String[] args)
    	{
    		MoneyNest4You mn4y = new MoneyNest4You("MoneyNest4You");
    		mn4y.setDefaultCloseOperation( EXIT_ON_CLOSE );
    		mn4y.setSize(1010, 300);
    		mn4y.setLocationRelativeTo( null );
    		mn4y.setVisible(true);
    	}
    Please read my suggestions above. All this code above in the main method is wasted meaningless code since the JFrame that the class extends is empty. Again, your table model is broken and needs to be fixed for your code to work.

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

    Default

    Please let me know if the information posted is understandable or not. I'd be happy to discuss this in greater detail if you have questions or if I"m not clear.

    Much luck!

  5. #25
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    I'm fixing my code, and creating a showGUI function.

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ninjalord918 View Post
    I'm fixing my code, and creating a showGUI function.
    Great, but just remember that main problem and thus the main focus of your fix should be on your model. Much luck!

  7. #27
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    Here is what I'm doing. I created a creatAndShowGUI function, which I want added the table to the right panel. In the main the creatAndShowGUI will be called, but I cannot get the table added to the right panel.

    Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

    XML Code:
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableModel;
    import javax.swing.ImageIcon;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.text.NumberFormat;
    
    public class MoneyNest4You extends JFrame implements TableModelListener{
    	//protected static final String[][] Object = null;
    	private static int lastRow=2;
    	private static int firstRowNum=0;
    	private static int rowCount=2;
    	private static float f=0;
    	//private float total=0;
    	private static int columnNum=1;
    	
    	static DefaultTableModel model = null;
    	
    		String[] columnTitles = {"First Name",
    	            "Last Name",
    	            "Account Type",
    	            "Account Number",
    	            "Transaction Amount"};
    
    	Object[][] data = {
    	{"<html><b><font color=blue>First Name</font></b></html>", "<html><b><font color=blue>Last Name</font></b></html>",
    	"<html><b><font color=blue>Account Type</font></b></html>", "<html><b><font color=blue>Account Number</font></b></html>", "<html><b><font color=blue>Transaction Amount</font></b></html>"},
    	{"","","","", f},
    	{"<html><b><font color=blue>Total</font></b></html>","","","", f}};
    	
    	public MoneyNest4You (){
    		model = new DefaultTableModel(data, columnTitles){
    			 public boolean isCellEditable( int row, int col )  
    			 	{  
    				 	if ( row != firstRowNum && row!= lastRow ){  
    				 		return true;  
    				 	}  
    				 	else {  
    				 		return false;  
    				 	}  
    			 	}  
    		};
    		model.addTableModelListener(null);
    		final JTable table = new JTable(model);
    		table.setPreferredSize(new Dimension(760, 300));
    		//Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
    
            //Add the scroll pane to this panel.
            add(scrollPane);
    
    	}
    	
    	private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("MoneyNest4You");
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(1010, 300);
    		JMenuBar menubar = new JMenuBar();
    		frame.setJMenuBar(menubar);
    		JMenu file = new JMenu("File");
    		menubar.add(file);
    		ImageIcon exitIcon = new ImageIcon("src/exit.png");
    		JMenuItem exit = new JMenuItem("Exit", exitIcon);
    
    		file.add(exit);
    		
    		JMenu help = new JMenu("Help");
    		menubar.add(help);
    		JMenuItem about = new JMenuItem("About");
    		help.add(about);
    		
    		JPanel leftpane = new JPanel();
    		JPanel rightpane = new JPanel();
    		JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftpane, rightpane);
    		frame.add(splitpane);
    		splitpane.setOneTouchExpandable(true);
    		splitpane.setDividerLocation(200);
    		leftpane.setBackground(Color.white);
    		rightpane.setBackground(Color.gray);
    		leftpane.setLayout(new GridLayout(3,1));
    		rightpane.setLayout(new GridLayout(2,1));
    		ImageIcon calIcon = new ImageIcon("src/cal.png");
    		JButton calButton = new JButton("Calculate", calIcon);
    		//calButton.setPreferredSize(new Dimension(105, 50));
    		JButton predictButton = new JButton("Predict");
    		//predictButton.setPreferredSize(new Dimension(105, 50));
    		JButton insertRowButton = new JButton("Insert Row");
    		//insertRowButton.setPreferredSize(new Dimension(105, 50));
    		leftpane.add(calButton);
    		leftpane.add(predictButton);
    		leftpane.add(insertRowButton);
    		
    		MoneyNest4You newContentPane = new MoneyNest4You();
    		newContentPane.setOpaque(true);
    		rightpane.add(newContentPane);	
        }
    
    
    	
    	private void setOpaque(boolean b) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public static void main(String[] args)
    	{
    		createAndShowGUI();
    	}
    
    	@Override
    	public void tableChanged(TableModelEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    }

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    MoneyNest4You subclasses JFrame and you're trying to add a JFrame to a JPanel which simply can't be done. Again, you're concentrating on the wrong thing. Concentrate on the model -- make the model repairs first.

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance, a very simplified version of what I'm talking about:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    @SuppressWarnings("serial")
    public class MN4U2 extends JPanel {
       private DefaultTableModel model;
       
       public MN4U2() {
          String[] columnTitles = {"First Name", "Last Name", 
             "Account Type", "Account Number", "Transaction Amount"};
    
          Object[][] data = {
             {"", "", "", "", new Double(1)}, 
             {"", "", "", "", new Double(3)},
             {"", "", "", "", new Double(6)}, 
             {"", "", "", "", new Double(40)},
             {"Total", "", "", "", new Double(0)}};
    
          model = new DefaultTableModel(data, columnTitles) {
             public boolean isCellEditable(int row, int col) {
                if (row != getRowCount() - 1) {
                   return true;
                } else {
                   return false;
                }
             }
    
             @Override
             public Class<?> getColumnClass(int columnIndex) {
                if (columnIndex == 4) {
                   return Double.class;
                }
                return super.getColumnClass(columnIndex);
             }
             
             @Override
             public void setValueAt(Object value, int row, int column) {
                super.setValueAt(value, row, column);
                if (row != getRowCount() - 1 && column == getColumnCount() - 1) {
                   updateTotal();
                }
             }
          };
          JTable table = new JTable(model);
          updateTotal();
          table.setFillsViewportHeight(true);
          JScrollPane scrollpane = new JScrollPane(table);
          
          Dimension scrollPSz = scrollpane.getPreferredSize();
          int width = scrollPSz.width + 250;
          int height = scrollPSz.height / 2;
          scrollpane.setPreferredSize(new Dimension(width, height));
    
          setLayout(new BorderLayout());
          add(scrollpane, BorderLayout.CENTER);
       }
       
       private void updateTotal() {
          double total = 0.0;
          for (int i = 0; i < model.getRowCount() - 1; i++) {
             Object valueObject = model.getValueAt(i, 4);
             if (valueObject != null && valueObject instanceof Double) {
                double doubleValue = ((Double) valueObject).doubleValue();
                total += doubleValue;
             }
          }
          model.setValueAt(total, model.getRowCount() - 1, 4);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                MN4U2 mn4u = new MN4U2();
                JFrame frame = new JFrame("MoneyNest4You");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(mn4u);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
    
             }
          });
       }
    }

  10. #30
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    Sorry all, I've been trying to take couple steps away from this a little and work on something else. Couldn't wrap my head around this and needed a break.

    I don't understand what "2) Not overriding DefaultTableModel's getColumnClass so that it will return Double.class." does and why do I need this?

  11. #31
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ninjalord918 View Post
    I don't understand what "2) Not overriding DefaultTableModel's getColumnClass so that it will return Double.class." does and why do I need this?
    If you want the data in a column to behave as if it contains double data and not String data, you must rig the table model to allow this to happen, and this is done by creating your own getColumnClass(int column) method that replaces the one originally held by DefaultTableModel. This is nothing more than polymorphism in action, and I have an example of how this is done in my example code above:

    Java Code:
             @Override
             public Class<?> getColumnClass(int columnIndex) {
                if (columnIndex == 4) {
                   return Double.class;
                }
                return super.getColumnClass(columnIndex);
             }


    To be honest, I don't know myself how this works as I have not delved into the source code for the DefaultTableModel and AbstractTableModel classes, but I just do know, after reading the documentation for DefaultTableModel, that it is necessary. This is one reason reading the documentation held in the API and tutorials is essential.

    Best of luck and thanks for getting back to us on this.

  12. #32
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    Is the reason why I had issue with parsing doubles? With this, I don't need Double.parseDouble( String.valueOf( value ) ), right?

  13. #33
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    To be honest, I don't know myself how this works as I have not delved into the source code for the DefaultTableModel and AbstractTableModel classes,
    The code for the TableModel classes won't really help you understand how this method is used. The method is used by the JTable for:

    a) rendering - the method is invoked to determine the appropriate renderer to use. A Double class will use a numeric renderer which will right align data and add thousands separators

    b) editing - a JTextField is used as the default editor. The getText() method always returns a String. The default table cell editor must take the text and create the appropriate Object to store in the model. It uses reflection to create an Object of the type returned by the getColumnClass() method.

    For more details you need to look at the JTable source.

  14. #34
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  15. #35
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    Thanks everyone, my project is running fine. Adding additional features to it.

    Pete, this code is magic :) I don't need to get string value and parse double anymore. Thank you.
    Quote Originally Posted by Fubarable View Post

    Java Code:
             @Override
             public Class<?> getColumnClass(int columnIndex) {
                if (columnIndex == 4) {
                   return Double.class;
                }
                return super.getColumnClass(columnIndex);
             }

  16. #36
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  17. #37
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I don't need to get string value and parse double anymore.
    I gave you an explanation why you didn't need to parse the data and a complete working example four days ago. Glad to know you didn't bother looking at the code, so there is no need for me to reply the next time.

  18. #38
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by camickr View Post
    I gave you an explanation why you didn't need to parse the data and a complete working example four days ago. Glad to know you didn't bother looking at the code, so there is no need for me to reply the next time.
    But you didn't give the OP any boilerplate code that could be copied and pasted without reading or thinking so your reply was supposed to be totally ignored. That explains it ;-)

    kind regards,

    Jos

  19. #39
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    I tried your code, but when I tried to make the changes to my program and still had issue. And could wrap my head what I was doing wrong, so I stepped away from that issue and tried working on something else. Pete, provided his version and it worked.

  20. #40
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I showed you how to:

    a) add data to the TableModel so all the data is of the proper class
    b) write a TableModelListener so you could update the total when a value is changed
    c) get data from the model without parsing and then use the data to calculate a total.

    All you had to do was change the "total calculation" to be a "column total" instead of a "row total" which meant you had to create a loop.

    but when I tried to make the changes to my program
    There was the problem, instead of trying to modify something that worked you used your code which looked nothing like my example. So much for learning by example. Nowhere did you even ask for clairification about how my code worked. So my time was completely wasted.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 07:10 PM
  2. Replies: 1
    Last Post: 07-06-2010, 05:50 PM
  3. Replies: 2
    Last Post: 12-10-2009, 01:06 PM
  4. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 04:10 AM
  5. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 06:33 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
  •