Link to home
Start Free TrialLog in
Avatar of tburger88
tburger88

asked on

This homework problem has me stumped...

I've been working on this one for hours, and yes, this is homework so I'm not lookig for the answer - just a hint maybe? Question: "why is the tooltip text not displayed in this code?"

package myframewithcomponents;

import javax.swing.*;

public class MyFrameWithComponents extends JFrame {
    
    private JButton jbtOK = new JButton( "OK" );
    
    public static void main(String[] args) { 
        // Create a frame and set its properties 
        JFrame frame = new MyFrameWithComponents(); 
        frame.setTitle( "Logic Error" ); 
        frame.setSize( 200 , 100 ); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible( true ); }
    
    public MyFrameWithComponents() { 
        jbtOK.setToolTipText( "This is a button" ); 
        add( new JButton( "OK" ) ); }
} 

Open in new window

Avatar of Guru Ji
Guru Ji
Flag of Canada image

try this

JComponent button = new JButton("OK");

// Set tool tip text
button.setToolTipText("tool tip text");
Avatar of tburger88
tburger88

ASKER

OH. My. God. Head. Desk.

It wasn't instantiated. Could I be that dumb? Is that the problem?
Avatar of kaufmed
It wasn't instantiated.
I might have overlooked something, but everything appears to be instantiated to me. However, I see that in MyFrameWithComponents you are setting the tooltip on the class-level JButton, but then calling the add method with an entirely new and different instance of a JButton. Could that be the issue?
Could be, try that

Also once you add your button to the component/frame you adding, after that at last assign the tooltip
u are not adding the button to the form ur only adding it to the class
ASKER CERTIFIED SOLUTION
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THanks you all.