Author Topic: Java JPanel double menubar  (Read 2055 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Java JPanel double menubar
« on: September 29, 2014, 03:50:20 pm »
The program is supposed to have a menu bar with 0-9 menu items. When you click on one item the number should display. Then it should also have a key listener: when you would press a key (0-9) that number would print.

I had some troubels, couldn't get any response from pressing keys so I went the mouse listener way to try and pinpoint the problem.

Pic 1: I start the program, open menu "Items" and click on 9 that is then displayed.
Pic 2: I then left click on panel and get this result.

Why does it paint another menubar that isn't even functional?
How would I do the keyListener part?
         -I tried something along the lines: ...KeyAdapter....KeyTyped.....x=event.getKeyCode()...  But it didn't even recognize any key I pressed.
Also, how can I "debug" this program. I try to execute it line by line and put breakpoints in every method but it just executes to the part setVisible(true) and then I can't do anything more with it. It won't show menu, I can exit it...

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java JPanel double menubar
« Reply #1 on: September 29, 2014, 08:29:30 pm »
Please post a pastebin of your code, DreX. Or plain text code in code-tags.
Images are really hard to copy&paste into Eclipse.   :-\

Edit: Oh, and place a validate(); instruction before the repaint(); as well.

Quote
I tried something along the lines: ...KeyAdapter....KeyTyped.....x=event.getKeyCode()...  But it didn't even recognize any key I pressed.

Often it is a problem with the focus. But without code I can't tell you what went wrong.
« Last Edit: September 29, 2014, 08:41:51 pm by Deque »

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Java JPanel double menubar
« Reply #2 on: September 30, 2014, 08:13:12 pm »
Please post a pastebin of your code, DreX. Or plain text code in code-tags.
Images are really hard to copy&paste into Eclipse.   :-\

Edit: Oh, and place a validate(); instruction before the repaint(); as well.

Often it is a problem with the focus. But without code I can't tell you what went wrong.

My bad on the "program code in pic". As for the validate(); it didn't work. I still get the same result.
Here is the code.

Code: (java) [Select]
package exerciseChapter10q5;
import javax.swing.JFrame;

public class Main {
    public static void main (String [] args){
        MyFrame window=new MyFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(300,300);
        window.setVisible(true);
    }
}

package exerciseChapter10q5;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MyFrame extends JFrame{
    MyPanel panel;
   
    public MyFrame(){
        super("Title");
        panel=new MyPanel();
        add(panel);
        JMenuBar menuBar=new JMenuBar();
        setJMenuBar(menuBar);
        JMenu numbers=new JMenu("Items");
        menuBar.add(numbers);
        JMenuItem zero = new JMenuItem("0");
        JMenuItem one = new JMenuItem("1");
        JMenuItem two = new JMenuItem("2");
        JMenuItem three = new JMenuItem("3");
        JMenuItem four = new JMenuItem("4");
        JMenuItem five = new JMenuItem("5");
        JMenuItem six = new JMenuItem("6");
        JMenuItem seven = new JMenuItem("7");
        JMenuItem eight = new JMenuItem("8");
        JMenuItem nine = new JMenuItem("9");
        numbers.add(zero);
        numbers.add(one);
        numbers.add(two);
        numbers.add(three);
        numbers.add(four);
        numbers.add(five);
        numbers.add(six);
        numbers.add(seven);
        numbers.add(eight);
        numbers.add(nine);
        zero.addActionListener(new NumberListener(0));
        one.addActionListener(new NumberListener(1));
        two.addActionListener(new NumberListener(2));
        three.addActionListener(new NumberListener(3));
        four.addActionListener(new NumberListener(4));
        five.addActionListener(new NumberListener(5));
        six.addActionListener(new NumberListener(6));
        seven.addActionListener(new NumberListener(7));
        eight.addActionListener(new NumberListener(8));
        nine.addActionListener(new NumberListener(9));
    }
   
    class NumberListener implements ActionListener {
        private int num;
       
        public NumberListener(int num) {
            this.num=num;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            panel.setNumber(num);
        }
    }
}

package exerciseChapter10q5;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

public class MyPanel extends JPanel {
    private String number="";
   
    public MyPanel(){
        addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent e) {
                int x;
                if(e.getButton()==1){
                    x=13;
                    setNumber(x);
                }
            }
           
        });
    }
   

    public void paintComponent(Graphics g){
        Graphics2D g2=(Graphics2D) g;
        showMessage(g2);
    }
   
    public void setNumber(int number){
        this.number=number+" ";
        validate();
        repaint();
    }
   
    public void showMessage(Graphics2D g2){
        Font myFont=new Font("SansSerif",Font.BOLD, 40 );
        g2.setFont(myFont);
        g2.setColor(Color.BLACK);   
        g2.drawString(number, 100,100);

    }
}

As for the keyListener. This is the change I made.
Code: (java) [Select]
    public MyPanel(){
        addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e) {
                int x;
                x=e.getID();
                setNumber(x);
            }
           
        });
    }

Also. What does validate do exactly?

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java JPanel double menubar
« Reply #3 on: October 01, 2014, 09:50:03 am »
Thank you. First off I didn't encounter any problems with the second menu, but it might be related.

In MyPanel the paintComponent method must call super.paintComponent(g), so the parent is updated as well.
You must always call super.paintComponent if you override paintComponent anywere.

Code: (Java) [Select]
public void paintComponent(Graphics g){
    super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g;
        showMessage(g2);
    }

See if that solves your problems with the overlapping numbers and the second menu.

I will have a look at the Keylistener problem now.

Validate restructures the layout. If you encounter layout problems like with the second menu, it might help.

-----------------------------------------

KeyListener:
Set MyPanel focusable and request the focus. See below:

Code: (Java) [Select]
public MyPanel(){
    setFocusable(true);
    requestFocus();
   
    addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e) {
                int x;
                x=e.getID();
                System.out.println("key typed: " + e.getKeyChar());
                System.out.println("key typed: " + x);
                setNumber(x);
            }
           
        });
« Last Edit: October 01, 2014, 09:58:39 am by Deque »

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Java JPanel double menubar
« Reply #4 on: October 03, 2014, 06:22:39 pm »
Thank you. First off I didn't encounter any problems with the second menu, but it might be related.

In MyPanel the paintComponent method must call super.paintComponent(g), so the parent is updated as well.
You must always call super.paintComponent if you override paintComponent anywere.

Code: (Java) [Select]
public void paintComponent(Graphics g){
       super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g;
        showMessage(g2);
    }

See if that solves your problems with the overlapping numbers and the second menu.

I will have a look at the Keylistener problem now.

Validate restructures the layout. If you encounter layout problems like with the second menu, it might help.

-----------------------------------------

KeyListener:
Set MyPanel focusable and request the focus. See below:

Code: (Java) [Select]
public MyPanel(){
       setFocusable(true);
       requestFocus();
       
       addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e) {
                int x;
                x=e.getID();
                System.out.println("key typed: " + e.getKeyChar());
                System.out.println("key typed: " + x);
                setNumber(x);
            }
           
        });

thanks man. everything works, except the e.getKeyChar() which then prints from 48-57 but i can figure that out myself. (edit....ASCII)
ty vm

« Last Edit: October 03, 2014, 08:33:14 pm by DreX »