removeAll not removing at next validate? - java

Can someone explain why the following doesn't work as I expect?
Pressing the button 'should' result in the display only containing the (empty) JScrollPane, ie the input field and button should disappear. However they stay until the component is resized...
public static void main(String[] args)
{
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
Container cp = frame.getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JScrollPane(panel));
Component textField = new JTextField("i am input");
JButton button = new JButton(new AbstractAction("i am pressy")
{
#Override
public void actionPerformed(ActionEvent e)
{
// this is already on the EDT
panel.removeAll();
panel.revalidate();
}
});
panel.setLayout(new FlowLayout());
panel.add(textField);
panel.add(button);
frame.pack();
frame.setVisible(true);
}
Thanks for your help. p.

When updating a visible GUI the code should be:
panel.revalidate();
panel.repaint(); // sometimes needed, this appears to be one of them

The revalidate() method marks components as needing to be laid out, but until something triggers repaint() you won't see any change. Resizing the parent window is one such trigger; switching applications is another. In this previous version, note how setSize() on the panel obviates the need for repaint(). Similarly, this example changes the layout in resetGame().
The article Painting in AWT and Swing goes into more detail.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
/** #see https://stackoverflow.com/questions/5812002 */
public class RevalidateTest {
private static JPanel panel = new JPanel(); // default FlowLayout
private static JTextField text = new JTextField("Text field");
private static JButton clear = new JButton(new AbstractAction("Clear") {
#Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.add(reset);
panel.revalidate();
panel.repaint();
}
});
private static JButton reset = new JButton(new AbstractAction("Reset") {
#Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.add(text);
panel.add(clear);
panel.revalidate();
panel.repaint();
}
});
static void createAndShowGUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(text);
panel.add(clear);
frame.add(panel); // default BorderLayout center
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}

You can execute panel.repaint() as specified in the comment by #Jeremy, however, the UI will still change when you resize the window. The reason being that the removal of the elements from the JPanel will cause the panel to resize. A repaint operation will not cause the panel to resize until the JFrame rechecks its layout (as happens on a window resize).
To make sure that the layout is correctly layed out on a change, you can call frame.validate(). This operation will cause the JFrame to revalidate itself and all child components, which is the same operation that is taking place during a window resize event. To execute this method in your code you would need to change JFrame frame to final, i.e.,
final JFrame frame = new JFrame("test");

Related

Why can't I add a Component to a JFrame with a MouseListener?

I have a JFrame. When I click in the frame, I want to add a Component (in this case an extension of a Canvas). I added a MouseListener to the frame as follows:
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
frame.add(canvas);
frame.repaint();
}
});
However, the component is not added when the mouse is clicked on the frame. I have no problem adding the component in my main method. Adding a print statement in the mouse listener prints correctly, as does removing components from the frame.
Like #sprinter9 said, add frame.pack() before repainting.
Try below
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Frame {
public static void main(String[] args){
final JFrame frame = new JFrame("Add Component");
final Canvas canvas = new Canvas();
canvas.setBackground(Color.BLACK);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
frame.add(canvas);
frame.pack();
frame.repaint();
}
});
frame.setMinimumSize(new Dimension(320,240));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Demo, http://kurungkurawal.com/gifs/frame-add-component.gif
You are adding the component directly to the JFrame. You should use it's contentPane instead:
frame.getContentPane().add(canvas);

why cant i change the position of the panel inside a frame?

I am trying to create a GUI with a couple of buttons and a drawing area.
it seems to be working except the drawing area is very small and not in the right location.
here is my code:
public class ssGUI extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
protected JButton b1, bConnect, bDisconnect, b2;
protected JPanel canvas;
public ssGUI() {
// run button
b1 = new JButton("do something");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
b1.addActionListener(this);
b1.setEnabled(false);
// connect button
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
// disconnect button
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
// clean nmea data button
b2 = new JButton("do something else");
b2.setMnemonic(KeyEvent.VK_E);
b2.addActionListener(this);
b2.setEnabled(false);
// drawing panel
canvas = new JPanel();
canvas.setBackground(Color.white);
add(b1); add(bConnect); add(bDisconnect); add(b2); add(canvas, BorderLayout.CENTER);
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Range Adjustment GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ssGUI newContentPane = new ssGUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setLocation(500, 500);
frame.setSize(500, 500);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}}
this is how my GUI looks like:
as you can see, the "canvas" is very small and at the side of the buttons.
i need it to be placed underneath them and fill frame area.
can anyone help me to resolve that problem?
thank you for your help.
Set your main panel's layout to BorderLayout.
Create a panel just for your buttons and add this panel to your main panel's NORTH position.
Here is a working example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ssGUI extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
protected JButton b1, bConnect, bDisconnect, b2;
protected JPanel canvas;
public ssGUI() {
setLayout(new BorderLayout());
// run button
b1 = new JButton("do something");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
b1.addActionListener(this);
b1.setEnabled(false);
// connect button
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
// disconnect button
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
// clean nmea data button
b2 = new JButton("do something else");
b2.setMnemonic(KeyEvent.VK_E);
b2.addActionListener(this);
b2.setEnabled(false);
// drawing panel
canvas = new JPanel();
canvas.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(b1);
topPanel.add(bConnect);
topPanel.add(bDisconnect);
topPanel.add(b2);
add(topPanel, BorderLayout.NORTH);
add(canvas, BorderLayout.CENTER);
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Range Adjustment GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ssGUI newContentPane = new ssGUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setLocation(500, 500);
frame.setSize(500, 500);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
This is another problem with the layouts.
JPanel has a default layout and I don't see you setting the layout for ssGUI class. Hence the default layout (FlowLayout) will be used.
All components added to ssGUI will be arranged in a linear fashion in a row for as much as it can hold. When your component exceed the width, it will be placed to the next row.
You can consider using a layout for your main panel ssGUI. A GridBagLayout will probably give you what you want.

java frame reprint not working

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class sample {
JTextField field=new JTextField(10);
JPanel panel=new JPanel();
JFrame frame=new JFrame();
String message;
public sample()
{
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
field.setEditable(true);
JButton button=new JButton("press here");
ActionListener listener=new action();
button.addActionListener(listener);
panel.add(field);
panel.add(button);
frame.add(panel);
}
public void getMessage()
{
JTextField f=new JTextField(10);
f.setText(message);
JPanel p=new JPanel();
p.add(f);
frame.add(p);
frame.repaint(); <-- problem here
}
class action implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
message=field.getText();
frame.remove(panel);
getMessage();
}
}
}
//The problem is frame doesn't call repaint method at getMessage method. So, I have to minimize the window to solve this problem. Is their any other way to solve this problem?
The problem is that you added a component to a visible GUI. By default all components have a size of (0, 0) so there is nothing to paint.
What you really need to do is invoke the layout manager so the size/location of the component can be determined.
The basic code should be:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to repaint components
In JDK7 and later you can also do the revalidate()/repaint() on the frame.

Java: Why validate don't work

I want to change the panel dynamically and as this answer show, they recommend to use the cardLayout. But I want to change the whole UI(no old button left) and cardLayout seems not so convenient. So I have the following code:
JFrame frame = new JFrame ("Key test");
MyDrawPanel1 dp1 = new MyDrawPanel1(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());
p.add(dp1,BorderLayout.CENTER);
frame.getContentPane().add(p);
frame.pack();
frame.setVisible (true);
And in the MyDrawPanel1 there is a button to change panel:
public MyDrawPanel1(final JFrame frame) {
clickButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.getContentPane().removeAll();
//frame.validate();
frame.getContentPane().add(new MyDrawPanel2());
((JPanel)frame.getContentPane()).revalidate();
}
});
this.add(clickButton);
}
Edit:some more codes in MyDrawPanel1.
JButton clickButton = new JButton("click");
Image image = new ImageIcon("D:/0.jpg").getImage();
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawImage(image, 3, 40, null);
}
public Dimension getPreferredSize() {
if (image != null) {
return new Dimension(image.getWidth(null), image.getHeight(null));
}
return super.getPreferredSize(); // default
}
But the first panel doesn't disappear and I have to minimize it to refresh it so I can see the second panel. My question is the why validate don't work and if there is any other alternatives. Thanks.
Edit:here are the pictures I snipped about the panel.(first panel):
(after clicked):
Edit:
The madProgrammer and Adarsh Singhal provide two ways to solve the problem. The first is the second the panel don't call the super.paintComponent(), so add it then it works fine (as this answer say, it is the eraser so the first panel was gone). The second is calling the frame.repaint(), but I don't understand why?
We've to use repaint() to tell the components to repaint themselves. Visualize it as your case. While revalidate is used to update the layouts. So, whenever you add/remove components dynamically, you need to call both of them.The following written code displays a JFrame set to CardLayout to draw Red dp1(JPanel) completely on JFrame. On dp1, there is a Jbutton. If you click that button, dp1 will be removed & dp2(JPanel) will be drawn. dp2 is Green to distinguish changes. It seems you've forgotten repaint().
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame{
JButton button;
private static MyFrame frame;
public MyFrame(){
this.setSize(400, 400);
this.setLayout(new CardLayout());
this.setLocationRelativeTo(null);
JPanel dp1 = new JPanel();
dp1.setBackground(Color.RED);
add(dp1);
button = new JButton("Click me to remove dp1 & draw dp2");
dp1.add (button);
JPanel dp2 = new JPanel ();
dp2.setBackground(Color.GREEN);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
frame.getContentPane().remove(dp1);
frame.add(dp2);
frame.revalidate();
frame.repaint();
}});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible (true);
}
public static void main(String[] args) {
frame = new MyFrame();
}
}

Trouble using Cardlayout in java

Having Trouble adding components on panels in cardlayout , they are appearing weirdly (very small and top center) , have tried many layouts but not getting appropriate results , i have to place , buttons , Split Panes , tab Panes on different panels
here is the sample code. Having same issue on the code which i m working right now
pls see where i m going wrong
public static void main(String[] args) {
CardLayout cards = new CardLayout();
JPanel cardPanel = new JPanel();
cardPanel.setLayout(cards);
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Frame");
guiFrame.setSize(528, 555);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setVisible(true);
JButton B_1 = new JButton("");
JButton B_2 = new JButton("");
JPanel firstCard = new JPanel();
firstCard.add(B_1);
B_1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
cards.next(cardPanel);
}
});
JPanel secondCard = new JPanel();
secondCard.add(B_2);
B_2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
cards.previous(cardPanel);
}
});
cardPanel.add(firstCard);
cardPanel.add(secondCard);
guiFrame.add(cardPanel);
}
}
Your code wouldn't even compile.
I rearranged your code and added the following features:
I started your GUI with a call to SwingUtilities invokeLater. This puts the Swing GUI on the Event Dispatch thread (EDT).
I put like code together (JFrame, JPanel) so that the code would be easier to follow.
I put a JLabel on each of the card panels so you could see which panel was which.
I put text in the JButtons. They were so small because you had no text or label for the buttons to display.
I moved some of the JFrame code to the end of the method. The last thing you do in a Swing GUI is set the frame visible. You have to construct all of the Swing components before you make the JFrame window visible.
Here's the working, tested, minimal example of a card layout that you were trying to code.
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CardLayoutTest implements Runnable {
#Override
public void run() {
JFrame guiFrame = new JFrame();
// make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Frame");
final CardLayout cards = new CardLayout();
final JPanel cardPanel = new JPanel();
cardPanel.setLayout(cards);
final JPanel firstCard = new JPanel();
firstCard.setLayout(new FlowLayout());
JLabel label1 = new JLabel("Panel 1");
firstCard.add(label1);
JButton b_1 = new JButton("Swap to Panel 2");
firstCard.add(b_1);
final JPanel secondCard = new JPanel();
secondCard.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Panel 2");
secondCard.add(label2);
JButton b_2 = new JButton("Swap to Panel 1");
secondCard.add(b_2);
b_1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
cards.next(cardPanel);
}
});
b_2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
cards.previous(cardPanel);
}
});
cardPanel.add(firstCard, "First Panel");
cardPanel.add(secondCard, "Second Panel");
guiFrame.add(cardPanel);
guiFrame.setSize(528, 555);
// This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new CardLayoutTest());
}
}
This is how CarLayout Works, by default, it puts object in horisontal center and vertical top. If you did not specified size of added component, it is resized to smallest size possible.
Configure your layout alignment, set sizes on your components, or use another layout.

Categories