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

    Default

    Are the models or vectors used to initiate either a model or a table shared? If so, you should make a copy of the vector for each table.

    Ah, close. On second look, perhaps it's because each data array is using a reference to the same sInitial array within it. Also for most situations you are better off using a DefaultTableModel rather than an AbstractTableModel.
    Last edited by Fubarable; 09-28-2008 at 03:41 AM.

  2. #22
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Ah, close. On second look, perhaps it's because each data array is using a reference to the same sInitial array within it. Also for most situations you are better off using a DefaultTableModel rather than an AbstractTableModel.
    Nice piece of work Funurable.

    Here the working code.
    Java Code:
    //Hardwired's  modified code modified
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    import javax.swing.JTable;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    import java.util.Vector;
    
    public class ScrollTestRx2 extends JFrame implements ActionListener{
        JPanel jplShow;
        JScrollPane jspShow;
       // String []sInitial={" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "};  
        int count;    
         Vector<JTable> jtable;
         /*got the 2d vector idea from http://forums.sun.com/thread.jspa?threadID=570939&messageID=2826384                   
          Vector <Vector <String>> MyVector;   
          MyVector= new Vector <Vector <String>>();*/
        Vector <Vector<String>> sInitial;
        
        ScrollTestRx2(){
            sInitial=new Vector<Vector <String>>();
            count=0;
            jtable=new Vector<JTable>(1);
            JButton jbnadd=new JButton("Add");
            
            
            jbnadd.addActionListener(this);
            System.out.println("JFrame default layout = " +
                                getLayout().getClass().getSimpleName());
            // Let the AWT do the work.
            JPanel northPanel = new JPanel();
            
            System.out.println("JPanel default layout = " +
                                northPanel.getLayout().getClass().getSimpleName());
            northPanel.add(jbnadd);
            getContentPane().add(northPanel, "North");
    
            jplShow=new JPanel();
            jplShow.setLayout(null);
            jplShow.setPreferredSize(new Dimension(670,520));
            jspShow=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jspShow.setViewportView(jplShow);
            jspShow.setPreferredSize(new Dimension(670,520));
            getContentPane().add(jspShow);  // default center section
    //        setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(800,700));
            pack();
            // Always call this last.
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().equals("Add")){
                // Your layout management section.
                
                int compCount = jplShow.getComponentCount();
                int panelHeight = 120;
                int margin = 20;
                int yCoordinate= compCount*panelHeight + margin;
                JPanel jplDisplay=new JPanel(new BorderLayout());
               // String s = "<html>panel " + (jplShow.getComponentCount()+1) +
               //            "<br>yCoordinate = " + yCoordinate;
              //  jplDisplay.add(new JLabel(s, JLabel.CENTER));
                
                //ADDING A TABLE
                        final String[] names = {"beat1"," "," "," ","Beat 2"," "," "," ",
                                "Beat3"," "," "," ","Beat4"," "," "," "};
                        
                        
                        
                        Vector <String> stemp= new Vector<String>();
                        for(int i=0;i<16;i++){stemp.add(i, " ");}
                        
                        //String[] stemp={" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "}; 
                        sInitial.add(count,stemp);
                        System.out.println(sInitial.elementAt(count));
                        final Object[][] data = {  stemp.toArray()    };
                        TableModel AbModel = new AbstractTableModel() {
                        public int getColumnCount() { return names.length; }
                        public int getRowCount() { return data.length;}
                        public Object getValueAt(int row, int col) { return data[row][col]; }
                        public String getColumnName(int column) {return names[column];}
                 //       public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
                        public void setValueAt(Object aValue, int row, int column) {data[row][column] = aValue;}
                        };   
                        
                        jtable.add(count, new JTable(AbModel));
                        String temp=String.valueOf(count);
                        jtable.elementAt(count).setName(temp);
                        System.out.println("Name:"+jtable.elementAt(count).getName());                   
                        jtable.elementAt(count).addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                if (e.getClickCount() ==2){    
                    System.out.println("ML=" + this + ", ME=" + e);
                    String stemp=e.getComponent().getName();//gettting name of element
                    int itemp=Integer.parseInt(stemp);
                    JTable jtTemp =(JTable)e.getSource();
                    System.out.println(" double clicked Table "+e.getComponent().getName());
                    int selcol=jtable.get(itemp).getSelectedColumn();
                    int selrow=jtable.get(itemp).getSelectedRow();                 
                    System.out.println("Column:"+(selcol+1)+" has been selected");
                    String newval=null;
                    Object oldval=jtable.get(itemp).getValueAt(selrow, selcol);
                    if(oldval==" "){ newval="|S|";}//S=Slow hit H=Hard Hit 
                    else if(oldval=="|S|"){ newval="|H|";}
                    else if(oldval=="|H|"){ newval=" ";}
                   // sInitial[selcol]=newval;
                    sInitial.elementAt(itemp).add(selcol, newval);/////
                    jtable.get(itemp).setValueAt(newval, selrow, selcol);       
                    System.out.println(sInitial.elementAt(itemp));
                    jtable.get(itemp).repaint();
                    
                    }           
                }
                } );  
                      
                        JScrollPane jspTable=new JScrollPane(jtable.elementAt(count));
                        jspTable.setPreferredSize(new Dimension(520, 50));
                        
                        
                        jplDisplay.add(jspTable);
                        
                jplDisplay.setBounds(10, yCoordinate, 600, 100);
                jplDisplay.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                jplShow.add(jplDisplay);
                System.out.println(yCoordinate);
                int maxY = yCoordinate + panelHeight;
                jplShow.setPreferredSize(new Dimension(670,maxY));
                jplShow.revalidate();
                count++;
            }
        }
    
        public static void main(String[] args){
            new ScrollTestRx2();
        }
    }
    Thanks guys for all the help.

    PS. // public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
    can't get this part. Also any other suggestions will bee appreciated.
    Last edited by goodwillwins; 09-28-2008 at 06:05 AM.

  3. #23
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    The above code works fine, except that when i select a Jtable cell the entire row get highlighted. What changes do i make so that just the cell clicked on gets highlighted.

    Also when i choose another jtable the previous one remains highlighted.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. JScrollPane And ArrowKeys
    By arpitvavadia in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 09:22 PM
  2. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 04:58 AM
  3. JScrollPane with HTML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:32 PM
  4. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 06:23 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 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
  •