Results 1 to 14 of 14
  1. #1
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default need help to sort a list

    need help to sort a list. i used defaulttablemodel, and i need to create a button to sort all computers but i don't know how to do that.
    i tried to insert this:
    Java Code:
           if(mygtukasX==bRikiavimas)
           {
               atranka = ka.visiPC.sort(Kompiuteris.pagalKaina);// Kompiuteris.pagalKaina
               ClearTable();
               printOutTable(atranka);
               return;
           }
    but it's not working.
    in this code i have to insert that sort. :
    Java Code:
    package Lab2Kazlauskaitė;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import studijosKTU.*;
    import java.io.*;
    import javax.swing.table.DefaultTableModel;
    //-------------------------------------------------
    public class Meniu extends JFrame implements ActionListener {
        
        static PCApskaita ka = new PCApskaita();
        ListKTUx<Kompiuteris> atranka = new ListKTUx<Kompiuteris>(new Kompiuteris());
        //ListKTU<Kompiuteris> rikiuoti = new ListKTU<Kompiuteris>();
        public Meniu()
        {
            Langas();
        }
        JButton bSkaitytiFaila = new JButton("Skaityti faila");
        JButton bPapildytiSar = new JButton ("Papildyti sarasa");
        JButton bAtrinktiPagalGamintoja = new JButton("Atr. pagal gamintoja");
        JButton bAtrinktiPagalKaina = new JButton("Atr. pagal kaina");
        JButton bAtrinktiPagalTalpa = new JButton("Atr. pagal talpa");
        JButton bBrangiausias = new JButton("Brangiausias");
        JButton bRikiavimas = new JButton("Rikiavimas");
        private DefaultTableModel model = new DefaultTableModel();
        private JTable lentele = new JTable(model);
        //private JTable lentele;
        private JScrollPane juosta;
    //-------------------------------------------------
        public void Langas() {
            bSkaitytiFaila.addActionListener(this);
            bPapildytiSar.addActionListener(this);
            bAtrinktiPagalGamintoja.addActionListener(this);
            bAtrinktiPagalKaina.addActionListener(this);
            bAtrinktiPagalTalpa.addActionListener(this);
            bBrangiausias.addActionListener(this);
            bRikiavimas.addActionListener(this);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(900, 400);
            išdėstymas();
            setVisible(true);        
        }
    //-------------------------------------------------
        public void išdėstymas() {
            setLocation(400, 300);
            Container vidus = getContentPane();
            vidus.setBackground(Color.LIGHT_GRAY);
            vidus.setLayout(new BorderLayout());
            //mygtuku juosta
            Panel v = new Panel();
            v.setLayout(new FlowLayout());
            vidus.add(v, BorderLayout.NORTH);
    
            v.add(bSkaitytiFaila);
            v.add(bPapildytiSar);
            v.add(bAtrinktiPagalGamintoja);
            v.add(bAtrinktiPagalKaina);
            v.add(bAtrinktiPagalTalpa);
            v.add(bBrangiausias);
            v.add(bRikiavimas);
            //lenteles panele
            Panel p = new Panel();
            p.setLayout(new BorderLayout());
            vidus.add(p, BorderLayout.CENTER);
                    
    	juosta = new JScrollPane( lentele );
    	p.add( juosta );
            model.addColumn("Gamintojas");
            model.addColumn("Talpa");
            model.addColumn("V.Plokste");
            model.addColumn("RAM");
            model.addColumn("Kaina");
    }
    //-------------------------------------------------
        private void ClearTable()
        {
        while (model.getRowCount()>0)
                {
                    model.removeRow(0);
                }
        }
    //-------------------------------------------------
        public void printOutTable(ListKTUx<Kompiuteris> sar)
        {       
            Object duomenys[] = new Object [5];        
            for(Kompiuteris a : sar)
            {
                duomenys[0] = a.getGamintojas();
                duomenys[1] = a.getTalpa();
                duomenys[2] = a.getvPlokste();
                duomenys[3] = a.getaRAM();
                duomenys[4] = a.getKaina();
                
                model.addRow(duomenys);
            }        
        }
    //------------------------------------------------- 
        public void actionPerformed(ActionEvent event) {
           Object mygtukasX = event.getSource();
           if(mygtukasX==bSkaitytiFaila)
           {
                File f1 = new File(".", "ban.txt");
                JFileChooser fc = new JFileChooser(".");
                int rez = fc.showOpenDialog(null);
                if (rez == JFileChooser.APPROVE_OPTION)
                {		  
    		  f1 = fc.getSelectedFile();
                      Ks.setDataFolder(fc.getCurrentDirectory().toString());		  
                } 
                else if (rez == JFileChooser.CANCEL_OPTION)
                {
    		  
                }          
                ka.visiPC.load(f1.getName());
                ClearTable();
                printOutTable(ka.visiPC);
                return;
           }
           if(mygtukasX==bPapildytiSar)
           {
               String pc = JOptionPane.showInputDialog("Iveskite kompiuterio duomenis");
                   Kompiuteris a=new Kompiuteris(pc);
                   String klaidosPožymis=a.validate();
                   if (klaidosPožymis.length()==0)
                   {
                       ka.visiPC.add(a);
                       ka.visiPC.save("rez.txt");
                       ClearTable();
                       printOutTable(ka.visiPC);
                   }
                   else
                     JOptionPane.showMessageDialog (this,"Irasas nepriimtas");
              return;
           }
           if(mygtukasX==bAtrinktiPagalGamintoja)
           {
               String inputValue = JOptionPane.showInputDialog("Iveskite gamintoja");
               try{
                   atranka = ka.atrinktiGamintoja(String.valueOf(inputValue));
               }
               catch (NumberFormatException exc)
               {
                   JOptionPane.showMessageDialog (this,"blogai ivestas pavadinimas");
                   return;
               }
               ClearTable();
               printOutTable(atranka);
           }
           if(mygtukasX==bAtrinktiPagalKaina)
           {
               try
               {
               String inputValue = JOptionPane.showInputDialog("Iveskite pradine kaina");
               atranka = ka.atrinktiPagalKaina(Double.parseDouble(inputValue));
               }
               catch (NumberFormatException exc)
               {
                   JOptionPane.showMessageDialog (this,"Blogai ivestas skaicius");
                   return;
               }
                ClearTable();
                printOutTable(atranka);
                return;
           }
           if(mygtukasX==bAtrinktiPagalTalpa)
           {
               try
               {
                   String inputValue = JOptionPane.showInputDialog("Iveskite talpa");
                   atranka = ka.atrinktiPagalTalpa(Double.parseDouble(inputValue));
               }
               catch (NumberFormatException exc)
               {
                   JOptionPane.showMessageDialog(this,"Blogai ivestas skaicius");
                   return;
               }
           }
           if(mygtukasX==bBrangiausias)
           {
                atranka = ka.maksimaliosKainosPC();
                ClearTable();
                printOutTable(atranka);
                return;       
           }
    
        }
        
    }
    Last edited by gretaks; 11-01-2011 at 07:26 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: need help to sort a list

    Did you read the API documentation for the Collections class? It has a method that can sort a List.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    well, kind of.. can u write a code how to do that?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: need help to sort a list

    Quote Originally Posted by gretaks View Post
    well, kind of.. can u write a code how to do that?
    Yes, I can, but you go first because it's your assignment. Feel free to come back here again if you're stuck; tip: also read the API documentation for the Comparable<T> interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    haha i tried.. but's it's not working for me.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: need help to sort a list

    Quote Originally Posted by gretaks View Post
    haha i tried.. but's it's not working for me.
    A real programmer tries again; s/he doesn't let the incorrect program win.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    i'm not a programmer, in life i won't need it this thing..i'm looking forward to create films and so on.. ;D and i tried about 5 times..

  8. #8
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    ok, i need to sort a list of computers..
    i made buttons to find a biggest price computer, biggest HDD and so on. using methods

    when i tried to use method sort() it''s not working. some error using like this

    if(mygtukasX==bRikiavimas) // mygtukasX=buttonX and bRikiavimas=bSort (names)
    {
    atranka = ka.visiPC.sort(Kompiuteris.pagalKaina);// visiPC it's where my computer list are kind of.. i use .sort(Kompiuteris.pagalKaina) so it''s using Kompiuteris=Computer(info).pagalKaina(by price) to sort it
    ClearTable();
    printOutTable(atranka);
    return;
    }

    should i write something different?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: need help to sort a list

    when i tried to use method sort() it''s not working. some error
    You need to show the error messages you are getting.

  10. #10
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    at this line:
    atranka = ka.visiPC.sort(Kompiuteris.pagalKaina);
    it shows this error:
    incompatible types
    required studijosKTU.ListKtux<Lab2Kazlauskaite.Kompiuteris>
    found: void

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: need help to sort a list

    You need to show the definitions for Kompiuteris, pagalKaina, VisiPC. ListKtux and studijosKTU.
    Without the definitions for ALL of the variables shown in the error message, there is no way to make any suggestions.

  12. #12
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    now i know how to do it.. i cane create a table by myself and create comperators to each column, o it have to work with setRowSorter on one table it's work and on mine it's not .. lecturer haven't find error so he said to do different or leave it how i tried to do and he gave me 0.5 ;) so i have 9.5/10 it's ok :/ :)

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: need help to sort a list

    Is your problem solved now?

  14. #14
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: need help to sort a list

    em.. kind of...i can write a program a bit different and it will be wokring..but no need anymore..it's enough to know how to do that. :)

Similar Threads

  1. bucket sort, list in java
    By someone in forum New To Java
    Replies: 10
    Last Post: 04-11-2011, 11:13 PM
  2. Insertion Sort for linked list help?
    By bubtub24 in forum New To Java
    Replies: 3
    Last Post: 11-28-2010, 06:21 AM
  3. SORT list on items where frequencies are involved
    By emgee in forum Java Applets
    Replies: 1
    Last Post: 11-14-2010, 02:42 PM
  4. [SOLVED] Insertion Sort in Linked List
    By taylorp in forum New To Java
    Replies: 10
    Last Post: 03-27-2009, 12:34 AM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM

Tags for this Thread

Posting Permissions

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