Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:17:02+00:00 2026-06-16T13:17:02+00:00

I have a panel with two buttons. I’m trying to insert an image inside

  • 0

I have a panel with two buttons. I’m trying to insert an image inside the panel and I want to draw lines inside the image after clicking on a button. I have used the below code but this doesn’t seem to work.

public class Try_Panel extends JFrame {
  // start attributes
  private JPanel jPanel1 = new JPanel(null, true);
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  // end attributes

  public Try_Panel(String title) {
    // Frame-Init
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 300; 
    int frameHeight = 300;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);
    // start components

    jPanel1.setBounds(48, 24, 209, 145);
    jPanel1.setOpaque(false);
    cp.add(jPanel1);
    jButton1.setBounds(88, 208, 75, 25);
    jButton1.setText("jButton1");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton1_ActionPerformed(evt);
      }
    });
    cp.add(jButton1);
    jButton2.setBounds(184, 208, 75, 25);
    jButton2.setText("jButton2");
    jButton2.setMargin(new Insets(2, 2, 2, 2));
    jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton2_ActionPerformed(evt);
      }
    });
    cp.add(jButton2);
    // end components

    setVisible(true);
  } // end of public Try_Panel

  // start methods
  public void jButton1_ActionPerformed(ActionEvent evt) {
      BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        JLabel l=new JLabel(new ImageIcon(image));
        Graphics graphics = image.getGraphics();
        Graphics2D g = (Graphics2D) graphics;
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setColor(Color.BLUE);
        g.drawLine(0, 0, 300, 400);
        jPanel1.add(l);
  } // end of jButton1_ActionPerformed

  public void jButton2_ActionPerformed(ActionEvent evt) {
    // TODO add your code here
  } // end of jButton2_ActionPerformed

  // end methods

  public static void main(String[] args) {
    new Try_Panel("Try_Panel");
  } // end of main

} // end of class Try_Panel

The biggest problem is the same code worked in my other class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:17:03+00:00Added an answer on June 16, 2026 at 1:17 pm

    Try wrapping the image inside the ImageIcon AFTER you have updated it. Also, you should also call Graphics#dispose when you are finished rendering to the graphics context.

    public void jButton1_ActionPerformed(ActionEvent evt) {
        BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = image.createGraphics();
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setColor(Color.BLUE);
        g.drawLine(0, 0, 300, 400);
        g.dispose();
        JLabel l=new JLabel(new ImageIcon(image));
        jPanel1.add(l);
    }
    

    You should also rely on the layout managers rather the trying to do it yourself, it will simply make your life easier.

    Personally, I think it would easier to paint directly to a custom component, like JPanel. Check out Performing Custom Painting for more details

    UPDATED with example

    Basically, I changed your example to

    1. Use layout managers
    2. Load the UI within the context of the EDT
    3. revalidate the jPanel1
    public class BadLabel extends JFrame {
        // start attributes
    
        private JPanel jPanel1 = new JPanel(new BorderLayout(), true);
        private JButton jButton1 = new JButton();
        private JButton jButton2 = new JButton();
        // end attributes
    
        public BadLabel(String title) {
            // Frame-Init
            super(title);
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            int frameWidth = 300;
            int frameHeight = 300;
            setSize(frameWidth, frameHeight);
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            int x = (d.width - getSize().width) / 2;
            int y = (d.height - getSize().height) / 2;
            setLocation(x, y);
    //        setResizable(false);
            Container cp = getContentPane();
    //        cp.setLayout(null);
            // start components
    
    //        jPanel1.setBounds(48, 24, 209, 145);
            jPanel1.setOpaque(true);
            jPanel1.setBackground(Color.RED);
            cp.add(jPanel1);
    
            JPanel buttons = new JPanel();
    //        jButton1.setBounds(88, 208, 75, 25);
            jButton1.setText("jButton1");
            jButton1.setMargin(new Insets(2, 2, 2, 2));
            jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    jButton1_ActionPerformed(evt);
                }
            });
            buttons.add(jButton1);
    //        jButton2.setBounds(184, 208, 75, 25);
            jButton2.setText("jButton2");
            jButton2.setMargin(new Insets(2, 2, 2, 2));
            jButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    jButton2_ActionPerformed(evt);
                }
            });
            buttons.add(jButton2);
            // end components
    
            cp.add(buttons, BorderLayout.SOUTH);
    
            setVisible(true);
        } // end of public BadLabel
    
        // start methods
        public void jButton1_ActionPerformed(ActionEvent evt) {
            BufferedImage image = new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g = image.createGraphics();
            g.fillRect(0, 0, image.getWidth(), image.getHeight());
            g.setColor(Color.BLUE);
            g.drawLine(0, 0, 300, 400);
            g.dispose();
            JLabel l = new JLabel(new ImageIcon(image));
            l.setBorder(new LineBorder(Color.BLUE));
            jPanel1.add(l);
            jPanel1.revalidate();
        } // end of jButton1_ActionPerformed
    
        public void jButton2_ActionPerformed(ActionEvent evt) {
            // TODO add your code here
        } // end of jButton2_ActionPerformed
    
        // end methods
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception exp) {
                    }
                    new BadLabel("BadLabel");
                }
            });
        } // end of main
    } // end of class BadLabel}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to put two buttons inside a panel using Swing widgets. Inside the
I have a WinForm application that has a panel and two buttons inside the
I have two panels in update panel. In panel1, there is button. If I
I have the dock panel, two buttons on which docked to left and right
I have two radio buttons both set as async triggers for an update panel
I have a custom JOptionPane with two buttons as follows: AgreementPanel panel = new
I have a base form with two buttons (e.g. OK and Cancel). I want
I have two different buttons viz save and update. I want to open single
I have two buttons added in two different panels, if first button is clicked
i have two panel. when i'm click on the left panel items its calling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.