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?
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)
{ }}
}
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.
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.
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