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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:34:58+00:00 2026-06-15T06:34:58+00:00

I have a small question – how can I repaint JPanel in the main

  • 0

I have a small question – how can I repaint JPanel in the main class from other classes/files/methods?

The story is, that I have a Swing window, which has a list of methods, which are automatically generated by going through files and classes in the folder, and reading methods with their descriptions (annotations). Now, the thing is that when there comes up a class, from which one method needs to paint, let’s say, a sun using circles and lines, then how can I have it show up in the main window?

I’ve looked (read: Googled) around, and found that JPanel should be convenient, but painting onto it from another class and method, without knowing what would be their names and when would they use the painting, created… difficulties.

I’m also adding the main method, if that’d be of any use..

static int indez = 0;
    protected static JList list; 
    static boolean valitud = false;
    public static JTextArea txtala;
    public static JScrollPane scrl;
    public static JPanel pilt;
    static Map<Integer, String[]> info = new TreeMap<Integer, String[]>();
    static ArrayList<Integer> nrs = new ArrayList<Integer>();
    public static void main(String[] args){
        Iterator<Entry<Integer, String[]>> iterator;
        Entry<Integer, String[]> entry;
        Integer val = 0;

        JFrame f = new JFrame("Praktikumid");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(750, 350);

        JPanel panel = new JPanel();
        pilt = new JPanel(){public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString("This is my custom Panel!", 10, 20);
        }};

        pilt.setVisible(false);
        pilt.setPreferredSize(new Dimension(f.getWidth() - 200, f.getHeight() - 20));
        pilt.setBackground(Color.GRAY);

        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        ActionListener kuular = new ActionListener() {
            public void actionPerformed(ActionEvent tegu) {
                valitud = true;
                try {
                    praxStarter();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        MouseListener hiir = new MouseListener() {  
            public void mouseClicked(MouseEvent e) {
                indez = list.getSelectedIndex();
            }
            @Override
            public void mouseEntered(MouseEvent e) {}
            @Override
            public void mouseExited(MouseEvent e) {}
            @Override
            public void mousePressed(MouseEvent e) {}
            @Override
            public void mouseReleased(MouseEvent e) {}  
        };  

        JLabel kiri = new JLabel("Vali hiirega ja vajuta nupule");

        ArrayList<String> selections = new ArrayList<String>();

        // Iterating over all the items in another method to get the modules

        list = new JList(selections.toArray());
        list.setSelectedIndex(0);
        list.addMouseListener(hiir);

        JScrollPane listScroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        listScroller.setMinimumSize(new Dimension(200, 50));
        listScroller.setMaximumSize(new Dimension(200, f.getHeight() * 1000));

        JButton button = new JButton("Vali");
        button.setActionCommand(Integer.toString(list.getSelectedIndex()));
        button.addActionListener(kuular);

        Font font = new Font("Arial", Font.PLAIN, 12);
        txtala = new JTextArea("<-- Vali sealt");
        txtala.setEditable(false);
        txtala.setMargin(new Insets(5, 8, 0, 0));
        txtala.setFont(font);
        scrl = new JScrollPane(txtala, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrl.setPreferredSize(new Dimension(f.getWidth() - 200, f.getHeight() - 20));

        pilt.setBackground(Color.PINK);
        pilt.setVisible(false);
        pilt.setPreferredSize(new Dimension(f.getWidth() - 200, f.getHeight() - 20));

        GroupLayout.ParallelGroup menu = layout.createParallelGroup();
        GroupLayout.SequentialGroup row = layout.createSequentialGroup();
        GroupLayout.SequentialGroup leftright = layout.createSequentialGroup();
        GroupLayout.ParallelGroup topdown = layout.createParallelGroup();
        GroupLayout.ParallelGroup dbl = layout.createParallelGroup();
        GroupLayout.ParallelGroup dblrow = layout.createParallelGroup();

        menu.addComponent(kiri);
        menu.addComponent(listScroller);
        menu.addComponent(button);
        leftright.addGroup(menu);
        dbl.addComponent(scrl);
        dbl.addComponent(pilt);
        leftright.addGroup(dbl);

        row.addComponent(kiri);
        row.addComponent(listScroller);
        row.addComponent(button);
        topdown.addGroup(row);
        dblrow.addComponent(scrl);
        dblrow.addComponent(pilt);
        topdown.addGroup(dblrow);

        layout.setHorizontalGroup(leftright);
        layout.setVerticalGroup(topdown);

        panel.setComponentZOrder(pilt, 1);
        panel.setComponentZOrder(scrl, 0);

        f.add(panel);
        //f.pack();
        f.setVisible(true);
    }

    public static void praxStarter()  throws Exception{
            // As a note - nrs is the holder for menu indexes
            // info is a Map of those same menu items 
            // And one entry in info is in the form of String[]{name, class.method}
        String[] tekst = new String[2];
        String kls, met;
        Method meetod;

        txtala.setText("");

        tekst = info.get(nrs.get(indez));
        kls = tekst[1].substring(0, tekst[1].indexOf("."));
        met = tekst[1].substring(tekst[1].indexOf(".") + 1, tekst[1].length());

        meetod = Class.forName(kls).getMethod(met);
        meetod.setAccessible(true);
        meetod.invoke(Class.forName(kls).newInstance());
    }

Also, here would be an example of one class drawing (or trying to draw) there:

public class brush extends Applet {
public void paint(Graphics g) {
        int x0 = 150;
        int y0 = 150;
        int r = 100;
        int x, y;
        double t;

        int w = getWidth();
        int h = getHeight();

        x0 = w / 2;
        y0 = h / 2;

        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);

        g.setColor(Color.black);

        for (t = -Math.PI; t < Math.PI; t = t + Math.PI / 16) {
            x = (int) (r * Math.cos(t) + x0);
            y = (int) (r * Math.sin(t) + y0);
            g.drawLine(x0, y0, x, y);
        }
    }
}

Thanks ahead for any answer and advice!

Edit:
Here’s a screenshot:
Screenshots

On the left is regular, print-text-on-screen module. On the right is the text part hidden, and JPanel shown, with the JPanel painted from pilt = new JPanel() part, but without any other succsessful edits outside the main class.

Edit: I’ve tried to draw in multiple ways, trying out solutions searching showed up, e.g.

`txtala.setVisible(false);
scrl.setVisible(false);
pilt.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pilt.setVisible(true);
//bi = new BufferedImage(pilt.getWidth() + 1000, pilt.getHeight() + 1000, BufferedImage.TYPE_INT_ARGB);
Graphics g = pilt.getGraphics();
g.setColor(Color.black);
g.drawLine(0, 0, 1000, 1000);
g.drawString("OLOLOLOLO!", 30, 50);
pilt.paint(g);
pilt.update(g);
//g.dispose();
//repaint();`
  • 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-15T06:34:59+00:00Added an answer on June 15, 2026 at 6:34 am

    Ohkay, so I went the easy way. In stead of using the same pane in the main window, I just crated a new popup and put the frame in that. I know, it’s not the bestest of solutions, but hay, better than nothing. If I manage to figure out a way to do it without a popup, then I’m going to post it here. Until then, good luck y’all!

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

Sidebar

Related Questions

I have small question about pdp-11(simulator), I have this command (it begins from the
I have small design question about html.DropDownListFor() How can I change width of html.DropDownListFor()
I have a small question: For example I'm using System.IO.File.Copy() method from .NET Framework.
I have a small question about dojo: I have a div: <div id=image class=classImage>
I have a small question, if anybody could help me. Why I can't see
I have a small question. How can I test an application which uses the
I have a small question, doubt: If I have a website, where I can't
I have a small question. What return common binary reading methods on the end
Just a small question. Say I have the following HTML: <div class=foo> {text} </div>
Ok I have a small question. I have the following <div><span class=spanright onclick>Update</span><label class=myinfo><b>Business

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.