Results 1 to 8 of 8
  1. #1
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default programming structure in java

    Had been programming with C,C++ before and moved to java recently

    have difficulty in understanding the structure i.e

    in C there are Functions

    in C++ there are classes and methods

    in java also there are classes and methods but i find that there are nested methods also. please refer me to a proper documentation which explains the different programming structures

    regards

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What "nested methods"?

    You can have inner classes (including anonymous classes), and those classes can have methods, maybe that's what you're thinking. But that term says to me methods containing method definitions, and that you don't have (without an anonymous class or something).

    Edit: Or maybe you're tlaking about method overloading (which I doubt), or maybe method overriding (which might fit the term "nested methods"), or, in that same vein, maybe even method hiding. But I don't believe any of these concepts are not included in C++ (at least).

  3. #3
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default programming structure in java

    In the code below

    1) Following code i do not understand , could you please explain this. inside method addListSelectionListener is passed a parameter new ListSelectionListener and this goes on . Is there a similar structure in C++ ?

    Java Code:
    //When selection changes, provide user with row numbers for
            //both view and model.
            table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent event) {
                            int viewRow = table.getSelectedRow();
                            if (viewRow < 0) {
                                //Selection got filtered away.
                                statusText.setText("");
                            } else {
                                int modelRow = 
                                    table.convertRowIndexToModel(viewRow);
                                statusText.setText(
                                    String.format("Selected Row in view: %d. " +
                                        "Selected Row in model: %d.", 
                                        viewRow, modelRow));
                            }
                        }
                    }
            );
    2. The following code the method
    table.getSelectionModel().addListSelectionListener sits inside the constructor
    This structure is also confusing for me.

    Java Code:
    public TableFilterDemo() {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    
            //Create a table with a sorter.
            MyTableModel model = new MyTableModel();
            sorter = new TableRowSorter<MyTableModel>(model);
            table = new JTable(model);
            table.setRowSorter(sorter);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
    
            //For the purposes of this example, better to have a single
            //selection.
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
            //When selection changes, provide user with row numbers for
            //both view and model.
            table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent event) {
                            int viewRow = table.getSelectedRow();
                            if (viewRow < 0) {
                                //Selection got filtered away.
                                statusText.setText("");
                            } else {
                                int modelRow = 
                                    table.convertRowIndexToModel(viewRow);
                                statusText.setText(
                                    String.format("Selected Row in view: %d. " +
                                        "Selected Row in model: %d.", 
                                        viewRow, modelRow));
                            }
                        }
                    }
            );

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by kmm1977 View Post
    In the code below

    1) Following code i do not understand , could you please explain this. inside method addListSelectionListener is passed a parameter new ListSelectionListener and this goes on . Is there a similar structure in C++ ?

    Java Code:
    //When selection changes, provide user with row numbers for
            //both view and model.
            table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent event) {
                            int viewRow = table.getSelectedRow();
                            if (viewRow < 0) {
                                //Selection got filtered away.
                                statusText.setText("");
                            } else {
                                int modelRow = 
                                    table.convertRowIndexToModel(viewRow);
                                statusText.setText(
                                    String.format("Selected Row in view: %d. " +
                                        "Selected Row in model: %d.", 
                                        viewRow, modelRow));
                            }
                        }
                    }
            );
    That is an anonymous class, see that "new ListSelectionListener()", it is defining an anonymous class that implements the interface ListSelectionListener.
    2. The following code the method
    table.getSelectionModel().addListSelectionListener sits inside the constructor
    This structure is also confusing for me.

    Java Code:
    public TableFilterDemo() {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    
            //Create a table with a sorter.
            MyTableModel model = new MyTableModel();
            sorter = new TableRowSorter<MyTableModel>(model);
            table = new JTable(model);
            table.setRowSorter(sorter);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
    
            //For the purposes of this example, better to have a single
            //selection.
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
            //When selection changes, provide user with row numbers for
            //both view and model.
            table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent event) {
                            int viewRow = table.getSelectedRow();
                            if (viewRow < 0) {
                                //Selection got filtered away.
                                statusText.setText("");
                            } else {
                                int modelRow = 
                                    table.convertRowIndexToModel(viewRow);
                                statusText.setText(
                                    String.format("Selected Row in view: %d. " +
                                        "Selected Row in model: %d.", 
                                        viewRow, modelRow));
                            }
                        }
                    }
            );
    Same thing.

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Hi. It is code uses anonymously class. for example
    Java Code:
    public class ExAnonymously {
        private interface OneArg {
            public void invoke(int var);
        }
    
        private static void loop(OneArg block) {
            for (int i=0;i!=10;++i) {
                block.invoke(i);
            }
        }
    
        public static void main(String[] args) {
            loop(
                new OneArg() {
                    public void invoke(int var) {
                        System.out.print(var + " ");
                    }
                }
            );
        }
    }
    i.e. I not create new instance for interface OneArg instead, I use anonymous classes
    You need read this topic about anonymously class Anonymous Classes (Java in a Nutshell)
    Last edited by Petr; 01-19-2011 at 02:42 PM.
    Skype: petrarsentev
    http://TrackStudio.com

  6. #6
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default programming structure in java

    Thanks for that i m reading and trying to understand it

    in the following code line

    myTable = new JTable(DTM) {...

    is this also anonymous class ?
    Java Code:
    
    public ReportTable(ReportFilterSearcher pFilterSearcher) {
    DTM = new DefaultTableModel();
    fastTable = new Hashtable();
    myFilterSearcher = pFilterSearcher;
    myCursor = null;
    workingOnSMT = false;
    hlclIcon = new ImageIcon(ClassLoader.getSystemResource("icons/keyboard.png"));
    for (String name : columnNames) DTM.addColumn(name);
    myTable = new JTable(DTM) {
    
    private static final long serialVersionUID = 2893179648951894907L;
    
    public TableCellRenderer getCellRenderer(int row, int column) {
    	TableColumn tableColumn = getColumnModel().getColumn(column);
    				TableCellRenderer renderer = tableColumn.getCellRenderer();
    if (renderer == null) {
    	Class<?> c = getColumnClass(column);
    	if( c.equals(Object.class) ){
    		if (row < getRowCount() & column <getColumnCount()) {
    			Object o = getValueAt(row,column);
    			if( o != null )	c = getValueAt(row,column).getClass();
    						}
    					}
    			renderer = getDefaultRenderer(c);
    				}
    				return renderer;
    			}
    ...............

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes. Exactly why anyone would extend JTable in that manner, however, I don't know. JTable is a concrete class (it is not an interface and not abstract) and so any extension of it should also be a concrete class. The only thing I can think of is the person who wrote that was simply attempting to be "clever", and that will always come back to bite you.

  8. #8
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default programming structure in java

    i am in a strange situation
    having been assigned a task to modify and add small functionalities to a java application ( code which you saw is not written clean )

    myself comming from C++ background , have to get grip on Java programming structures. so this project is a learning ground for me

Similar Threads

  1. java structure
    By PBHATT in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 08:09 PM
  2. Tree structure using JAVA
    By trill in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 11:02 AM
  3. Passing a structure from Java to C using JNI
    By vipin27 in forum New To Java
    Replies: 0
    Last Post: 02-23-2010, 06:22 AM
  4. Stack data structure in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:34 PM
  5. Java Array of Structure
    By PAffiliates in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 06:08 AM

Posting Permissions

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