Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same time.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same time.

    For some reason the JTabbedPane isn't showing up. I tried changing its orientation to bottom and then left to see if that would fix it but it didn't.

    It was just a simple program to see if all three could go together.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;
     import javax.swing.*;
    import java.util.*;
    import java.io.*;
     
    public class TesterClassOne extends JFrame
    {
     
    private JToggleButton jtb;
     
    public TesterClassOne()
    {
     
     
    JMenuBar jmb = new JMenuBar();
    setJMenuBar(jmb);
    JMenu aMenu = new JMenu("Menu");
    jmb.add(aMenu);
     
    JMenuItem anItem = new JMenuItem("A JMenuItem");
    aMenu.add(anItem);
     
    JTabbedPane jtp = new JTabbedPane(JTabbedPane.LEFT);
    jtp.addTab("A Tab", null);
    jtp.addTab("Another Tab", null);
    add(jtp);
    setVisible(true);
    jtp.setVisible(true);
     
     // setContentPane(getContentPane());
     
    }
     
    public static void main(String[] args)
    {
    TesterClassOne tc1 = new TesterClassOne();
    JToolBar jtba = new JToolBar();
    tc1.add(jtba);
    JButton aButton = new JButton("A JButton");
    jtba.add(aButton);
    JButton aButton2 = new JButton("Another JButton");
    jtba.add(aButton2);
    }
    }

    I have tried it with the setConentPane() and without it too. Doesn't appear to make a difference. Have tried it where all three were in constructor and also when all three were in main. (Defined there I mean.) Neither one solves the problem. I've tried some combinations and it still doesn't appear to work.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    You're main method is adding stuff over you're tabbed pane. Try using a layout manager if you want to display the tabs and buttons in two separate sections, or if you want to place the buttons into the tab, add it as the second parameter in the addTab method.

    For more info on layout managers, see: A Visual Guide to Layout Managers

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (May 22nd, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    I figured out a way to get it to work by using Box Layout. Though I didn't try it with the test program, but another, bigger one.

    However, I have two new problems, one of them is probably easier than the other. The first one, the easier one, is why isn't the JScrollPane showing up when I invoke that private class as the content for a tab on the JTabbedPane?

    I am wondering something about that though. I invoked a super that had one parameter in common with the parent class. The Component parameter. Hmmmmmmm...wait a minute...should I change it to pass a Component or whatever as a parameter instead of putting a JPanel to be passed? Second, for the first part still, is it even doing anything as the super invokes two scroll bar policies as parameters that aren't being passed to the constructor of the child class.

    The other problem is that I was trying to add a MouseListener(in fact I added the class's mouse listener since the class itself implements mouse listener, to the JPanel object being used by the JavaPaintProgram object as the content pane. Well actually, the JScrollPane is the content pane but I can't seem to see a scroll pane there either, though I told it to make the thing really big so that, if it were working correctly, there should be one for either the JScrollPane that is associated with the JTabbedPane or on the one that is associated, as part of the JScrollPane that contains it, as part of the content pane for the entire program. First off, is it a good idea, I know it's valid as I've tried it and it compiles and has no run errors so far from it, to do what I did by making the entire class to be the mouse listener of the JPanel.

    Another question, related to this second one, is, for this Mouse Listener, I have a paint method already there as well as all the necessary Mouse Listener methods. However, I don't know how to get the Mouse Listener to call a paint method. I'd like it to be able to paint as the mouse moves and is held, as long as it's in bounds of the JPanel of course, and I don't know what method I could call from Graphics class nor do I know how I could get it to call paint, since you can't instantiate a Graphics object, I did think that maybe I could do something like

    Graphics gra = Graphics.this;

    but if Graphics is an abstract class, not sure what that would do.

    Anyway, not sure how to call paint method inside mouse listener since I need to pass it a Graphics object to invoke it.

    I know that I can use the ActionListener of the button that chooses a Color to use the JColorChooser to select the color and I can call g.setColor() or whatever, but I'm not sure how to get it to go with the Mouse Listener.

    Here's the code:

    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
     
    public class JavaPaintProgram extends JFrame implements MouseListener
    {
     
    private JPanel panel;
    private JToolBar paintToolBar;
    private JMenuBar menubar;
    private JMenu file, help;
    private JMenuItem save, importImage, help2, exit;
    private JButton moreColors;
    private JTabbedPane jtp;
    private JColorChooser colorChooser;
    private Color color;
    public JavaPaintProgram()
    {
    menubar = new JMenuBar();
    file = new JMenu("File");
    help = new JMenu("Help");
    menubar.add(file);
    menubar.add(help);
    save = new JMenuItem("Save");
    importImage = new JMenuItem("Item image from");
    exit = new JMenuItem("Exit");
    file.add(save);
    file.addSeparator();
    file.add(importImage);
    file.addSeparator();
    file.add(exit);
    help2 = new JMenuItem("Help");
    help.add(help2);
    setJMenuBar(menubar);
    paintToolBar = new JToolBar();
     
    moreColors = new JButton("Select Color");
    paintToolBar.add(moreColors);
    paintToolBar.setBackground(Color.YELLOW);
    panel = new JPanel();
    panel.add(paintToolBar);
    setVisible(true);
    panel.setVisible(true);
    jtp = new JTabbedPane(JTabbedPane.TOP);
    panel.add(jtp);
    jtp.addTab("A Tab", new PaintWindow(new JPanel()));
     
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    JScrollPane jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    moreColors.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    Color c = JColorChooser.showDialog(null, "Choose a color", Color.BLACK);
    setColor(c);
     
    }});
      setContentPane(jsp);
     
    }
     
    public void setColor(Color color)
    {
    this.color = color;
    }
     
    public Color getColor()
    {
    return color;
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
    JavaPaintProgram paintProgram = new JavaPaintProgram();
    // String cn = UIManager.getSystemLookAndFeelClassName();
               //  UIManager.setLookAndFeel(cn);
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
     
     
    }
     
     
    public void mouseClicked(MouseEvent e)
    {
     
    }
     
    public void mouseEntered(MouseEvent e)
    {
     
    }
     
    public void mouseExited(MouseEvent e)
    {
     
    }
     
    public void mousePressed(MouseEvent e)
    {
     
    }
     
    public void mouseReleased(MouseEvent e)
    {
     
    }
     
    private class PaintWindow extends JScrollPane
    {
    public PaintWindow(JPanel panel)
    {
    super(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.setBackground(Color.WHITE);
    panel.setVisible(true);
    panel.setSize(3000, 3000);
    panel.addMouseListener(JavaPaintProgram.this);
     
     
    }
     
    }
     
    }

    The set and get Color methods are used to be able, since I'm doing this JColorChooser inside an anonymous class, to be able to apply it to the JPanel object of the JScrollPane.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Ok, I changed it to Component and it neither hurt it nor really made it show up.

    Hmmmmm...by any chance is that problem with the JScrollPane a problem with my layout?

    The thought just occurred to me that that could be what the problem is, and if so, what layout should I be using?

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    What do you want your GUI to look like? That will determine what kind of layout you'll want to use. The scroll bars don't show up because there's no need. The preferred size of whatever is inside the scroll bar is small enough that there's nothing to scroll to. You probably should only be painting inside of the paintComponent method. There might be exceptions, but they are that: the exception, not the rule.

    What I would probably do is keep a BufferedImage that you can draw to when the MouseListener wants to (use the getGraphics() method on the BufferedImage). Then in your paintComponent method, just draw that buffer out to the graphics argument provided to that method. You'll need to invoke the repaint() method to ensure that paintComponent gets called whenever you're internal BufferedImage gets changed, not just whenever Java determines that the window is "dirty" and needs to be repainted.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Painting as in Microsoft Paint is what I'm referring to.

    However, it probably wouldn't hurt to call paintComponent() all the same.

    However, Graphics still is abstract. I did think of passing the method a DebugGraphics object (which can be instantiated) inside the MouseListener.

    Also, I think you may be right about using a BufferedImage, whatever that is. If I used some kind of image and just had the user edit it, then it'd be easier to save it as a picture extension then if I, for instance, tried to get the contents of the JPanel to be saved as a jpeg.

    Am now going to figure out what in the world you meant by those last lines about the window being dirty.

    Not trying to be rude, but to me the idea of how to fix it to bring out some Windex. I honestly don't know what you mean by "dirty".

    I did one time get a graphics thing to work on another project that sometimes wouldn't refresh instantly or redraw images or would redraw them in the wrong spot and move all my components over. Is that what you meant?

    Also, what about Graphics2D? That deals with the color as well as the text of an image, at least I think it does.

    I don't know how to add an image with a jar file.

    I tried putting it in same directory but either I didn't have it where I should have or it won't work.

    It did work when I specially mentioned the whole path name, but if I had an image that could vary in path name as it'd be on other's computers, then I can't use that approach.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    how to add an image with a jar file.
    Are you trying to add the image file to the jar file or what?
    or does the jar file contain classes that add the image to something else?

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Red face Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Add image to jar file.

    I'm not sure what Kevin meant by BufferedImage and dirty windows.

    I am trying something, to see if my plan would work to use a DebugGraphics object to be used to call a paint method from within a listener method.

    I'm trying out one with actionListener right now.

    I should be working but am getting mysterious Null Pointer Exception despite the fact that the file chooser is definitely getting a file name down right.

    Here's what I added to the code I had above:

    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
     
    public class JavaPaintProgram extends JFrame implements MouseListener
    {
     
    private JPanel panel;
    private JToolBar paintToolBar;
    private JMenuBar menubar;
    private JMenu file, help;
    private JMenuItem save, importImage, help2, exit;
    private JButton moreColors;
    private JTabbedPane jtp;
    private JColorChooser colorChooser;
    private Color color;
    public JavaPaintProgram()
    {
    menubar = new JMenuBar();
    file = new JMenu("File");
    help = new JMenu("Help");
    menubar.add(file);
    menubar.add(help);
    save = new JMenuItem("Save");
    importImage = new JMenuItem("Item image from");
    exit = new JMenuItem("Exit");
    file.add(save);
    file.addSeparator();
    file.add(importImage);
    file.addSeparator();
    file.add(exit);
    help2 = new JMenuItem("Help");
    help.add(help2);
    setJMenuBar(menubar);
    paintToolBar = new JToolBar();
     
    moreColors = new JButton("Select Color");
    paintToolBar.add(moreColors);
    paintToolBar.setBackground(Color.YELLOW);
    JButton insertImageFromFile = new JButton("Upload Image From File");
     
    paintToolBar.add(insertImageFromFile);
    insertImageFromFile.setBackground(Color.GREEN);
     
    insertImageFromFile.addActionListener( new ActionListener() 
    {
    public void actionPerformed(ActionEvent e)
    {
     
    JFileChooser jfc = new JFileChooser();
     
    jfc.setApproveButtonText("Upload Image");
     
     
    jfc.showOpenDialog(null);
     
     
    ImageIcon img  = new  ImageIcon(jfc.getSelectedFile().getName());
    System.out.println(img);
     
    Graphics willthiswork = new DebugGraphics();
     
    willthiswork.drawImage( img.getImage(), 200, 200, panel);
     
     
     
    }});
    panel = new JPanel();
    panel.add(paintToolBar);
    setVisible(true);
    panel.setVisible(true);
    jtp = new JTabbedPane(JTabbedPane.TOP);
    panel.add(jtp);
    jtp.addTab("A Tab", new PaintWindow(new JPanel()));
     
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    JScrollPane jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    moreColors.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    Color c = JColorChooser.showDialog(null, "Choose a color", Color.BLACK);
    setColor(c);
     
    }});
      setContentPane(jsp);
     
    }
     
    public void setColor(Color color)
    {
    this.color = color;
    }
     
    public Color getColor()
    {
    return color;
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
    JavaPaintProgram paintProgram = new JavaPaintProgram();
    // String cn = UIManager.getSystemLookAndFeelClassName();
               //  UIManager.setLookAndFeel(cn);
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
     
     
    }
     
     
    public void mouseClicked(MouseEvent e)
    {
     
    }
     
    public void mouseEntered(MouseEvent e)
    {
     
    }
     
    public void mouseExited(MouseEvent e)
    {
     
    }
     
    public void mousePressed(MouseEvent e)
    {
     
    }
     
    public void mouseReleased(MouseEvent e)
    {
     
    }
     
    private class PaintWindow extends JScrollPane
    {
    public PaintWindow(Component panel)
    {
    super(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.setBackground(Color.WHITE);
    panel.setVisible(true);
    panel.setSize(3000, 3000);
    panel.addMouseListener(JavaPaintProgram.this);
     
     
    }
     
    }
     
    }

    I get the weird error message:

     

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.DebugGraphics.drawImage(DebugGraphics. java:1021)
    at JavaPaintProgram$1.actionPerformed(JavaPaintProgra m.java:67)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)





    When I try to click the upload from file button.

    Was my whole plan to "outjerk" the Java Programming Language a good approach or is that what is actually causing the Null Pointer Exception?

    I found that Graphics.this is out and Graphics.getClass() won't work as it's a static method which would supposedly not work for static variables.

    Wish I knew why I ran into things like that.

    I'm thinking that the message:

    "non-static variable (X) cannot be referenced from a static context" means that I can't do this in a non-static method:

    VariableClass variable = SomeOtherClass.callingSomeStaticMethodWhichReturns AnInstanceOfVariableClass();

    Also, I noticed inside static methods, particularly the main method, I cannot have any references to this it seems either.



    I think I found the problem! Hadn't initiated panel yet by the time I had the ActionListener defined.

    Yeah, that might do it.

    Wait, that's still not it, so what is?
    Last edited by javapenguin; May 20th, 2011 at 04:23 PM. Reason: Still having the problem.

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    To stop making a very long post, I'm going to another post.

    I've just noticed something else. It says that I have to turn off double buffering to use DebugGraphics.

    I'm not even quite sure what DebugGraphics is. All I know is that it's a child class of Graphics and it's also not abstract so that I can use polymorphism to make it a Graphics object so I can avoid the trap of not being able to instantiate a Graphics object.

    I tried the new lines:

    RepaintManager repaintManager = RepaintManager.currentManager(component);
    repaintManager.setDoubleBufferingEnabled(false);

    Ok, I changed the component to panel. Silly mistake on my part.

    However, I think the variable willthiswork is still null!

    How come!

    I found that panel and the image aren't null and are all valid so why is it null?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Sorry, I can't follow your ramblings. Do you have some questions to ask, perhaps with compiler error messages.
    at JavaPaintProgram$1.actionPerformed(JavaPaintProgra m.java:67)
    look at the code on line 67. What variable is null?

  12. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    I believe that the variable willthiswork is null.

    I tried printing it and it threw a Null Pointer Exception so that proves it.

    The question is....why is it null?

    I instantiated it.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    I tried printing it and it threw a Null Pointer Exception so that proves it.
    Not true.
    Object obj = null;
    System.out.println("obj=" + obj);

    would print: obj=null

  14. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    I had thought so, but it appears that the println statement is throwing the error.

    To make this more bizzare, I recopied the code into the class's paint method and called it and instead of making an image, I got a ton of JFileChoosers coming up!

    What's the deal with that?

    Do you know what is null or going on here?

    I'm totally confused now.



    Ok I think I figured out the problem why the File Choosers were attacking.

    Perhaps paint method is called lots of times and since I had a JFileChooser coming up in the paint method, it made a JFileChooser every time paint method was called.
    Last edited by javapenguin; May 20th, 2011 at 05:06 PM.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    The NPE is happening inside of the DebugGraphics object.
    Read the API doc on how to use that class. I've never heard of it until now.

  16. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (May 20th, 2011)

  17. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Ok, I altered it to avoid that unknown class altogether.

    But now it got rid of Null Pointer message but still my image must be being drawn in invisible ink.

    How do I make it show up?

    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
     
    public class JavaPaintProgram extends JFrame implements MouseListener
    {
     
    private JPanel panel;
    private JToolBar paintToolBar;
    private JMenuBar menubar;
    private JMenu file, help;
    private JMenuItem save, importImage, help2, exit;
    private JButton moreColors;
    private JTabbedPane jtp;
    private JColorChooser colorChooser;
    private Color color;
    private File f;
    public JavaPaintProgram()
    {
    menubar = new JMenuBar();
    file = new JMenu("File");
    help = new JMenu("Help");
    menubar.add(file);
    menubar.add(help);
    save = new JMenuItem("Save");
    importImage = new JMenuItem("Item image from");
    exit = new JMenuItem("Exit");
    file.add(save);
    file.addSeparator();
    file.add(importImage);
    file.addSeparator();
    file.add(exit);
    help2 = new JMenuItem("Help");
    help.add(help2);
    setJMenuBar(menubar);
    paintToolBar = new JToolBar();
     
    moreColors = new JButton("Select Color");
    paintToolBar.add(moreColors);
    paintToolBar.setBackground(Color.YELLOW);
    JButton insertImageFromFile = new JButton("Upload Image From File");
     
    paintToolBar.add(insertImageFromFile);
    insertImageFromFile.setBackground(Color.GREEN);
     
    panel = new JPanel();
    System.out.println(panel.getDebugGraphicsOptions());
     
     
    insertImageFromFile.addActionListener( new ActionListener() 
    {
    public void actionPerformed(ActionEvent e)
    {
     
    JFileChooser jfc = new JFileChooser();
     
    jfc.setApproveButtonText("Upload Image");
     
     
    jfc.showOpenDialog(null);
     
    setFile(jfc.getSelectedFile());
    ImageIcon img  = new  ImageIcon(jfc.getSelectedFile().getName());
    System.out.println(img);
     
    JavaPaintProgram temp = JavaPaintProgram.this;
     
    Graphics willthiswork = temp.getGraphics();
     
     
     
     
    System.out.println(img.getImage());
    System.out.println(panel);
     System.out.println(willthiswork);
     
    willthiswork.drawImage( img.getImage(), 200, 200, panel);
     
     
     
    }});
     
    panel.add(paintToolBar);
    setVisible(true);
    panel.setVisible(true);
    jtp = new JTabbedPane(JTabbedPane.TOP);
    panel.add(jtp);
    jtp.addTab("A Tab", new PaintWindow(new JPanel()));
     
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    JScrollPane jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    moreColors.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    Color c = JColorChooser.showDialog(null, "Choose a color", Color.BLACK);
    setColor(c);
     
    }});
      setContentPane(jsp);
     
    }
     
    public void setColor(Color color)
    {
    this.color = color;
    }
     
    public void setFile(File f)
    {
     
    this.f = f;
    }
     
    public File getFile()
    {
    return f;
    }
     
     
     
    public Color getColor()
    {
    return color;
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
    JavaPaintProgram paintProgram = new JavaPaintProgram();
    // String cn = UIManager.getSystemLookAndFeelClassName();
               //  UIManager.setLookAndFeel(cn);
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
     
     
     
     
    }
     
     
    public void mouseClicked(MouseEvent e)
    {
     
    }
     
    public void mouseEntered(MouseEvent e)
    {
     
    }
     
    public void mouseExited(MouseEvent e)
    {
     
    }
     
    public void mousePressed(MouseEvent e)
    {
     
    }
     
    public void mouseReleased(MouseEvent e)
    {
     
    }
     
    private class PaintWindow extends JScrollPane
    {
    public PaintWindow(Component panel)
    {
    super(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.setBackground(Color.WHITE);
    panel.setVisible(true);
    panel.setSize(3000, 3000);
    panel.addMouseListener(JavaPaintProgram.this);
     
     
    }
     
    }
     
    }

  18. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    I tried painting a component and it won't paint.

    I think I'm having trouble understanding how to use paint and stuff.

    I have a better code but now everything compiles without exceptions but it's not doing what I'd like it to do.

    Why won't it paint that Button?

    Also, why isn't the image being drawn as I know the method has to be working now...at least...the null pointer exception is gone.

    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
     
    public class JavaPaintProgram extends JFrame implements MouseListener
    {
     
    private JPanel panel;
    private JToolBar paintToolBar;
    private JMenuBar menubar;
    private JMenu file, help;
    private JMenuItem save, importImage, help2, exit;
    private JButton moreColors;
    private JTabbedPane jtp;
    private JColorChooser colorChooser;
    private Color color;
    private File f;
    public JavaPaintProgram()
    {
    menubar = new JMenuBar();
    file = new JMenu("File");
    help = new JMenu("Help");
    menubar.add(file);
    menubar.add(help);
    save = new JMenuItem("Save");
    importImage = new JMenuItem("Item image from");
    exit = new JMenuItem("Exit");
    file.add(save);
    file.addSeparator();
    file.add(importImage);
    file.addSeparator();
    file.add(exit);
    help2 = new JMenuItem("Help");
    help.add(help2);
    setJMenuBar(menubar);
    paintToolBar = new JToolBar();
     
    moreColors = new JButton("Select Color");
    paintToolBar.add(moreColors);
    paintToolBar.setBackground(Color.YELLOW);
    JButton insertImageFromFile = new JButton("Upload Image From File");
     
    paintToolBar.add(insertImageFromFile);
    insertImageFromFile.setBackground(Color.GREEN);
     
    panel = new JPanel();
    System.out.println(panel.getDebugGraphicsOptions());
     
     
     
    panel.add(paintToolBar);
    setVisible(true);
    panel.setVisible(true);
    jtp = new JTabbedPane(JTabbedPane.TOP);
    panel.add(jtp);
     PaintWindow atemp = new PaintWindow(new JPanel());
    atemp.setVisible(false);
     
     
    CircleJButton cjb = new CircleJButton("Draw a Circle");
     
    paintToolBar.add(cjb);
     
    jtp.addTab("A Tab", atemp);
     
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    JScrollPane jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    moreColors.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    Color c = JColorChooser.showDialog(null, "Choose a color", Color.BLACK);
    setColor(c);
     
    }});
     
    insertImageFromFile.addActionListener( new ActionListener() 
    {
    public void actionPerformed(ActionEvent e)
    {
     
    JFileChooser jfc = new JFileChooser();
     
    jfc.setApproveButtonText("Upload Image");
     
     
    jfc.showOpenDialog(null);
     
    setFile(jfc.getSelectedFile());
    ImageIcon img  = new  ImageIcon(jfc.getSelectedFile().getName());
    System.out.println(img);
     
    JavaPaintProgram temp = JavaPaintProgram.this;
     
    Graphics willthiswork = temp.getGraphics();
     
     
     
     
    System.out.println(img.getImage());
    System.out.println(panel);
     System.out.println(willthiswork);
     
    willthiswork.drawImage( img.getImage(), 200, 200, Color.GREEN, panel);
     
     
     
    }});
     
      setContentPane(jsp);
     
    }
     
     
    public void setColor(Color color)
    {
    this.color = color;
    }
     
    public void setFile(File f)
    {
     
    this.f = f;
    }
     
    public File getFile()
    {
    return f;
    }
     
     
     
    public Color getColor()
    {
    return color;
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
    JavaPaintProgram paintProgram = new JavaPaintProgram();
    // String cn = UIManager.getSystemLookAndFeelClassName();
               //  UIManager.setLookAndFeel(cn);
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
     
     
     
     
    }
     
     
    public void mouseClicked(MouseEvent e)
    {
     
    }
     
    public void mouseEntered(MouseEvent e)
    {
     
    }
     
    public void mouseExited(MouseEvent e)
    {
     
    }
     
    public void mousePressed(MouseEvent e)
    {
     
    }
     
    public void mouseReleased(MouseEvent e)
    {
     
    }
     
    private class PaintWindow extends JScrollPane
    {
    public PaintWindow(Component panel)
    {
    super(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.setBackground(Color.WHITE);
    panel.setVisible(true);
    panel.setSize(3000, 3000);
    panel.addMouseListener(JavaPaintProgram.this);
     
     
    }
     
    }
     
    private class CircleJButton extends JButton
    {
     
    public CircleJButton(String title)
    {
    super(title);
    setSize(100,100);
     
    }
     
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawOval(this.getX(), this.getY(), 30,30);
    g.setColor(Color.RED);
     
    }
    }
     
     
     
    }

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Why won't it paint that Button?
    When I compile and execute the code all I get is the title bar???
    Is that all that is supposed to show?

  20. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    The first example at the very beginning was a simpler example.

    The one I'm talking about now is far more complex.

    Sorry for the confusion.

    I was able to solve the original problem, I think, but now have others.

    I found that many classes like JFrame have a getGraphics() method for me.

    However, I thought I heard somewhere (like in the last few hours) that using that approach was a bad idea but it seems to compile and run, though none of the paint or paint components or Graphics methods are working.

    Inside the listener for the button that has you upload an image for a file, I know it's getting the Image and the objects are all not Null and are fine, or appear to be, but nothing is being drawn!

    Also, I know my code in my private class that handles the button that will draw a Circle, it's not drawing a Circle or even an oval for that matter around the Button component like it should be.

    I'm not sure what I'm doing wrong.


  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    When I compile and execute the code all I get is the title bar???
    Is that all that is supposed to show?

    Did your last post answer this question?

  22. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    There should be more. Not sure what you mean by "only title bar".

    Here is what I have now:

    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
     
    public class JavaPaintProgram extends JFrame implements MouseListener
    {
     
    private JPanel panel;
    private JToolBar paintToolBar;
    private JMenuBar menubar;
    private JMenu file, help;
    private JMenuItem save, importImage, help2, exit;
    private JButton moreColors;
    private JTabbedPane jtp;
    private JColorChooser colorChooser;
    private Color color;
    private File f;
    public JavaPaintProgram()
    {
    menubar = new JMenuBar();
    file = new JMenu("File");
    help = new JMenu("Help");
    menubar.add(file);
    menubar.add(help);
    save = new JMenuItem("Save");
    importImage = new JMenuItem("Item image from");
    exit = new JMenuItem("Exit");
    file.add(save);
    file.addSeparator();
    file.add(importImage);
    file.addSeparator();
    file.add(exit);
    help2 = new JMenuItem("Help");
    help.add(help2);
    setJMenuBar(menubar);
    paintToolBar = new JToolBar();
     
    moreColors = new JButton("Select Color");
    paintToolBar.add(moreColors);
    paintToolBar.setBackground(Color.YELLOW);
    JButton insertImageFromFile = new JButton("Upload Image From File");
     
    paintToolBar.add(insertImageFromFile);
    insertImageFromFile.setBackground(Color.GREEN);
     
    panel = new JPanel();
    System.out.println(panel.getDebugGraphicsOptions());
     
     
     
    panel.add(paintToolBar);
    setVisible(true);
    panel.setVisible(true);
    jtp = new JTabbedPane(JTabbedPane.TOP);
    panel.add(jtp);
     PaintWindow atemp = new PaintWindow(new JPanel());
    atemp.setVisible(false);
     
     
    CircleJButton cjb = new CircleJButton("Draw a Circle");
     
    paintToolBar.add(cjb);
     
    jtp.addTab("A Tab", atemp);
     
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    JScrollPane jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    moreColors.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    Color c = JColorChooser.showDialog(null, "Choose a color", Color.BLACK);
    setColor(c);
     
    }});
     
    insertImageFromFile.addActionListener( new ActionListener() 
    {
    public void actionPerformed(ActionEvent e)
    {
     
    JFileChooser jfc = new JFileChooser();
     
    jfc.setApproveButtonText("Upload Image");
     
     
    jfc.showOpenDialog(null);
     
    setFile(jfc.getSelectedFile());
    ImageIcon img  = new  ImageIcon(jfc.getSelectedFile().getName());
    System.out.println(img);
     
    JavaPaintProgram temp = JavaPaintProgram.this;
     
    Graphics willthiswork = temp.getGraphics();
     
     
     
     
    System.out.println(img.getImage());
    System.out.println(panel);
     System.out.println(willthiswork);
     
    willthiswork.drawImage( img.getImage(), 200, 200, Color.GREEN, panel);
     
     
     
    }});
     
      setContentPane(jsp);
     
    }
     
     
    public void setColor(Color color)
    {
    this.color = color;
    }
     
    public void setFile(File f)
    {
     
    this.f = f;
    }
     
    public File getFile()
    {
    return f;
    }
     
     
     
    public Color getColor()
    {
    return color;
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
    JavaPaintProgram paintProgram = new JavaPaintProgram();
    // String cn = UIManager.getSystemLookAndFeelClassName();
               //  UIManager.setLookAndFeel(cn);
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
     
     
     
     
    }
     
     
    public void mouseClicked(MouseEvent e)
    {
     
    }
     
    public void mouseEntered(MouseEvent e)
    {
     
    }
     
    public void mouseExited(MouseEvent e)
    {
     
    }
     
    public void mousePressed(MouseEvent e)
    {
     
    }
     
    public void mouseReleased(MouseEvent e)
    {
     
    }
     
    private class PaintWindow extends JScrollPane
    {
    public PaintWindow(Component panel)
    {
    super(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.setBackground(Color.WHITE);
    panel.setVisible(true);
    panel.setSize(3000, 3000);
    panel.addMouseListener(JavaPaintProgram.this);
     
     
    }
     
    }
     
    private class CircleJButton extends JButton
    {
     
    public CircleJButton(String title)
    {
    super(title);
    setSize(100,100);
     
    }
     
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawOval(this.getX(), this.getY(), 30,30);
    g.setColor(Color.RED);
     
    }
     
    public void paint(Graphics g)
    {
    super.paint(g);
    g.setColor(Color.RED);
    g.drawOval(this.getX(), this.getY(), 30,30);
    }
    }
     
     
     
    }

    Unless it's an error in compiler programs.

    I'm wondering something, is it possible for code compiled on JGrasp or NetBeams or BlueJay to not work on Eclipse?

    I mean, assuming it is valid code of course.

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    what you mean by "only title bar"
    The title bar is at the top of the frame. It has the java icon on the left and the window controls on the right.

    Your new code shows that same thing. No contents for the frame.

  24. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    If you mean that I don't have any title for the JFrame or anything in the JPanel object that is attached to the tab, you are correct.

    Though why it won't add an image though it should to the main JPanel panel, I'm not sure.

  25. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    The window I see is about 100 pixels by 20 pixels. There is nothing else shown.
    Attached Images Attached Images

  26. #24
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    Why does my token for this page keep expiring?

    Anyway, I got this when I enlarged it:
    Attached Images Attached Images

  27. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't get a JToolBar, JMenuBar, and a JTabbedPane to all be visible at the same t

    when I enlarged it
    how does it look when it first starts? Does it look like the image I posted?
    It shouldn't start that way.

Page 1 of 2 12 LastLast

Similar Threads

  1. Junit3 error: Implicit super constructor TestCase() is not visible
    By albertkao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 11:53 AM
  2. [SOLVED] How to close all tabs in JTabbedPane?
    By LeonLanford in forum AWT / Java Swing
    Replies: 7
    Last Post: June 28th, 2010, 10:58 AM
  3. Replies: 3
    Last Post: April 14th, 2010, 07:33 PM
  4. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 07:26 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM