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.

Results 1 to 10 of 10

Thread: How to click a button and have a line drawn on a panel in a frame.

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to click a button and have a line drawn on a panel in a frame.

    Hello!

    I'm a VB.net developer that is trying to learn Java. I am using IntelliJ on Windows 10.

    Using Swing, I have created a simple JFrame with 2 buttons, Render and OK. There is also a JPanel in the center of the frame. Clicking "OK" exits the program. What I would like to do is click "Render" and have a line drawn onto the JPanel.

    This is the code I have:

    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Graphics;
    import java.awt.Color;
     
    public class MainForm extends JPanel{
        private JButton btnOK;
        private JPanel pnlMainPanel;
        private JButton btnRender;
        private JPanel pnlImage;
        private static int width = 800;
        private static int height = 600;
     
        public MainForm() {
     
            btnOK.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
     
            btnRender.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
     
                }
            });
        }
     
        public static void main(String[] args) {
     
            JFrame frame = new JFrame("Drawing Test");
     
            frame.setContentPane(new MainForm().pnlMainPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
     
    }

    What I don't know how to do, and cant figure out, is how to invoke the JPanel's paintComponent() from the action listener of the Render button. Thanks in advance for any help and/or suggestions.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    how to invoke the JPanel's paintComponent()
    You don't call the paintComponent() directly. It is called by the JVM when the component needs to be painted or after the repaint method has been called.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    How long have you been trying to learn Java? I ask this because there is quite a bit to learn before doing painting or any graphics related programming.

    Regards,
    Jim

  4. #4
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    I've had a couple of Java courses for my CIS degree. Java I and Java II. Which doesn't mean much.

    All of the examples I see for drawing on a JPanel show the paintComponent function inside of a class that extends JPanel with the drawing instructions inside the paintComponent. So I'm probably not even on the right track. Maybe what I really should be doing is drawing on a buffered image and then displaying that image in the panel.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    Either approach should work. The first one should be easier.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    You might want to start here --> https://docs.oracle.com/javase/tutor...ing/index.html

    Regards,
    Jim

  7. #7
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    So I've changed the code to this:

    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.imageio.ImageIO;
     
    public class JPanelExample {
     
     
     
        public static void main(String[] arguments) throws IOException {
     
            JPanel panel = new JPanel();
     
            BufferedImage image = ImageIO.read(new File("C:\\Temp\\ic342.jpg"));
            JLabel label = new JLabel(new ImageIcon(image));
            panel.add(label);
     
            //Main window
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("JPanel Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Add a button
            JButton btn = new JButton("Render");
            btn.setBounds(260,700,100, 65);
     
            //Add the listener to the JButton
            btn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
     
                }
            });
     
            frame.setLayout(new BoxLayout( frame.getContentPane(), BoxLayout.Y_AXIS));
     
            //Add the Jpanel to the main window
            frame.add(panel);
     
            //Adding button onto the frame
            frame.add(btn);
     
            frame.pack();
            frame.setVisible(true);
     
        }
    }

    How would I add the code in the btn ActionListener to display the buffered image instead of it displaying when the form loads.

    Nevermind - I figured it out. I have to call the frame.pack(); after I call panel.add(label); in the ActionListener.
    Last edited by Krotus; February 1st, 2019 at 02:14 AM. Reason: Resolved

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    Is the code doing what you want now?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    Yes, it is, thanks.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to click a button and have a line drawn on a panel in a frame.

    Ok. The code doesn't do what the title of this thread asks about. The action listener method is empty. There is no code that draws a line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Button not showing on panel?
    By camel-man in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 27th, 2014, 03:08 PM
  2. Replies: 4
    Last Post: December 6th, 2012, 05:21 AM
  3. How to close a frame on a button click ??
    By rk0887 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 22nd, 2012, 08:58 AM
  4. How to increase thickness of line drawn in java applet
    By maz3r in forum Java Theory & Questions
    Replies: 2
    Last Post: August 20th, 2012, 11:28 AM
  5. [???]basic question on frame window panel
    By Codeguess in forum AWT / Java Swing
    Replies: 1
    Last Post: January 12th, 2011, 08:03 AM