I have 3 separate questions. I'm trying to develop a program that displays several vocabulary lists and has a row of buttons at the top for selecting the lists. These buttons don't appear until I re-size the window. Does anyone know why this occurs? Second, I've extended the JPanel class to implement the Mouse Listener event listener. The main class declaration, which extends the JFrame class, implements an Action Listener. Is there any way to implement both listeners in the main class declaration? Third and finally, I've attempted to draw a test string on an extension of a JPanel, but nothing shows up. Again, what am I doing wrong? I've attached the appropriate .java file if you need it, rather than just waste a bunch of space on this thread with my code. Any help would be greatly appreciated.

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

public class ClickyLatinHelp extends JFrame implements ActionListener {
	
	boolean showeng = true;
	
	JButton List1Button = new JButton("List 1");
	JButton List2Button = new JButton("List 2");
	JButton List3Button = new JButton("List 3");
	JButton List4Button = new JButton("List 4");
	JButton List5Button = new JButton("List 5");
	JButton List6Button = new JButton("List 6");
	JButton List7Button = new JButton("List 7");
	JButton List8Button = new JButton("List 8");
	JButton List9Button = new JButton("List 9");
	
	DrawingArea canvas = new DrawingArea();
	JPanel ButtonBanner = new JPanel();
	JPanel HoldStuff = new JPanel();
	
	BoxLayout box = new BoxLayout(HoldStuff, BoxLayout.Y_AXIS);
	
	public ClickyLatinHelp() {
		super("Welcome to Latin Help");                 
		setBounds(100, 100, 700, 600);                  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setVisible(true);
		
		List1Button.addActionListener(this);
		List2Button.addActionListener(this);
		List3Button.addActionListener(this);
		List4Button.addActionListener(this);
		List5Button.addActionListener(this);
		List6Button.addActionListener(this);
		List7Button.addActionListener(this);
		List8Button.addActionListener(this);
		List9Button.addActionListener(this);
		
		ButtonBanner.add(List1Button);
		ButtonBanner.add(List2Button);
		ButtonBanner.add(List3Button);
		ButtonBanner.add(List4Button);
		ButtonBanner.add(List5Button);
		ButtonBanner.add(List6Button);
		ButtonBanner.add(List7Button);
		ButtonBanner.add(List8Button);
		ButtonBanner.add(List9Button);
		HoldStuff.add(ButtonBanner);
		HoldStuff.add(canvas);
		add(HoldStuff);
	}
	
	public class DrawingArea extends JPanel implements MouseListener {
		public void mouseClicked(MouseEvent event){
			if (showeng = true) {
				showeng = false;
			}else {
				showeng = true;
			}
		}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseReleased(MouseEvent event){}
		
		public void paintComponent(Graphics comp) {
    	super.paintComponent(comp);
    	Graphics2D comp2D = (Graphics2D) comp;
    	comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	Font font = new Font("Arial", Font.BOLD, 18);
    	comp2D.setFont(font);
    	comp2D.drawString("Why, Hello there", 100, 300);
    	}
	}

	public void actionPerformed(ActionEvent evt) {
		
		}
	
	public static void main(String[] arguments) {
        ClickyLatinHelp TLH = new ClickyLatinHelp();
    }
    
}

(1) The buttons show up in my machine (JDK 1.6). So it seems to be some problem with your machine. You may use Button of AWT rather than JButton of swing which is a lightweight component and does not show up among any AWT stuff. However, your case is OK. Since you put all the buttons into one separated container so that no conflict occurs, and even JButton shoud show up. If you simply put a JButton into a canvas (e.g. Applet) it wouldn't show up.
(2) You did not set up Layout manager correctly. I modified your code with BorderLayout. It works. (See attached file)
(3) Should we call the class ClickyLatinHelp as a driver class rather then "main class" ? One class may implement several interfaces. Since ActionListener and MouseListener both are interfaces the driver class may implement both.
I am noticed that :
Your code:

if (showeng = true) {
				showeng = false;
			}else {
				showeng = true;
			}
		}

is not correct. It should be if (showeng == true)... rather than if (showeng=true)

Edited 6 Years Ago by tong1: n/a

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

public class ClickyLatinHelp extends JFrame implements ActionListener {
	
	boolean showeng = true;
	
	JButton List1Button = new JButton("List 1");
	JButton List2Button = new JButton("List 2");
	JButton List3Button = new JButton("List 3");
	JButton List4Button = new JButton("List 4");
	JButton List5Button = new JButton("List 5");
	JButton List6Button = new JButton("List 6");
	JButton List7Button = new JButton("List 7");
	JButton List8Button = new JButton("List 8");
	JButton List9Button = new JButton("List 9");
	

	
	public ClickyLatinHelp() {
		super("Welcome to Latin Help");                 
		setBounds(100, 100, 700, 600);                  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setVisible(true);
		
			DrawingArea canvas = new DrawingArea();
	JPanel ButtonBanner = new JPanel();
//	JPanel HoldStuff = new JPanel();
	
//	BoxLayout box = new BoxLayout(HoldStuff, BoxLayout.Y_AXIS);
    setLayout(new BorderLayout());		
		List1Button.addActionListener(this);
		List2Button.addActionListener(this);
		List3Button.addActionListener(this);
		List4Button.addActionListener(this);
		List5Button.addActionListener(this);
		List6Button.addActionListener(this);
		List7Button.addActionListener(this);
		List8Button.addActionListener(this);
		List9Button.addActionListener(this);
		
		ButtonBanner.add(List1Button);
		ButtonBanner.add(List2Button);
		ButtonBanner.add(List3Button);
		ButtonBanner.add(List4Button);
		ButtonBanner.add(List5Button);
		ButtonBanner.add(List6Button);
		ButtonBanner.add(List7Button);
		ButtonBanner.add(List8Button);
		ButtonBanner.add(List9Button);
		add(ButtonBanner,BorderLayout.NORTH);
		add(canvas,BorderLayout.CENTER);

	}
	
	public class DrawingArea extends JPanel implements MouseListener {
		public void mouseClicked(MouseEvent event){
			if (showeng = true) {
				showeng = false;
			}else {
				showeng = true;
			}
		}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseReleased(MouseEvent event){}
		
		public void paintComponent(Graphics comp) {
			
    	super.paintComponent(comp);
    	Graphics2D comp2D = (Graphics2D) comp;
    	comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	Font font = new Font("Arial", Font.BOLD, 18);
    	comp2D.setFont(font);
    	comp2D.drawString("Why, Hello there", 100, 300);
    	}
	}

	public void actionPerformed(ActionEvent evt) {
		
		}
	
	public static void main(String[] arguments) {
        ClickyLatinHelp TLH = new ClickyLatinHelp();
    }
    
}

First thing: setVisible AFTER the GUI has been built.


Another way to toggle a boolean is to set it to its ! value;
showeng = !showeng; // toggle flag

Edited 6 Years Ago by NormR1: n/a

A class can implement many interfaces.
this is done by
class className implements iterface1,interface2, interfcae3 {
}

Thank you all for your help, it works like a charm now that I modified it with your suggestions. Tong1, thanks for fixing my Java code. NormR1, one I set visible after building the gui, everything showed up right away without me resizing. Also, thanks for the tip about toggling booleans. new programmer, thanks for showing me how to implement multiple interfaces.

This question has already been answered. Start a new discussion instead.