This is not a repeat question. Yes there are similar, but none have provided a working answer.
public class Tool extends JPanel implements ActionListener{
public JPanel Panel;
public Tool() {
}
public void show(){
displayStuff();
Panel.setVisible(true);
revalidate();
repaint();
}
}
Tool MyTool = new Tool();
JPanel Master = new JPanel();
JPanel Dash = = new JPanel();
JTabbedPane Tabs = new JTabbedPane();
JTabbedPane Tabs.addTab("Dash", Dash);
JTabbedPane Tabs.addTab("Tool", MyTool.Panel);
Master.add(Tabs);
The real code is much more complex. But the basic issue is that when changes occurs on MyTool.Panel as a result of user pressing some buttons.
MyTool.Panel does NOT get repainted until I use mouse to move Master.
How can I force it to repaint?
Its not a perfect solution but you can validate and redraw the entire application, thats what I have done in the past. I've used something like
class MyPanel extends JPanel{
public void doRedraw(){
getTopLevelAncestor().revalidate();
getTopLevelAncestor().repaint();
}
}
Hope this helps.
Related
This may seem a very newbie question but I can't seem to find answers for this situation.
The How to Use Panels tutorial says there is an add() method in JPanel but I cannot implement it in my code.
public class JPanelTest extends AbstractView {
private final JPanel panel;
public JPanelTest () {
this.panel = new JPanel(new BorderLayout());
}
private void initComponents() {
JLabel label = new JLabel("label testing ");
this.panel.add(label);
}
}
The AbstractView is an abstract class that extends JPanel and implements ViewSupport, PropertyChangeListener.
In the last line of code this.panel.add(label); gives a compile error.
I don't see Panel.add() suggested in Eclipse.
The suggested add-related methods are addAncestorListener, addNotify, addVetoableChangeListener.
How can I not see a simple add method in the suggested box?
I am using 1.6 compile level of Eclipse. Would that make a difference?
Thanks in advance!
You create the JPanel, panel, but add it to nothing. So yes, it is receiving the JLabel, but since neither the JLabel nor the panel JPanel are added to anything displayed by the main GUI window (a JFrame?) none get displayed.
Solution: either add the panel JPanel or the JLabel itself to the this object (which is hopefully added to the top level window/JFrame).
So either:
private void initComponents() {
JLabel label = new JLabel("label testing ");
this.panel.add(label);
this.add(panel);
}
or
private void initComponents() {
JLabel label = new JLabel("label testing ");
// this.panel.add(label);
this.add(label);
}
Where did you get the AbstractView? It is not a build in to Swing hierachy.
Base on your class name maybe you want to extend JPanel instead of AbstractView
public class JPanelTest extends JPanel {
Good afternoon,
As a personal project intended to help me practice using JPanels and subclasses, I am coding a professional web portfolio containing my education, work and volunteer experience, and other notable works using JApplets.
I have the layout set to BorderLayout with the JButtons aligned WEST. When a button is clicked, the JPanels in the CENTER are supposed to switch out with the appropriate panel. However, I am not that far yet.
As of now, I only have a JLabel onto my Home JPanel that says "Home," because I'd like to make sure the method from the Home JPanel class is working before doing anything more. The issue is that the JPanel isn't displaying on the applet.
The thing is, when I move all the code into from the JPanel's class onto the main class, it displays just fine. So I know the problem is with either how I'm constructing the method, or with how I constructed my JPanel's class.
I've tried setting it to visible-- that didn't work. I've tried setting the LayoutManager as a parameter for the class constructor, I've tried adding paintComponent and super.paint(g), I tried using this.home.addHomePanel-- but nothing worked.
I know I'm missing a few things. It would be appreciated if someone could give me a hand. Please let me know if you need more information. Thank you for reading.
Main Class:
public class myWebFolio extends JApplet implements ActionListener
{
JButton[ ] menu =
{
new JButton("Home"),
new JButton("Education"),
new JButton("Work Experience"),
new JButton("Programming Projects"),
new JButton("Other")
};
//adds panel to memory
private JPanel buttonPanel = new JPanel();
private Home home;
public void init()
{
setLayout (new BorderLayout( ) ); //changes the layout of the appl
home = new Home();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
/*
* Adds an ActionListener to each button
* and then adds the button to the buttonPanel.
* Also adds an invisible componenet to give the buttons
* spacing.
*/
for (int i=0; i<menu.length; i++)
{
menu[i].addActionListener(this);
buttonPanel.add(Box.createVerticalStrut(100));
buttonPanel.add(menu[i]);
}
add(buttonPanel,BorderLayout.WEST);
home.addHomePanel();
}
public void actionPerformed(ActionEvent event)
{
}
public void paintComponent(Graphics g)
{
super.paint(g);
}
}
Home Panel's Class:
public class Home extends JPanel
{
JPanel homePanel = new JPanel(new FlowLayout());
JLabel label = new JLabel("Home");
/**
* Constructor for objects of class Home
*/
public Home()
{
}
public void addHomePanel()
{
homePanel.add(label, FlowLayout.LEFT);
homePanel.setVisible(true);
add(homePanel, BorderLayout.CENTER);
}
public void paintComponent(Graphics g)
{
super.paint(g);
}
}
In a basic GUI I'm creating I'm trying to extend JPanel and setting BoxLayout as a layout. Here's what I'm trying to do:
public class TestPanel extends JPanel {
public TestPanel() {
super();
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
}
}
I've recently discovered that this cannot be used as a parameter until the current instance is fully constructed; yet, I've seen this kind of code frequently in examples I've found around the net. Does it mean I need to do something like this to be sure everything is going as expected?
public class TestPanel extends JPanel {
private TestPanel() {
super();
}
public static TestPanel create() {
TestPanel panel = new TestPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
return panel;
}
}
EDIT: To be more clear, here's the issue I'm referring to. I'm not sure those consideration apply to my case, but I would've thought so.
I am really new to GUI programming in Java, I did a lot of research and I couldn't find an answer to this problem.
I have a simple JFrame with a menu, and inside this JFrame I have a JPanel with a log in form (were users input their username and password), and then I want to change that JPanel to another JPanel depending on what users want to do.
What would be the best way of doing this? I think that stacking JPanels is OK. But after I add new JLayeredPanels in Netbeans they don't stack. I read somewhere that I should use Z ordering or something like that, but I can't find it on the designer view.
Well, thank you very much for your patience!
CardLayout class has a useful API that can serve your requirements. Using methods like next(), first(), last() can be helpful.
I've prepared a simple demonstration of changing panels within a parent panel and/or frame.
Take a look at it:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PanelChanger implements ActionListener
{
JPanel panels;
public void init(Container pane)
{
JButton switcher = new JButton("Switch Active Panel!");
switcher.addActionListener(this);
JPanel login = new JPanel();
login.setBackground(Color.CYAN);
login.add(new JLabel("Welcome to login panel."));
JPanel another = new JPanel();
another.setBackground(Color.GREEN);
another.add(new JLabel("Yeah, this is another panel."));
panels = new JPanel(new CardLayout());
panels.add(login);
panels.add(another);
pane.add(switcher, BorderLayout.PAGE_START);
pane.add(panels, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
CardLayout layout = (CardLayout)(panels.getLayout());
layout.next(panels);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("CardLayoutDemo");
PanelChanger changer = new PanelChanger();
changer.init(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
The code has a JPanel with an inner JPanel that displays awt drawing. Upon mouseclick the inner JPanel is to be replaced by one of its polymorphic siblings. This code isn't replacing the jPanel.
class ContentPanel extends JPanel {
private GraphicPanel graphicPanel;
public ContentPanel(GraphicPanel graphicPanel) {
this.graphicPanel = graphicPanel;
add(this.graphicPanel);
public void setGraphicPanel(GraphicPanel graphicPanel) {
this.graphicPanel = graphicPanel;
// invalidate();
// revalidate();
// repaint();
}
Setting the graphicPanel to a polymorphic relative doesn't cause any errors, it just isn't painting the new graphicPanel. Using cardLayout is not preferred, there must be a cleaner way. How to proceed?
in setGraphicPanel, you need to remove the current graphicPanel and add the new one. THEN call revalidate.
something like this:
public void setGraphicPanel(GraphicPanel graphicPanel) {
this.removeAll();
this.graphicPanel = graphicPanel;
this.add(graphicPanel);
this.revalidate();
}
Although CardLayout was designed to do just this thing. Are you sure you don't want to use CardLayout?