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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:40:14+00:00 2026-05-25T12:40:14+00:00

I am trying to learn java and i am practicing with a simple program

  • 0

I am trying to learn java and i am practicing with a simple program with 2 simple buttons. Here is my code :

 import javax.swing.*;

public class Main {

    public static void main(String[] args) {

    JFrame frame = new JFrame("Askhsh 3");    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ColorJPanel application = new ColorJPanel();
    frame.add(application);
    frame.setSize(500,500);
    frame.setVisible(true);

    }

}

And the class ColorJPanel:

import java.awt.*;
import javax.swing.*;


public class ColorJPanel extends JPanel{

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    this.setBackground(Color.WHITE);

    JButton arxikopoihsh = new JButton("Αρχικοποίκηση");

    JButton klhrwsh = new JButton("Κλήρωση");

    add(arxikopoihsh);
    add(klhrwsh);    

    this.revalidate();
    this.repaint();

    }
}

As you can see the only thing i want to do for now is to place 2 simple buttons that do nothing! Here is my output:
http://imageshack.us/photo/my-images/847/efarmogh.jpg/
When i am running the application i am seeing the buttons filling the window!
Note that if i remove the “this.revalidate();” command i have to resize the window to see the buttons !
Thanks very much for your time 🙂

  • 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-05-25T12:40:14+00:00Added an answer on May 25, 2026 at 12:40 pm

    Don’t add components in paintComponent. This method is for painting only, not for program logic or to build GUI’s. Know that this method gets called many times, often by the JVM and most of the time this is out of your control, and also know that when you ask for it to be called via the repaint() method, this is only a suggestion and the paint manager may sometimes choose to ignore your request. The paintComponent method must be lean and fast as anything that slows it down will slow down the perceived responsiveness of your application.

    In your current code, I don’t even see a need to have a paintComponent method override, so unless you need it (if doing for instance custom painting of the component), I suggest that you get rid of this method (and the calls to repaint and revalidate). Instead, add your components in the class’s constructor and make sure to pack your top level container after adding components and before calling setVisible(true). Most important — read the Swing tutorials as this is all covered there.

    e.g.,

    Main.java

    import javax.swing.*; 
    
    public class Main { 
    
      public static void main(String[] args) { 
    
        JFrame frame = new JFrame("Askhsh 3"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        ColorJPanel application = new ColorJPanel(); 
        frame.add(application); 
        frame.pack();
        frame.setVisible(true); 
      } 
    }
    

    ColorJPanel.Java

    import java.awt.*; 
    import javax.swing.*; 
    
    public class ColorJPanel extends JPanel{ 
      public static final int CJP_WIDTH = 500;
      public static final int CJP_HEIGHT = 500;
    
      public ColorJPanel()  { 
        this.setBackground(Color.WHITE); 
        JButton arxikopoihsh = new JButton("Αρχικοποίκηση"); 
        JButton klhrwsh = new JButton("Κλήρωση"); 
        add(arxikopoihsh); 
        add(klhrwsh); 
      } 
    
      // let the component size itself
      public Dimension getPreferredSize() {
        return new Dimension(CJP_WIDTH, CJP_HEIGHT); 
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

greetngs, i am trying to learn Java and Swing by writing a simple game
I am trying to learn Java. I would like to implement a simple networked
Greetings. I am trying to learn Java and Swing (today is my first day).
Actually i had been practicing some of the java programs today(trying to learn it
I am trying to learn how to specify class constructors in Java. I am
I am trying to learn Java by doing some assignments from a Stanford class
I am a Java developer trying to learn Class creation in Scala. I create
I am trying to learn more about java. This program is an attempt to
So, I'm trying to learn Java Swing and custom components. I've created a JFrame,
I trying to learn how to use Java Swing's GroupLayout. First of all I

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.