I fixed the StackOverFlowError problem from my last thread.. however, my ActionListener is still not working. if any GUI guru here may point out the issue, it would be great. i think this is something very simple and i miss it.

//my ActionListener class

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ShowTable implements ActionListener
{
	private DataHolder dh = new DataHolder();
	private JScrollPane sPane;
	private JTable sTable;
	
	public ShowTable(JScrollPane paraS, JTable paraT)
	{
		this.sPane = paraS;
		this.sTable = paraT;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
			try
			{
				sTable = new JTable(dh.getRowData(), dh.getColumnNames());
				sTable.setAutoCreateRowSorter(true);
				sTable.setBackground(java.awt.Color.lightGray);
			}
	
			catch(SQLException sqle)
			{
				sqle.printStackTrace();
			}
		
		sPane = new JScrollPane(sTable);
		
	}
}

And this is my Menu class, please see the middle *** section
you may ignore the rest of the code in the class, the GUI itself works

import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class MenuBar extends JPanel
{
	private FlowLayout fl = new FlowLayout();
	private JMenuBar menuBar;
	private JMenu menuFile, menuEdit;
	public JMenuItem openMenuItem, closeMenuItem, printMenuItem, showTableMItem, addRecordMItem;
	private JScrollPane scrollPane;
	private JTable table;
	
	public MenuBar()
	{
	openMenuItem = new JMenuItem("Open");
	openMenuItem.setHorizontalTextPosition(SwingConstants.LEFT);
	openMenuItem.setAccelerator(KeyStroke.getKeyStroke('o', java.awt.Event.CTRL_MASK, false));
	//openMenuItem.addActionListener(new ActionPerform(i));
	
	closeMenuItem = new JMenuItem("Close");
	closeMenuItem.setAccelerator(KeyStroke.getKeyStroke('q', java.awt.Event.CTRL_MASK, false));
	closeMenuItem.addActionListener(new closeMenuHandler());
	
	
	printMenuItem = new JMenuItem("Print");
	printMenuItem.setAccelerator(KeyStroke.getKeyStroke('p', java.awt.Event.CTRL_MASK, false));
	////////////// ACTION ///////////////////////
	
	//////////// Menu Bar Edit items /////////////////
	showTableMItem = new JMenuItem("Show Table");


	
******** this is the problem *********************

showTableMItem.addActionListener(new ShowTable(scrollPane, table));

	
******* please ignore the rest of the code **********


	
	addRecordMItem = new JMenuItem("Add Record");
	//addRecordMItem.addActionListener(this);
	///////////////////////////////////////////////
	
	
	
	///////////////// MENU Config ///////////////////////////////////////
	menuFile = new JMenu("File");
	menuFile.add(openMenuItem);
	menuFile.add(printMenuItem);
	menuFile.add(closeMenuItem);
	
	menuEdit= new JMenu("Edit");
	menuEdit.add(showTableMItem);
	menuEdit.add(addRecordMItem);
	///////////////////////////////////////
	
	//////// Add all menu things to the menu bar /////////
	menuBar = new JMenuBar();
	menuBar.add(menuFile);
	menuBar.add(menuEdit);
	
	
	setLayout(fl);
	fl.setAlignment(FlowLayout.LEFT);
	add(menuBar);
	}// end Constructor
	
	
	
	
}// end class MenuBar


class closeMenuHandler implements ActionListener
{
	public void actionPerformed(ActionEvent ae)
	{
		System.exit(0);
		
	}
}

Recommended Answers

All 6 Replies

I just had a quick scan of your code, so I may have missed things, but...
In the actionlistener you overwrite the value of sTable that you set up in the constructor, so it seems pointless to have passed the value into the constructor in the first place. Is this what you intended?
Plus, if you want to display the new JTable that you create in the listener, shouldn't you add it to a visible frame or panel of some sort?

I just had a quick scan of your code, so I may have missed things, but...
In the actionlistener you overwrite the value of sTable that you set up in the constructor, so it seems pointless to have passed the value into the constructor in the first place. Is this what you intended?
Plus, if you want to display the new JTable that you create in the listener, shouldn't you add it to a visible frame or panel of some sort?

thx for the reply.

// In the actionlistener you overwrite the value of sTable that you set up in the constructor, so it seems pointless to have passed the value into the constructor in the first place.

i didn't mean to overwrite the value of sTable but the use the sTable from the Menu class.

i am trying to parse the JPanel from a JFrame class to the Menu class, and then the Menu class have (JPanel from JFrame class) , (JScrollPane -> local ) , and (JTable -> local)... then parse all the (JPanel, JScrollPane and JTable) to the ActionListener ..(put table to the Scroller , and then Scroller to the Pane).. then parse the Pane back to the Menu and back to the JFrame class. then add(JPane) from there.

this is the thing i can't get it to work. = (

That explanation doesn't make sense to me. Try to work on one issue at a time. And tell us what the overall task you're trying to accomplish is so we can help you by telling you what the best way to accomplish that goal is.

That explanation doesn't make sense to me. Try to work on one issue at a time. And tell us what the overall task you're trying to accomplish is so we can help you by telling you what the best way to accomplish that goal is.

thanks. maybe i was heading to the wrong direction.

first of all, i have a class extends JFrame (the main class)

import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class TheFrame extends JFrame 
{
	JPanel menuPanel;          // for the menu bar elements
	//JPanel bodyPanel;          // for the showTable, addRecord main stuffs
	
	
	public TheFrame()
	{
	menuPanel = new MenuBar();
	menuPanel.setBackground(java.awt.Color.GREEN);
	

	//********** ignore for now *************
	/*
	JLabel msg = new JLabel("Welcome!", JLabel.CENTER);
    bodyPanel = new JPanel();
    bodyPanel.setBackground(java.awt.Color.GREEN);
    bodyPanel.add(msg, BorderLayout.SOUTH);
	*/
	// ******** ignore for now ************
	
	
	add(menuPanel, BorderLayout.NORTH);
	//add(bodyPanel, BorderLayout.CENTER);
	
	
		
    setBounds(400, 400, 800, 300);
    setVisible(true);
    
	}
	
    

	
	

	public static void main(String arg[]) throws SQLException
	{
		TheFrame frame = new TheFrame();
	}

	
}// end the Class TheFrame

ok, now i have a menuBar class, i need to have a menuBar containing a menuItem "showTableMIteam"

something like this:

import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class MenuBar extends JPanel //implements ActionListener
{
	private FlowLayout fl = new FlowLayout();
	private JMenuBar menuBar;
	private JMenu menuFile, menuEdit;
	private JMenuItem openMenuItem, closeMenuItem, printMenuItem, showTableMItem, addRecordMItem;

	private Container contentPane;
	private JScrollPane scrollPane;
	private JTable table;

	
	public MenuBar()
	{
		
	
		openMenuItem = new JMenuItem("Open");
		openMenuItem.setHorizontalTextPosition(SwingConstants.LEFT);
		openMenuItem.setAccelerator(KeyStroke.getKeyStroke('o', java.awt.Event.CTRL_MASK, false));
		//openMenuItem.addActionListener(new ActionPerform(i));
	
		closeMenuItem = new JMenuItem("Close");
		closeMenuItem.setAccelerator(KeyStroke.getKeyStroke('q', java.awt.Event.CTRL_MASK, false));
		closeMenuItem.addActionListener(new closeMenuHandler());
	
	
		printMenuItem = new JMenuItem("Print");
		printMenuItem.setAccelerator(KeyStroke.getKeyStroke('p', java.awt.Event.CTRL_MASK, false));
		////////////// ACTION ///////////////////////
	
		//*****************************************************************************
		//////////// Menu Bar Edit items /////////////////
		showTableMItem = new JMenuItem("Show Table");
		showTableMItem.addActionListener(new ShowTable(scrollPane, table));
	
	
		//*****************************************************************************
	
	
		addRecordMItem = new JMenuItem("Add Record");
		//addRecordMItem.addActionListener(this);
		///////////////////////////////////////////////
	
	
	
		///////////////// MENU Config ///////////////////////////////////////
		menuFile = new JMenu("File");
		menuFile.add(openMenuItem);
		menuFile.add(printMenuItem);
		menuFile.add(closeMenuItem);
	
		menuEdit= new JMenu("Edit");
		menuEdit.add(showTableMItem);
		menuEdit.add(addRecordMItem);
		///////////////////////////////////////
	
		//////// Add all menu things to the menu bar /////////
		menuBar = new JMenuBar();
		menuBar.add(menuFile);
		menuBar.add(menuEdit);
	
	
		setLayout(fl);
		fl.setAlignment(FlowLayout.LEFT);
		
		contentPane = getContentPane();
		
		add(menuBar, BorderLayout.NORTH);
		add(scrollPane, BorderLayout.CENTER);
	}// end Constructor
	

	public JScrollPane getScrollPane()
	{
		return scrollPane;
	}
	
	
}

class closeMenuHandler implements ActionListener
{
	public void actionPerformed(ActionEvent ae)
	{
		System.exit(0);
		
	}
}

and then a class for the ActionListener would get info from a database and add to a JTable (this part was tested and successful) and then add the JTable to the JScrollPane.

this is the class SHOWTable implementing ActionListener

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ShowTable implements ActionListener
{
	
	private DataHolder dh = new DataHolder();
	
	private JScrollPane sPane;
	private JTable sTable;

	
	public ShowTable(JScrollPane paraS, JTable paraT)
	{
		this.sPane = paraS;
		this.sTable = paraT;
	}// end constructor 
	
	public void actionPerformed(ActionEvent e)
	{
		
			try
			{
				sTable = new JTable(dh.getRowData(), dh.getColumnNames());
				sTable.setAutoCreateRowSorter(true);
				sTable.setBackground(java.awt.Color.lightGray);
			}
	
			catch(SQLException sqle)
			{
				sqle.printStackTrace();
			}
		
		sPane = new JScrollPane(sTable);
		//framePanel.add(sPane);
		
	}// end actionPerformed
	
}// end class ShowTable

sorry i have been messing around different things in the code, hope this will clarify what i am trying to do. Thanks

In your MenuBar class, you never initialized the scrollPane. This will cause problems in your MenuBar constructor, since you are using the scrollPane Object, which was never initialized (created using new JScrollPane). Hope that helps, if not, I can clarify. If you're having problems still once you fix that, point out what you're having problems with (For example, "my method xxx is supposed to do yyy but it isn't doing it and I don't know why") etc.

finally got it work the way supposed to... = )

thanks for all the helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.