Results 1 to 3 of 3
  1. #1
    kevinn206 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Help Implement Action Listener With Jbutton Above JTable

    Hey! Thank you for taking the time to look at my post. Whenever I run this program it is working as I intended, It has 3 JButtons on the top and a Jtable that imports data from mysql; Unfortunatly I am unable to get the action listener to recognize the buttons. Does anyone know what I have done wrong? Did I misplace some lines of code, or is this problem much more complex than I assumed?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class jtable implements ActionListener{
      public static void main(String[] args) {
      new jtable();
      //StoreData.StoreData();
      
      }
    
      public jtable(){
    	  JButton homeButton = new JButton("Home");
    	  JButton exitButton = new JButton("exit");
    	  JButton refreshButton = new JButton("Refresh");
      
      JFrame frame = new JFrame("NEW CRM INFO");
      JPanel panel = new JPanel();
      
    
      panel.add( homeButton, getConstraints(0,0,1,1, GridBagConstraints.EAST));
      homeButton.addActionListener(this);
      panel.add(exitButton,getConstraints(0,0,1,1, GridBagConstraints.EAST));
      exitButton.addActionListener(this);
      panel.add(refreshButton,getConstraints(0,0,1,1, GridBagConstraints.EAST));
      refreshButton.addActionListener(this);
    
      
      String data[][] = {{}};
      String col[] = {"InfoID","Company","Date/Time","Reason","Note"};
    
     
      //Creates default table model
      DefaultTableModel model = new DefaultTableModel(data,col);
      JTable table = new JTable(model);
     
      //SetsTable Size Within the Pane
      table.setPreferredScrollableViewportSize(new Dimension(910, 420));
    table.setFillsViewportHeight(true);
      
      JTableHeader header = table.getTableHeader();
      header.setBackground(Color.YELLOW);
      JScrollPane pane = new JScrollPane(table);
      pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      pane.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_ALWAYS);
     
      //Set AutoResize 
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      //Set Column Widths 
      table.getColumnModel().getColumn(0).setMinWidth(60);     
      table.getColumnModel().getColumn(1).setMinWidth(190);      
      table.getColumnModel().getColumn(2).setMinWidth(160);    
      table.getColumnModel().getColumn(3).setMinWidth(100);    
      table.getColumnModel().getColumn(4).setMinWidth(400);
    
      table.setAutoCreateRowSorter(true);
      panel.add(pane);
      frame.add(panel);
      frame.setSize(1000,500);
      frame.setUndecorated(true);
      frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      
      
      
      //Insert first position
    
    	}
      
    	 private GridBagConstraints getConstraints(int gridx, int gridy,
    			    int gridwidth, int gridheight, int anchor)
    			    {
    			        GridBagConstraints c = new GridBagConstraints();
    			        c.insets = new Insets(5, 5, 5, 5);
    			        c.ipadx = 0;
    			        c.ipady = 0;
    			        c.gridx = gridx;
    			        c.gridy = gridy;
    			        c.gridwidth = gridwidth;
    			        c.gridheight = gridheight;
    			        c.anchor = anchor;
    			        return c;
    			    }
    	 
    	  public void actionPerformed(ActionEvent e)
    	  {
    	  	
    	      Object source = e.getSource();
    	      if (source == exitButton)
    	          System.exit(0);
    	      else if (source == homeButton)
    	      {Home.Home();}
    	      else if (source == acceptButton)
    	      	{	          }}
    		
    	}

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Help Implement Action Listener With Jbutton Above JTable

    First of all use proper Java naming conventions. Class names start with an upper case character.

    The problem is that you create the JButtons as local variables so the actionPerformed() method knows nothing about them.

    A better solution is to use the action command of the ActionEvent.
    Java Code:
    String command = e.getActionCommand();
    
    if ("Exit".equals(command))
      // do exit processing
    else if (...)
    Or you can also use annonymouse inner classes a create a custom ActionListener for each button.
    Last edited by camickr; 11-09-2011 at 05:09 PM.

  3. #3
    kevinn206 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help Implement Action Listener With Jbutton Above JTable

    I think ill use the Annonymouse class ;)

    Thank you very much for your help! I really Appreicate it!!

    --Post Resolved

Similar Threads

  1. Jbutton inside a jtable wont fire action
    By giskard in forum AWT / Swing
    Replies: 5
    Last Post: 08-02-2011, 08:07 PM
  2. Help with Combo box and action listener
    By sunilmenhdiratta in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2011, 08:32 PM
  3. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM
  5. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM

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
  •