Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with get input from JTextFiled and get in in JTable.Please help.

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Problem with get input from JTextFiled and get in in JTable.Please help.

    Hi,i need to get users input from jtextfield and set it to JTable.This is my code:

    public class Projektni extends JFrame {
        public final JTextField ime = new JTextField(10);
        public final JTextField prezime = new JTextField(10);
        public final JTextField index = new JTextField(10);
        public DefaultListModel podaci = new DefaultListModel();
        JButton imeB=new JButton("Upisi u tabelu");
        public JList lista = new JList(podaci);
        String kolone[] = {"ID","Name","Age"};
    //prazan konstruktor gde implementiramo i definisemo dugmice,polja za upisivanje podataka i osluskivace
        public Projektni()
        {
     
    DefaultTableModel model = new DefaultTableModel(); 
    JTable table = new JTable(model); 
    String nextRowId = Integer.toString(model.getRowCount());
    model.addColumn("Ime"); 
    model.addColumn("Prezime"); 
    model.addColumn("Indeks");
    model.addRow((new Object[] { 
                  nextRowId,
                  ime.getText(),
                  prezime.getText(),
                  index.getText()}
     
     
        ));
     
    repaint();
     
            JPanel pane2=new JPanel (new GridLayout(6, 7));   
              pane2.add(table);
             pane2.setVisible(true);
     
     
            ime.setPreferredSize(new Dimension(200,80));
            prezime.setPreferredSize(new Dimension(200,80));
            index.setPreferredSize(new Dimension(150,40));
     
     
     
     
            imeB.addActionListener(new ActionListener(){
     
     
                @Override
                public void actionPerformed(ActionEvent e){
                 String pokupiIme=ime.getText();
                 String pokupiPrezime=prezime.getText();
                 String pokupiIndex=index.getText();
     
     
                }
            }
            );
     
     
     
     
     
     
     JPanel panel=new JPanel (new GridLayout(6, 7));
    panel.add(new JLabel("Ime studenta:"));
    panel.add(ime);
    panel.add(new JLabel("Prezime studenta:"));
    panel.add(prezime);
    panel.add(new JLabel("Broj indeksa studenta:"));
    panel.add(index);
    panel.setBackground(Color.cyan);
    panel.add(imeB);
    String[] seminari = {"Microsoft","MAC","Java"};
    final JComboBox cek = new JComboBox(seminari);
    panel.add(cek);
     
    JButton upisibtn=new JButton("Upisi");
    upisibtn.setPreferredSize(new Dimension(30,30));
    JTabbedPane tab = new JTabbedPane();
    getContentPane().add(tab);
    tab.add("Tab1",panel);
    tab.add("Tab2",pane2);
     
    upisibtn.addActionListener(new ActionListener()
    {
     
        @Override
     
        public void actionPerformed(ActionEvent e)
        {
            try{
                String p=nazivZaUnosNaziva()+" "+nazivZaUnosPrezimena()+" "+nemaIndeX()+" "+cek.getSelectedItem()+"\n";
             String s=ime.getText()+" "+prezime.getText()+" "+index.getText()+" "+cek.getSelectedItem()+"\n";
     
            Upisi.upisi(s);
            }
            catch(NemaNaziv|NemaIndeks|NemaPrezime ex){
                JOptionPane.showMessageDialog(null, ex);
     
            }
     
     
     
     
     
               }
    }
    );
     
    //Dugme prikazi ,koji prikazuje upisane podatke preko klase citaj.
    JButton citajbtn=new JButton("Prikazi");
    citajbtn.setPreferredSize(new Dimension(30,30));
    citajbtn.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
       for(String s:Citaj.citaj())
       podaci.addElement(s);
     
      }
    });
     
    //Dodavanje komponenata na panel.
    panel.add(upisibtn);
    panel.add(citajbtn);
     
    panel.add(lista);
        }
     
      //Vraca string naziva studenta,ukoliko nije upisan vraca izuzetak
        public String nazivZaUnosNaziva() throws NemaNaziv {
            String naziv = ime.getText();
            if (naziv.equals("")) {
                throw new NemaNaziv("Niste uneli naziv studenta. ");
            }
            return naziv;
        }
        //Vraca string prezime studenta,ukoliko nije upisan vraca izuzetak
        public String nazivZaUnosPrezimena() throws NemaPrezime {
            String prez = prezime.getText();
            if (prez.equals("")) {
                throw new NemaPrezime("Niste uneli prezime studenta. ");
            }
            return prez;
        }
        //Vraca string index studenta,ukoliko nije upisan vraca izuzetak
         public String nemaIndeX() throws NemaIndeks {
            String indeks = index.getText();
            if (indeks.equals("")) {
                throw new NemaIndeks("Niste unelii ndeks studenta. ");
            }
            return indeks;
        }
     
     
     
     
     
     
    //pokretacki metod
        public static void main(String[] args) {
     
            Projektni tp = new Projektni();
            tp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            tp.setVisible(true);
            tp.setSize(600, 800);
     
        }
     
     
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Problem with get input from JTextFiled and get in in JTable.Please help.

    What's the problem?

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with get input from JTextFiled and get in in JTable.Please help.

    I don't know how to input users in text filed show in JTable.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Problem with get input from JTextFiled and get in in JTable.Please help.

    Look at the JTextField method getText() and then review the JTable tutorial(s). Sowing specific questions may reap more specific answers.

Similar Threads

  1. Output in JTextFiled with refreshing every row
    By kenhksit in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 8th, 2014, 05:19 AM
  2. Replies: 1
    Last Post: February 5th, 2014, 08:22 AM
  3. Re: setVariable to Swing JTextfiled?
    By aknessy in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2011, 10:42 AM
  4. setVariable to Swing JTextfiled?
    By Frankly3D in forum AWT / Java Swing
    Replies: 1
    Last Post: April 6th, 2011, 08:52 AM
  5. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM

Tags for this Thread