Results 1 to 5 of 5

Thread: action listener

  1. #1
    skuskusas is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Question action listener

    Hello i need som help for mouse clicker action.

    action listener-qqq.png
    here me working program. problem is that if i want import data from list me need push the button. I want change data when i selected patch.

    and second me problem is: i want change me DB layout. i want all data from column import in 1 row. (i want turn around me JTable)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: action listener

    Please post the code you are working on and ask specific questions about how to solve your problems.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: action listener

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    skuskusas is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Question Re: action listener

    //me code i want make buttons[2] action without press button. i want when select patch i get it in console then i can self finish me code.
    here is somthing like that but is it in Jtable and dont work for me :(
    how to get the values from selected row of JTable (Swing / AWT / SWT forum at JavaRanch)




    Problem 2 is: is want that all data from weekNo (look picture from db) post not in column but in first row.

    thx for help.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    public class nn extends JFrame {
    
        private DefaultListModel model;
        private JButton buttons[];
        private JList dbList;
        private JPanel p2, p1, navigationPanel;
        private String bLabel[] = {"Įkelti", "Ištrinti pažymėtą", "Įkelti pažymeta", "Išvalyti DB","listen"};
        Connection con;
        Statement st;
        ResultSet rs;
        String db;
        String fileName;
        PreparedStatement ps;
        String a, aa;
        int s, ss, sss, ssss, sssss;
        int rows = 0;
        Object data1[][] = null;
        Object Colheads[][];
        JScrollPane scroller;
        JTable table;
        JScrollPane tablePanel;
        private JList listbox;
        Object mlm;
        
        public nn() {
    
            super("Prekes");
            setSize(760, 700);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            model = new DefaultListModel();
            buttons = new JButton[5];
            dbList = new JList(model);
    
            try {
                Class.forName("org.apache.derby.jdbc.ClientDriver");
                con = DriverManager.getConnection("jdbc:derby://localhost:1527/mydatabase", "app", "app");
            } catch (Exception e) {
            }
            try {
                st = con.createStatement();
            } catch (SQLException sqlex) {
                JOptionPane.showMessageDialog(null, "Failed to Connect to Database", "Error Connection", JOptionPane.WARNING_MESSAGE);
                dispose();
            }
    
            p2 = new JPanel();
    
            try {
                rs = st.executeQuery("SELECT * from item");
            } catch (SQLException ex) {
                Logger.getLogger(nn.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                while (rs.next()) {
                    s = rs.getInt(1);
                    a = rs.getString(2);
                    ss = rs.getInt(3);
                    aa = rs.getString(4);
                    sss = rs.getInt(5);
                    ssss = rs.getInt(6);
                    rows++;
    
                }
                data1 = new Object[rows][6];
    
                Object[] Colheads = {"Id", "Name", "NavNo", "NOO", "WeekNO", "ItemNO"};
                rs = st.executeQuery("Select * from item");
    
                for (int i1 = 0; i1 < rows; i1++) {
                    rs.next();
                    for (int j1 = 0; j1 < 6; j1++) {
                        data1[i1][j1] = rs.getString(j1 + 1);
                    }
                }
    
                table = new JTable(data1, Colheads);
                int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
                int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
                table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    
                tablePanel = new JScrollPane(table, v, h);
    
            } catch (Exception e) {
            }
    
            navigationPanel = new JPanel();
            navigationPanel.setLayout(new BorderLayout());
    
            final DefaultListModel listData = new DefaultListModel();
    
            listData.clear();
            try {
                rs = st.executeQuery("select * from items");
            } catch (SQLException ex) {
                Logger.getLogger(nn.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                while (rs.next()) {
                    listData.addElement(rs.getString("Id") + "-" + rs.getString("Name"));
    
                }
            } catch (SQLException ex) {
                Logger.getLogger(nn.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            listbox = new JList(listData);
            listbox.setVisibleRowCount(5);
            listbox.setFixedCellHeight(30);
            listbox.setFixedCellWidth(300);
            
            JScrollPane lentele = new JScrollPane(listbox);
            navigationPanel.add(lentele, BorderLayout.CENTER);
            
            p1 = new JPanel();
            navigationPanel.add(p1, BorderLayout.PAGE_START);
            for (int count = 0; count < buttons.length; count++) {
                buttons[count] = new JButton(bLabel[count]);
                p1.add(buttons[count]);
    
                p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
    
            }
            Container pane = getContentPane();
            setContentPane(pane);
    
            buttons[0].addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
    
                            SessionFactory sessions = new Configuration().configure().buildSessionFactory();
                            Session session = sessions.openSession();
                            Transaction tx = null;
                            tx = session.beginTransaction();
                            try {
                                JFileChooser chooser = new JFileChooser();
                                int returnVal = chooser.showOpenDialog(null);
                                if (returnVal == JFileChooser.APPROVE_OPTION) {
                                    File f = chooser.getSelectedFile();
                                    File ff = f.getCanonicalFile();
                                    fileName = ff.getCanonicalPath();
                                    Main app = new Main();
                                    app.readDataToExcelFile(fileName);
    
                                    rs = st.executeQuery("Select * from item");
    
                                    for (int i1 = 0; i1 < rows; i1++) {
                                        rs.next();
                                        for (int j1 = 0; j1 < 6; j1++) {
    
                                            data1[i1][j1] = rs.getString(j1 + 1);
                                            mlm = rs.getString(j1 + 1);
                                        }
                                    }
                                }
                                Kelias s3 = new Kelias();
                                s3.setName(fileName);
                                session.save(s3);
                                Kelias Item3 = (Kelias) session.get(Kelias.class, s3.getId());
                                tx.commit();
                                session.close();
                                tx = null;
    
                                listData.clear();
                                rs = st.executeQuery("select * from items");
                                while (rs.next()) {
                                    listData.addElement(rs.getString("Id") + "-" + rs.getString("Name"));
                                }
    
                                tablePanel.revalidate();
                                tablePanel.repaint();
    
                            } catch (Exception e) {
                            }
                        }
                    });
    
            buttons[1].addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                            try {
                                String labas = (listbox.getSelectedValue().toString());
                                String str = labas;
                                String[] temp;
                                String delimiter = "-";
                                temp = str.split(delimiter);
                                for (int i = 0; i < 1; i++) {
                                    String ID = (temp[0]);
                                    String aString = ID;
                                    int aInt = Integer.parseInt(aString);
    
                                    HibernateUtil.droptable("delete from items where ID = " + aInt + "");
                                    System.out.println("Deleted name: " + aInt);
    
                                    listData.clear();
                                    rs = st.executeQuery("select * from items");
                                    while (rs.next()) {
                                        listData.addElement(rs.getString("Id") + "-" + rs.getString("Name"));
                                    }
                                }
                            } catch (Exception e) {
                            }
                        }
                    });
            buttons[2].addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
    
                            String labas = (listbox.getSelectedValue().toString());
                            String str = labas;
                            String[] temp;
                            String delimiter = "-";
                            temp = str.split(delimiter);
                            for (int i = 0; i < 1; i++) {
                                String ID = (temp[1]);
                                System.out.println(temp[1]);
    
                                try {
                                    Main app = new Main();
                                    app.readDataToExcelFile(ID);
    
                                    rs = st.executeQuery("Select * from item");
    
                                    for (int i1 = 0; i1 < rows; i1++) {
                                        rs.next();
                                        for (int j1 = 0; j1 < 6; j1++) {
    
                                            data1[i1][j1] = rs.getString(j1 + 1);
                                            mlm = rs.getString(j1 + 1);
                                        }
                                        tablePanel.revalidate();
                                        tablePanel.repaint();
                                    }
                                } catch (Exception e) {
                                    System.out.println("nepavyko parodyti duomenu(0)");
                                }
                            }
                        }
                    });
    
            buttons[3].addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                            SessionFactory sessions = new Configuration().configure().buildSessionFactory();
                            Session session = sessions.openSession();
    
                            Transaction tx = null;
                            try {
                                tx = session.beginTransaction();
    
                                HibernateUtil.droptable("drop table item");
                                HibernateUtil.droptable("drop table items");
    
                                HibernateUtil.setup("create table item ( id int,Name VARCHAR(6),ItemNo int,Nav VARCHAR(6), WeekNo int, Quantity int)");
                                HibernateUtil.setup("create table items ( id int,Name VARCHAR(45))");
                                listData.clear();
                                JFileChooser chooser = new JFileChooser();
                                int returnVal = chooser.showOpenDialog(null);
                                if (returnVal == JFileChooser.APPROVE_OPTION) {
                                    File f = chooser.getSelectedFile();
                                    File ff = f.getCanonicalFile();
                                    fileName = ff.getCanonicalPath();
                                    Main app = new Main();
                                    app.readDataToExcelFile(fileName);
                                }
                                System.out.println("duomenys is DB istrinti");
                            } catch (Exception e) {
                                System.out.println("DB yra tuscia");
                            }
                            tablePanel.revalidate();
                            tablePanel.repaint();
                        }
                    });
            
                   listbox.addMouseListener(new MouseAdapter() {              
        public void mouseClicked(MouseEvent e) {
         if (e.getClickCount() == 2) {
          
           
                 }
                 }
                });
            
           
    
            pane.add(navigationPanel, BorderLayout.LINE_START);
            pane.add(tablePanel, BorderLayout.CENTER);
            
    
            setVisible(true);
            setResizable(true);
        }
    
        public static void main(String[] args) {
            nn rdjl = new nn();
        }
    }
    Last edited by skuskusas; 09-04-2012 at 08:55 PM. Reason: add tags

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: action listener

    Please edit the post and wrap the code in code tags. See:
    BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Can anyone help me with an action listener?
    By mdCollins1 in forum New To Java
    Replies: 5
    Last Post: 03-21-2012, 05:07 AM
  2. Add action listener to server
    By kevinn205 in forum New To Java
    Replies: 24
    Last Post: 12-14-2011, 08:15 PM
  3. Replies: 2
    Last Post: 12-11-2011, 11:44 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 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
  •