Results 1 to 3 of 3
- 11-09-2011, 04:51 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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) { }} }
- 11-09-2011, 05:03 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
Or you can also use annonymouse inner classes a create a custom ActionListener for each button.Java Code:String command = e.getActionCommand(); if ("Exit".equals(command)) // do exit processing else if (...)Last edited by camickr; 11-09-2011 at 05:09 PM.
- 11-09-2011, 06:24 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Jbutton inside a jtable wont fire action
By giskard in forum AWT / SwingReplies: 5Last Post: 08-02-2011, 08:07 PM -
Help with Combo box and action listener
By sunilmenhdiratta in forum AWT / SwingReplies: 2Last Post: 06-25-2011, 08:32 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
action listener on jcombobox
By chkm8 in forum New To JavaReplies: 2Last Post: 02-05-2009, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks