Stackoverflow exception on addActionListener method - java

I have some problems with my application. I'm trying to create a JButton just with AWT components. The main problem is that I have an exception : at QButton.QButton.addActionListener(QButton.java:83).
If i comment the line //this.addActionListener everything is ok. My object is extends Panel
public class QButton extends Panel implements MouseListener,ActionListener{
public Label text;
ImagePanel image;
ActionListener listener;
public QButton(String text){
Label l = new Label(text);
this.add(l);
this.text=l;
this.setLayout(new GridBagLayout());
this.setBackground(Color.gray);
TextButtonActions ac1=new TextButtonActions(this);
this.addMouseListener(ac1);
this.text.addMouseListener(ac1);
}
public QButton(ImagePanel img){
this.setLayout(new GridLayout());
this.image=img;
this.add(image);
PictureButtonActions ac2=new PictureButtonActions(this);
this.image.addMouseListener(ac2);
}
public QButton(String text, ImagePanel img){
this.setBackground(Color.gray);
this.setLayout(new GridLayout());
Label l = new Label(text);
this.add(l);
this.text=l;
this.image=img;
this.add(image);
TAndPButtonActions ac3=new TAndPButtonActions(this);
this.image.addMouseListener(ac3);
this.text.addMouseListener(ac3);
}
public void setText(String txt)
{
this.text.setText(txt);
}
public String getText()
{
return(text.getText());
}
public void setImage(ImagePanel i)
{
this.remove(image);
this.image=i;
this.add(i);
//System.out.println("setImage");
this.validate();
}
public ImagePanel getImage()
{
return(image);
}
void addActionListener(ActionListener listener)
{
this.listener=listener;
this.addActionListener(listener);
}
}

I'm not familiar with UI things, but the reason you get a StackOverflowException, is because your program trying to invoke the method which will call itself.
void addActionListener(ActionListener listener)
// ^^^^^^^^^^^^^^^^^
{
this.listener=listener;
this.addActionListener(listener); // <-- will keep calling itself.
//^^^^^^^^^^^^^^^^^
}

Perhaps you meant to say super.addActionListener(listener);?

Related

JAVA cardLayout. how to call a card from different classes

i have a program with a frame that contains a main panel with the cardlayout layout, and i want it to display different cards/panel.
In my case i'm really struggling to call a new card from a button action listener.
I want a new card to appear after i click on a button but none of the codes i put in my action listener displayed the card i wanted.
I know my actionListener work because i did a println inside.
here's my code. i got rid of anything that was unnecessary so it's easier to read. Thanks for the help!
i'll take all advices about code structuration
the mainFrame :
public class MainFrame extends JFrame{
final static String CONNEXION_VIEW = "connexionView";
final static String CONNEXION_FAILED_VIEW = "connexionRefusee";
public MainFrame()
{
super();
initialize();
}
private void initialize()
{
getMainPanel();
add(getMainPanel());
}
CardLayout cardLayout;
public CardLayout getCardLayout()
{
if (cardLayout == null)
{
cardLayout = new CardLayout();
}
return cardLayout;
}
JPanel mainPanel;
public JPanel getMainPanel()
{
if (mainPanel == null)
{
mainPanel = new JPanel();
mainPanel.setLayout(getCardLayout());
mainPanel.add(CONNEXION_VIEW, getConnexionView());
mainPanel.add(CONNEXION_FAILED_VIEW, getConnexionFailedView());
}
return mainPanel;
}
ConnexionView connexionView;
protected ConnexionView getConnexionView()
{
if (connexionView == null)
{
connexionView = new ConnexionView();
}
return connexionView;
}
ConnexionFailedView connexionFailedView;
protected ConnexionFailedView getConnexionFailedView()
{
if (connexionFailedView == null)
{
connexionFailedView = new ConnexionFailedView();
}
return connexionFailedView;
}
the connexion view, the one with the button to click with the action listener where i want to put my code
public class ConnexionView extends JPanel{
GridBagLayout gbl = new GridBagLayout();
private JButton btnConnexion;
Dimension dimensionBouton = new Dimension(170, 30);
public ConnexionView()
{
super();
initialise();
}
private void initialise()
{
setLayout(gbl);
GridBagConstraints gbcbtnConnexion = new GridBagConstraints();
gbcbtnConnexion.gridwidth = GridBagConstraints.REMAINDER;
gbcbtnConnexion.gridheight = GridBagConstraints.REMAINDER;
gbcbtnConnexion.gridx = 1;
gbcbtnConnexion.gridy = 2;
add(getBtnConnexion(), gbcbtnConnexion);
}
private JButton getBtnConnexion()
{
if (btnConnexion == null)
{
btnConnexion = new JButton("Connexion");
btnConnexion.setPreferredSize(dimensionBouton);
btnConnexion.setMinimumSize(dimensionBouton);
btnConnexion.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
/////code to display the connexion_Failed_View
System.out.println("test");
}
});
}
return btnConnexion;
}
}
and the connexion failed view, the one i want to display after the button is clicked
public class ConnexionFailedView extends JPanel{
public ConnexionFailedView()
{
super();
initialise();
}
private void initialise()
{
setBackground(Color.YELLOW);
}
thanks in advance
You will need to keep the component in your Button somehow, so you can access it.
class ConnexionView {
private JComponent mainPanel;
public ConnexionView(JComponent mp) { mainPanel = mp; }
}
Obviously, that means the MainFrame needs to pass it then.
Now, the listener can do
// it would be cleaner if you passed the layout in the constructor as well
CardLayout cl = (CardLayout) mainPanel.getLayoutManager();
cl.show(mainPanel, MainFrame.CONNEXION_FAILED_VIEW);

Java Observer/Observable update

I've tried to apply the Observable/Observer pattern but there is something wrong with my code when I try to change a the textfield of a JTextPane.
I've got 3 classes, Play, Controller and SecondWindow here are a sample of their code.
public class Play() {
Controller c = new Controller();
SecondWindow sw = new SecondWindow();
c.addObserver(sw)
c.setText("blabla");
}
My class Controller:
public class Controller extends Observable(){
private String text ="";
private static Controller getInstance() {
if (instance == null) {
instance = new Controller();
}
return instance;
}
public void setText(String s) {
text = s;
setChanged();
notifyObservers();
}
}
and SecondWindow:
public class SecondWindow extends JFrame implements Observer{
private JPanel contentPane;
private Controller c;
private JTextPane txt = new JTextPane();
public SecondWindow () {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SecondWindow frame = new SecondWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SecondWindow() {
initComponents();
createEvents();
c = Controller.getInstance();
}
public void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(1000, 0, 300,500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txt.setBounds(0, 0, 280, 460);
txt.enable(false);
contentPane.add(txt);
}
public void update(Observable arg0 , Object arg1){
// Things to change here
}
I can't manage to put the variable c in the textField (like a txt.setText(c.getText) instruction). I'm sure that it reads the method update, but I don't know how to make sure it works.
Hint: Per the Observerable API the notifyObservers method has an overload that accepts any object as a parameter:
public void notifyObservers(Object arg)
This can even be a String. And as per the Observer API, this object is then passed into the update method in the observer, and you can use it there.
void update(Observable o,
Object arg)
arg - an argument passed to the notifyObservers method.
Separate side issue here:
contentPane.setLayout(null);
For most Swing aficionados, seeing this is like hearing nails on a chalkboard -- it's painful. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
Side issue number two: your code is not Swing thread safe, since the Swing GUI could very well be notified by the observable off of the Swing event dispatch thread or EDT. While it is not likely to cause frequent or serious problems with this simple program, in general it would be better to use a SwingPropertyChangeSupport and PropertyChangeListeners rather than Observer / Observable if you can.
Next Side Issue
This:
public class Controller extends Observable(){
isn't compilable / kosher Java. Same for the duplicate parameter-less constructors for the SecondWindow class. Yes, we know what you're trying to do, but it's hard enough trying to understand someone else's code, you really don't want to make it harder by posting kind-of sort-of uncompilable code, trust me.
For example, something simple could be implemented in Swing using PropertyChangeListeners, like so:
import java.util.concurrent.TimeUnit;
public class Play2 {
public static void main(String[] args) {
Model2 model2 = new Model2();
View2 view2 = new View2();
new Controller2(model2, view2);
view2.show();
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// one of the few times it's OK to ignore an exception
}
String text = String.format("Counter Value: %d", i);
model2.setText(text);
}
}
}
import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
public class Model2 {
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(this);
public static final String TEXT = "text"; // name of our "bound" property
private String text = "";
public String getText() {
return text;
}
public void setText(String text) {
String oldValue = this.text;
String newValue = text;
this.text = text;
pcSupport.firePropertyChange(TEXT, oldValue, newValue);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
public void addPropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(name, listener);
}
public void removePropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(name, listener);
}
}
import javax.swing.*;
public class View2 {
private JPanel mainPanel = new JPanel();
private JTextField textField = new JTextField(10);
public View2() {
textField.setFocusable(false);
mainPanel.add(new JLabel("Text:"));
mainPanel.add(textField);
}
public JPanel getMainPanel() {
return mainPanel;
}
public void setText(String text) {
textField.setText(text);
}
public void show() {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class Controller2 {
private Model2 model2;
private View2 view2;
public Controller2(Model2 model2, View2 view2) {
this.model2 = model2;
this.view2 = view2;
model2.addPropertyChangeListener(Model2.TEXT, new ModelListener());
}
private class ModelListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent pcEvt) {
view2.setText((String) pcEvt.getNewValue());
}
}
}

How to have a Java Spinner with left and right arrows?

I've used java Spinner in Java Swing, but it had up and down arrows, is there a way to set it's orientation so that the arrows are left and right ?
Thanks to Jean-François Savard's answer, I'm one step closer, but still not quite right, I have the following lines :
public void installUI(JComponent c)
{
super.installUI(c);
c.removeAll();
FlowLayout FL=new FlowLayout();
FL.setHgap(0);
c.setLayout(FL);
JComponent editor=createEditor();
editor.setPreferredSize(new Dimension(30,16));
c.add(editor);
c.add(createPreviousButton());
c.add(createNextButton());
}
The spacing is not correct, how to fix it ? I hard coded in the above lines, how to automatically provide proper space for the text ?
A short search on google lead me to a custom implementation of JSpinner to do this.
public class LeftRightSpinnerDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LeftRightSpinnerDemo().makeUI();
}
});
}
public void makeUI() {
JSpinner spinner = new JSpinner();
spinner.setUI(new LeftRightSpinnerUI());
JFrame frame = new JFrame();
frame.add(spinner);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class LeftRightSpinnerUI extends BasicSpinnerUI {
public static ComponentUI createUI(JComponent c) {
return new LeftRightSpinnerUI();
}
#Override
protected Component createNextButton() {
Component c = createArrowButton(SwingConstants.EAST);
c.setName("Spinner.nextButton");
installNextButtonListeners(c);
return c;
}
#Override
protected Component createPreviousButton() {
Component c = createArrowButton(SwingConstants.WEST);
c.setName("Spinner.previousButton");
installPreviousButtonListeners(c);
return c;
}
// copied from BasicSpinnerUI
private Component createArrowButton(int direction) {
JButton b = new BasicArrowButton(direction);
Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
if (buttonBorder instanceof UIResource) {
b.setBorder(new CompoundBorder(buttonBorder, null));
} else {
b.setBorder(buttonBorder);
}
b.setInheritsPopupMenu(true);
return b;
}
#Override
public void installUI(JComponent c) {
super.installUI(c);
c.removeAll();
c.setLayout(new BorderLayout());
c.add(createNextButton(), BorderLayout.EAST);
c.add(createPreviousButton(), BorderLayout.WEST);
c.add(createEditor(), BorderLayout.CENTER);
}
}
Make sure to add the correct imports as I removed them to lighten the code.
Refer to this for the original post.

Leap Motion problems with Swing?

I'm starting to toy a little with the Leap Motion Controller and made a small GUI in Swing. That means it's just a Frame with a Label on it which should show a text of what the Leap Motion sees. Unfortunately my programm simply breaks down after two seconds. I have no Exceptions or something like that. Here's my code:
public class GUI extends JFrame{
private static final long serialVersionUID = 6411499808530678723L;
public JLabel label;
public GUI(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 200);
this.label = new JLabel("Waiting for Gestures");
this.add(label);
setVisible(true);
}
public class LeapListener extends Listener{
JLabel label;
LeapListener(JLabel label){
this.label = label;
}
#Override
public void onInit(Controller controller){
}
#Override
public void onExit(Controller controller){
}
#Override
public void onConnect(Controller controller){
System.out.println("bin da");
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
}
#Override
public void onDisconnect(Controller controller){
}
#Override
public void onFrame(Controller controller){
Frame frame = controller.frame();
GestureList glist = frame.gestures();
for (int i = 0; i < glist.count(); i++){
Gesture g = glist.get(i);
switch (g.type()){
case TYPE_CIRCLE:
CircleGesture circle = new CircleGesture(g);
this.label.setText("Mach mal nen Kreis!");
case TYPE_KEY_TAP:
KeyTapGesture key = new KeyTapGesture(g);
this.label.setText("Schreiben?");
case TYPE_SCREEN_TAP:
ScreenTapGesture screen = new ScreenTapGesture(g);
this.label.setText("Klicken?");
case TYPE_SWIPE:
SwipeGesture swipe = new SwipeGesture(g);
this.label.setText("Da wurde gewischt!");
default:
System.out.println("nothing!");
break;
}
}
}
}
public static void main(String[] args) {
GUI gui = new GUI();
LeapListener listener;
listener = gui.new LeapListener(gui.label);
Controller controller = new Controller();
controller.addListener(listener);
}
}
I have no idea what I've done wrong. The Windows-Error-Message says that the Java Platform SE binary is down (javaw.exe). Errormodule: Leap.dll
Is it a mistake that I've made or is my whole setup f#*ked up?
Ok, I found a solution by myself but unfortunately I can't explain it.
I have to create the Controller Object in the class declaration, not in the main-method. Then I have to add the listener to the controller in the constructor of my GUI, like that:
public class GUI extends JFrame{
private static final long serialVersionUID = 6411499808530678723L;
public JLabel label;
public Controller c = new Controller();
public LeapListener listener;
public GUI(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 200);
this.label = new JLabel("Waiting for Gestures");
this.add(label);
listener = new LeapListener(this.label);
c.addListener(listener);
setVisible(true);
}
...
}
So, I'm happy that I got it to work but maybe somebody is interested in the problem and may find a reason for the error I've had.

JFrame Components not showing up

I have a problem that the JFrame is not showing upmy components.
When i opened the GasStationPanel in WindowBuilder it show well, but the MainFrame is show as a blank windows.
Please, I need you help here.
Thanks!
The JFrame code is:
public class MainFrame extends JFrame {
private GasStationPanel pnlMainGasStation;
public MainFrame() throws SecurityException, IOException {
try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
getContentPane().setLayout(new BorderLayout());
this.pnlMainGasStation = new GasStationPanel("all cars","pumps","coffee");
this.add(pnlMainGasStation, BorderLayout.CENTER);
setLocationRelativeTo(null);
setTitle("GasStation");
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
Utils.closeApplication(MainFrame.this);
}
});
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = new Dimension();
frameSize.setSize(screenSize.width*0.7, screenSize.height*0.9);
setSize(frameSize);
setVisible(true);
}
public GasStationPanel getMainPanel() {
return pnlMainGasStation;
}
}
The GasStationPanel Code:
public class GasStationPanel extends JPanel {
private JSplitPane splinterRight, splinterLeft;
private AllCarsPanel allCarsPanel;
private FuelPumpListPanel fuelPumpsListPanel;
private CoffeeHousePanel coffeeHousePanel;
private List<GasStationController> allListeners;
public AllCarsPanel getAllCarsPanel() {
return allCarsPanel;
}
public FuelPumpListPanel getFuelPumpsListPanel() {
return fuelPumpsListPanel;
}
public CoffeeHousePanel getCoffeeHousePanel() {
return coffeeHousePanel;
}
public GasStationPanel(String allCarsStr, String fuelPumpsListStr,
String coffeeHousePanelStr) throws SecurityException, IOException {
// Init Listeners List
this.allListeners = new ArrayList<GasStationController>();
// Layout and size
setLayout(new BorderLayout());
// Build panels
allCarsPanel = new AllCarsPanel();
fuelPumpsListPanel = new FuelPumpListPanel();
coffeeHousePanel = new CoffeeHousePanel();
// Split the screen to three
splinterRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splinterLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splinterLeft.setLeftComponent(allCarsPanel);
splinterLeft.setRightComponent(fuelPumpsListPanel);
splinterRight.setLeftComponent(splinterLeft);
splinterRight.setRightComponent(coffeeHousePanel);
}
public void registerListener(GasStationController gasStationController) {
this.allListeners.add(gasStationController);
}
In short, you never add any components to your container. For example, in your GasStationPanel code, perhaps you should try invoking add(Component) by passing in your JSplitPanes as an argument. For example:
add(splinterLeft, BorderLayout.CENTER);

Categories