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 2 of 2 FirstFirst 12
Results 26 to 34 of 34

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

  1. #26
    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

    It looks like the one you posted when you first started, yes.

    How do I fix that?

    In addition to that, apparently I don't know how to use getGraphics().

    I tried it in another test program and called a method that drew something and called that method, passing it the result of the getGraphics(), which by the way I had to use the JFrame child class (the main program) to call that getGraphics() as the one for the JPanel I set as the content pane returned a null result), well anyway, it wouldn't draw anything. So I put it in a paint() method and suddenly it works.

    But people have been able to use other methods besides paint() to draw things. I've seen it before. Don't recall quite what they did (otherwise I wouldn't be having this problem right now perhaps), but anyway, that appears to be the same thing going wrong in the above code that produces the window I just posted above.

    The funny thing is, when I saw people do other methods that used Graphics, they often called repaint(), whatever that is, at the end of their method. But I think I tried that and it didn't know what the method was or something.

    Maybe I misspelled something.

    in the one above, not my test program, I have called paintComponent() for a JButton child class and it isn't drawing anything.

    Perhaps I don't know what paintComponent() does, though I thought it drew on the component.

  2. #27
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

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

    You need to understand how Swing paints the GUI. You shouldn't need to be calling getGraphics() unless you're doing some really gnarly stuff, and you shouldn't be trying to draw on the graphics outside a paint method.

    See Painting in AWT & Swing.

    It would really help if you properly indented the code you post, and explain exactly what you want your GUI to look like and how it should work.
    Last edited by dlorde; May 23rd, 2011 at 10:05 AM.

  3. The Following 2 Users Say Thank You to dlorde For This Useful Post:

    javapenguin (May 23rd, 2011), KevinWorkman (May 23rd, 2011)

  4. #28
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    Quote Originally Posted by javapenguin View Post
    In addition to that, apparently I don't know how to use getGraphics().
    If you're calling getGraphics() on a Component, you're already doing something wrong. Haven't I told you that in the past? Check out the link and advice that dlorde gave you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #29
    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

    But how do people have different methods to draw something?

    Also, paint seems to be being called repeatedly.

    Also, another funny thing, I don't quite get how an abstract method from Graphics could actually draw something.

    Also, it was someone else you told that getGraphics() was a bad idea, and I thought it was only for some certain situation.

    I only used getGraphics() because I can't instantiate a Graphics object, though I have tried

    DebugGraphics dbg = new DebugGraphics();

    and then used it to call a method, but I kept getting a Null Pointer Exception.

    getGraphics() wasn't throwing any compiler errors or run errors, unless I tried it with the JPanel, then it was as apparently the JPanel didn't have any graphics or something like that.

    Anyway, as for how in the world could all of those abstract methods do something when called makes me wonder if JComponent or something defined those methods or something.

    Or if the JFrame or whatever is calling the Graphics subclass DebugGraphics.

    I mean, is paint() the only method to draw, besides paintComponent()?

    However, I needed to call paint() method in the previous bit of code inside an ActionListener and don't know how to do it without getGraphics(), which won't draw it anyway.

    I'll look at that link.

  6. #30
    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

    Got some things to work, but the image still isn't being drawn when I call getImage().

    Also, how do I deal with the Null Pointer Exception?

    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());
     
     
    repaint();
     
     
    }});
     
      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);
    ImageIcon img  = new  ImageIcon(getFile().getName());
    System.out.println(img);
     
     
     
     
     
     
    System.out.println(img.getImage());
    System.out.println(panel);
     g.drawImage( img.getImage(), 200, 200, Color.GREEN,this);
     
     
    }
     
     
    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.setColor(Color.RED);
     
    g.drawOval(5, 5, 22,22);
     
    }
     
     
    }
     
     
     
    }

    I know what's causing it. Paint apparently is called automatically by class.

  7. #31
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

    A few pointers:
    a) Fix your code indentation - very hard to read even in the code tags
    b) Null Pointer - the exception should indicate the very line it is thrown on. When is the File f variable every instantiated? Not before you try and access it in the paint method (hint, hint)
    c) You should override the paintComponent method rather than the paint method - and do so in a lightweight component (eg JPanel)
    d) Call pack on a JFrame after adding components to its Content Pane - this will help layout and size the JFrame appropriately.
    e) To repaint a component, call repaint() rather than trying to get the graphics object and paint to that.

    The majority of the info above is in the java tutorials, which I suggest you read, re-read, and revisit time and again to drive the points of the documentation home.

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

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

    I know what's throwing it. It is trying to draw an image that initially won't be defined till I hit a button.

    I was asking how to fix the repaint() call.

    setFile() should instantiate it.

    My printlines prove that it's recognizing the file name.

    And that the icon isn't returning null either.

    So for paint, I shouldn't call super?

    Also, even if you said that I shouldn't use other methods for painting besides paint() and paintComponent() and paintBorder(), etc, can I have the paint method call other methods, at certain times and not every time I minimize of maximize a window, etc?
    Last edited by javapenguin; May 23rd, 2011 at 03:01 PM.

  9. #33
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    Quote Originally Posted by javapenguin View Post
    But how do people have different methods to draw something?
    I'm not really sure what you're talking about exactly, but they probably have Objects that know how to draw themselves, given a Graphics parameter. Then they call those methods from paintComponent().

    Quote Originally Posted by javapenguin View Post
    Also, paint seems to be being called repeatedly.
    Yep. You have no control over when or how often the paint methods are called. That's one of the many reasons you shouldn't use getGraphics().

    Quote Originally Posted by javapenguin View Post
    Also, another funny thing, I don't quite get how an abstract method from Graphics could actually draw something.
    What you're actually working with is Graphics2D, a subclass of Graphics that defines those abstracts methods. That's why you can cast the Graphics Object passed into paintComponent() to Graphics2D.

    Quote Originally Posted by javapenguin View Post
    Also, it was someone else you told that getGraphics() was a bad idea, and I thought it was only for some certain situation.
    Fair enough. But it's for almost every instance- the only case I can really think of right now is if you're drawing to an image- that's why I said it's wrong to call getGraphics() on a Component.

    Quote Originally Posted by javapenguin View Post
    I only used getGraphics() because I can't instantiate a Graphics object, though I have tried

    DebugGraphics dbg = new DebugGraphics();

    and then used it to call a method, but I kept getting a Null Pointer Exception.
    No. No no no. You get the Graphics instance because it is passed into paintComponent(). Don't try to get it in any other way.

    Quote Originally Posted by javapenguin View Post
    getGraphics() wasn't throwing any compiler errors or run errors, unless I tried it with the JPanel, then it was as apparently the JPanel didn't have any graphics or something like that.
    It won't necessarily throw errors, but it will absolutley result in erratic behavior.

    Quote Originally Posted by javapenguin View Post
    Anyway, as for how in the world could all of those abstract methods do something when called makes me wonder if JComponent or something defined those methods or something.
    Huh? How would that make any sense? They are defined in a subclass of Graphics, which is what an abstract method is- JComponent is not a subclass of Graphics. Graphics2D is.

    Quote Originally Posted by javapenguin View Post
    Or if the JFrame or whatever is calling the Graphics subclass DebugGraphics.
    Close. Graphics2D. Try it out.

    Quote Originally Posted by javapenguin View Post
    I mean, is paint() the only method to draw, besides paintComponent()?
    Use paintComponent(). Save yourself a headache.

    Quote Originally Posted by javapenguin View Post
    However, I needed to call paint() method in the previous bit of code inside an ActionListener and don't know how to do it without getGraphics(), which won't draw it anyway.
    No. You almost absolutely do not have to "call paint()". You should call repaint() and override paintComponent() to do your painting. Simple as that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. The Following User Says Thank You to KevinWorkman For This Useful Post:

    javapenguin (May 24th, 2011)

  11. #34
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

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

    Quote Originally Posted by javapenguin View Post
    But how do people have different methods to draw something?
    They override the paintComponent method of the component they want to draw on.

    Also, paint seems to be being called repeatedly.
    Swing calls the paint methods whenever it determines repainting may be necessary.

    Also, another funny thing, I don't quite get how an abstract method from Graphics could actually draw something.
    Via polymorphism. A Graphics subclass is provided that implements that method.

    Also, it was someone else you told that getGraphics() was a bad idea, and I thought it was only for some certain situation.

    I only used getGraphics() because I can't instantiate a Graphics object
    You should not be trying to instantiate a Graphics object. Swing calls the paint methods and passes them a Graphics object that you can use when you override those methods.

    I mean, is paint() the only method to draw, besides paintComponent()?
    You will get nowhere without understanding how Swing GUI painting works. Read the link provided, and read this tutorial: Custom Painting, especially A Closer Look.

    However, I needed to call paint() method in the previous bit of code inside an ActionListener and don't know how to do it without getGraphics(), which won't draw it anyway.
    You clearly have no idea what you need to do.

    I'll look at that link.
    You need to understand what it says. Also, try following the examples in the tutorial that show you how painting works.

  12. The Following User Says Thank You to dlorde For This Useful Post:

    KevinWorkman (May 26th, 2011)

Page 2 of 2 FirstFirst 12

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, 12:53 PM
  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