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 9122497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:07:05+00:00 2026-06-17T06:07:05+00:00

I created two classes which one extending. I create an object, but a file

  • 0

I created two classes which one extending. I create an object, but a file is not appearing (method work fine, because title form draw() method appears. There is all code:

public class Main_class extends JFrame implements ActionListener{
 //**************************// 
 public static void main(String[] args) {
       java.awt.EventQueue.invokeLater(new Runnable(){
 public void run(){
 new Main_class().setVisible(true);
 }
 });
}
 //**************************// 
 JPanel panel;

 JMenuBar mbar; 
 JMenuItem item;

 JMenuItem open;
 JMenu file;
 BufferedImage my_image;


public Main_class(){
setSize(800, 600);
setTitle("TEST");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel=new JPanel();

mbar=new JMenuBar();
setJMenuBar(mbar);

    file=new JMenu("File");
    mbar.add(file);

    open=new JMenuItem("Open");
    open.addActionListener(this);
    file.add(open);      
 }

@Override
public void actionPerformed(ActionEvent e) {
 String zrodlo=e.getActionCommand();
 image_class k=new image_class();
    if(zrodlo.equals("Open")) try {
        k.load(my_image);
    } catch (IOException ex) {
        Logger.getLogger(Main_class.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

///////////////////////////////////////////

public class image_class extends Main_class{

public void load(BufferedImage my_image) throws IOException{
  JFileChooser open_chooser=new JFileChooser("//");
  FileNameExtensionFilter rast=new FileNameExtensionFilter("Pliki grafiki rastrowej(.jpeg,.png.,gif...)", "jpeg","jpg", "gif","png","bmp");
  open_chooser.setFileFilter(rast);
  int a=open_chooser.showOpenDialog(null);

  if(a==JFileChooser.APPROVE_OPTION){
  String image_name=open_chooser.getSelectedFile().getAbsolutePath();
  String roz=image_name.substring(image_name.lastIndexOf('.')+1);
  my_image=ImageIO.read(open_chooser.getSelectedFile());               
  draw();    
}
}

public void draw(){  
 panel=new JPanel(){
        protected void paintComponent(Graphics g){
            Graphics g2 = g.create();
            g2.drawImage(my_image, 0, 0, getWidth(), getHeight(), null);
            g2.dispose();             
        }         
 };   
 panel.setBounds(0, 0, 200, 200);
 add(panel);
 revalidate();  
 repaint();   
 System.out.print("LOADED!!!!!!");
 }
 }
  • 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-17T06:07:06+00:00Added an answer on June 17, 2026 at 6:07 am

    One of your main problems is that you’re misusing inheritance. Your code here from image_class.java:

    public void draw() {
      panel = new JPanel() {
         protected void paintComponent(Graphics g) {
            Graphics g2 = g.create();
            g2.drawImage(my_image, 0, 0, getWidth(), getHeight(), null);
            g2.dispose();
         }
      };
      panel.setBounds(0, 0, 200, 200);
      add(panel);
      revalidate();
      repaint();
      System.out.print("LOADED!!!!!!");
    }
    

    You are adding the new JPanel to a Main_class instance, but not the one that is displayed but rather to the one that image_class inherits from. These are two completely distinct objects and making changes to one will not affect the other.

    The solution is not to misuse inheritance for this but rather to display the image in the original GUI.

    Also, you should never dispose of a Graphics object that was given to you from the JVM as this can have nasty side effects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created two classes in netbeans;One of them is a JPanel form and another
I have created two viewController classes such that one is superclass of another.i have
Say I have two classes created work and workItem. CWorker *work = new CWorker();
I have two classes, one of which is a subclass of another, and differs
I've created two classes with methods with same name. In one of them it
I have created two classes, one for StockHoldings, and the other Portfolio (an array).
I have two classes with many-to-many relation so I created a join table in-between
I have two model classes, say parent and child. Parents are created first, and
I have implemented two classes, isbn10 and isbn13. I would like to create a
I created two very simple Heroku apps to test out the service, but it's

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.