Solved

my button do not appear but when I resize my window the button appears

Posted on 2002-07-23
7
231 Views
Last Modified: 2010-03-31
I am beginner in java(SWING) but I have got a small problem with repaint() or refresh(). When I try to Add a new JButton during execution, my button do not appear but when I resize my window the button appears.  can I execute this refresh manually (howto)?    Thank in advance.    bye
0
Comment
Question by:zoltix
7 Comments
 
LVL 2

Accepted Solution

by:
flumpman earned 50 total points
ID: 7171806
Try revalidating the container that you are adding the button to. (public void revalidate() in JComponent)

--
flumpman
0
 
LVL 2

Expert Comment

by:s_lavie
ID: 7171821
or
repaint();
0
 

Author Comment

by:zoltix
ID: 7171831
I can't find revalidate() because i use component and Jframe.   In Jframe I found validate and it s Ok.  What is the diffenrence between validate, revalidate?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:s_lavie
ID: 7171841
You should use JComponent and not Component.
For what object you use Component?
Mix of awt and swing objects is a bad idea...
0
 
LVL 2

Expert Comment

by:flumpman
ID: 7171896
s_lavie is correct, that you shouldn't mix Components and JComponents.  However, when you say you are using a Component, is this the contentPane of the JFrame (java.awt.Component->java.awt.Container)?

If so, you can create a new JPanel and set the contentPane to be the JPanel you created.

Have a look at the following code example.  It shows a JFrame and adds a button which allows you to add another button which adds others ad infinitum...

Disclaimer: this is just an example it is not production worthy code :^)

------

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class AddButtonExample extends JFrame {
     
     JPanel mainPanel;
     JButton addButton;
     
     public AddButtonExample(String title) {
          super(title);
         
          addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                    System.exit(0);
               }
          });
         
          mainPanel = new JPanel();
          setContentPane(mainPanel);
         
          addButton = new JButton("Add JButton");
         
          addButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                    JButton anotherButton = new JButton("Add another");
                    mainPanel.add(anotherButton);
                    anotherButton.addActionListener(this);
//                    mainPanel.repaint();
                    mainPanel.revalidate();
               }
          });
         
          mainPanel.add(addButton);
     }
     
     public static void main(String[] args) {
          AddButtonExample example = new AddButtonExample("Add Button Example");
          example.setSize(400, 200);
          example.setVisible(true);
     }
}


Note that repaint doesn't cause the new JButton to be shown - revalidate does

--
flumpman
0
 

Expert Comment

by:ucero007
ID: 7174103
Make the component invisible and visible

  //
  component.setVisible(false);
  component.setVisible(true);
  //
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7174186
use setVisible(true), show().
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
triangle challenge 4 79
noX challenge 17 77
mapBully challenge 6 92
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now