i have this problem and yes i saw that other people had the problem to but i not realy able to compare there code to mine and see the problem that way so i hope u can help me.
i use intellij to write my code and use there gui desinger to make gui's but when i added a button i didnt get it to display untill i hover it with a mouse and the possitions are wrong and i cant realy get it to work. here are the classes
// this is the jpanel class
public class paintMenu extends JPanel{
public JPanel menuPanel;
public JButton newGameButt;
public JButton loadGameButt;
public JButton helpbutt;
public JButton optionsButt;
public JButton info;
public JButton quitButt;
public paintMenu(){
add(newGameButt);
add(loadGameButt);
add(helpbutt);
add(info);
add(optionsButt);
add(quitButt);
setVisible(true);
}
//this is de jframe class
public class jframepainter extends JFrame {
paintMenu menupaint = new paintMenu();
public jframepainter(){
//main frame settings
setTitle("Kingdom V " + Reference.version);
setSize(Reference.width, Reference.height);
setResizable(false);
setLocationRelativeTo(null);
setVisible(Kingdom.vissible);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//draw jpnael
getContentPane().add(menupaint);
}
Try setting the JFrame to visible after you add your JPanel to it. Also you may want to call this.pack() after you add your JPanel.
//main frame settings
setTitle("Kingdom V " + Reference.version);
setSize(Reference.width, Reference.height);
setResizable(false);
setLocationRelativeTo(null);
//draw jpnael
getContentPane().add(menupaint); //Moved this before setting Visible
this.pack(); // call pack before setting visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(Kingdom.vissible);
I don't know what the Kingdom class is, but i can assume that vissible is a typo and may be causing a compile-time error. You didn't clearly describe your problem.
Related
so after solving my first error due to the help of Hovercraft I finally started coding and getting stuff ready for my actual project. However, when I tried to add the label it now just shows me nothing at all, I mean if I remove the label and add any other stuff such as a button or so, it would show me the button but the moment I add the jlabel to the code it just gives me a complete blank screen no matter what the property of the label be. The code of the project is as follow:
Main Frame:
public class Parking_Mania {
public static void main(String []args)throws Exception
{
new GameFrame("Paking Mania");
}
}
Game Frame:
public class GameFrame extends JFrame{
File info=new File("information.txt");
public GameFrame(String name) throws IOException
{
if(!info.exists())
{
info.createNewFile();
}
this.setTitle(name);
this.setSize(640,510);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
Frames frame=new Frames();
this.add(frame);
frame.panel.show(frame, "opening");
}
}
Panel Class that handles changing different panels:
public class Frames extends JPanel{
CardLayout panel= new CardLayout();
public Frames()
{
this.setLayout(panel);
Opening op=new Opening(this);
//nxtframe nf= new nxtframe(this);
this.add(op, "opening");
//this.add(nf, "nt");
}
}
Finally the panel which should be showing itself on the frame:
public class Opening extends JPanel{
private Frames f;
private JLabel bg=new JLabel();
//private JLabel helpframe=new JLabel();
private JButton play=new JButton();
/*private JButton help=new JButton();
private JButton helpclose=new JButton();
private ImageIcon background;*/
public Opening(final Frames f){
this.f=f;
this.setBackground(Color.BLACK);
//this.add(bg);
this.add(play);
//bg.setIcon(new ImageIcon(getClass().getResource("/bg.jpg")));
}
}
Components should be added to the frame before the frame is made visible.
Start with the working examples found in the Swing tutorial. The tutorial covers all the basics of Swing.
I would especially start with the tutorial on How to Use CardLayout for the first demo code to understand and modify.
The tutorial will show you how to better structure your code so that you don't keep extending panels, just to add components to a panel.
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 {
I'm trying to understand what actually paints components in Swing. I read this article about painting in AWT and Swing and now tried to write the following simple program:
//A simple wrapper to understan how paint() works
public class MyButton extends JButton{
/**
* Default serialVersionUID
*/
private static final long serialVersionUID = 1L;
private final JButton jButton;
public MyButton(JButton jButton) {
this.jButton = jButton;
}
#Override
public void paint(Graphics g){
jButton.paint(g);
}
}
But when I try to add MyButton to frame
JFrame frame = new JFrame("Hello swing");
JPanel panel = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(new MyButton(button));
frame.add(panel);
it renders nothing
But after deleting
#Override
public void paint(Graphics g){
jButton.paint(g);
}
it renders the empty button:
QUESTION: Why does it behave that way? Why does the delegating cause rendering to fail?
First of all when you post a question you should post a proper SSCCE that demonstrates the problem. We can't copy/compile random lines of code. Until a problem is solved, you don't know what part of the code is causing the problem.
Why does the delegating cause rendering to fail?
My guess would be that the size of the button is (0, 0) so there is nothing to paint.
When you get rid of the custom paint method, then the real button can be painted because it does have a size because the layout manager has done its job.
public class Demo extends JFrame{
public static void main(String[] args)
{
JPanel panel = new JPanel();
getContentPane().setLayout(new BorderLayout());
panel.add(new JButton("Test"));
this.getContentPane().add(panel, BorderLayout.CENTER);
this.setSize(200,200);
this.setVisible(true);
}
}
If you want to add UI Components do it like that, don't use paint in any way.
If you want to paint for example a rectangle follow this tutorial: https://docs.oracle.com/javase/tutorial/uiswing/painting/
Your paint method does not draw the MyButton object, but instead draws the JButton which is member of your class. The problem now is, that this Button has not been added to the panel and so it's drawn on nothing. By removing your paint method, super.paint(g) is called because your class has no paint method and so your button, but not the member JButton is drawn.
I hope you understand what I am trying to explain to you.
I am trying to open a GUI from my main GUI by pressing a button. When the button is pressed, this is executed:
WorkloadFactor wf = new WorkloadFactor();
wf.setVisible(true);
This doesn't open the WorkloadFactor GUI. I am confused by this because I have other GUIs that open this way without issue.
WorkloadFactor class works fine when I run it by itself but won't open when it is called by my main GUI. Below is my class without imports and stuff:
public class WorkloadFactor extends JPanel {
public WorkloadFactor() {
setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
String[] tabnames = { "Zero", "One", "Two", "Three", "Four" };
for (int i = 0; i < tabnames.length; i++) {
tabbedPane.addTab(tabnames[i], createPane(tabnames[i]));
}
tabbedPane.setSelectedIndex(0);
JButton submit = new JButton("Submit All");
submit.setForeground(Color.RED);
add(tabbedPane, BorderLayout.CENTER);
add(submit, BorderLayout.SOUTH);
}
public JPanel createPane(final String t) {
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//setContentPane(contentPane); I think this might be it?
contentPane.setLayout(null);
setBounds(100, 100, 531, 347);
//***** all the components I am including then add them like so
//******contentPane.add(checkbox1);
//****** contentpane.add(label1);
return contentPane;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Set Workload Factor Information");
frame.getContentPane().add(new WorkloadFactor());
frame.setBounds(100, 100, 531, 347);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
I have tried arranging things in so many ways, putting everything in the constructor and other changes but can't seem to find a reason why instantiating this WorkloadFactor class elsewhere and setting it visible won't work.
Does it have something to do with setContentPane(contentPane) vs contentPane.add(xxxx) and returning it?
Thank you for reading!
WorkloadFactor wf = new WorkloadFactor();
wf.setVisible(true);
To be blunt, this won't display anything. Please understand that WorkloadFactor extends JPanel and like all non-top level components must be placed into a container that is ultimately held by a top-level window in order to be displayed. Look at how you display it in your main method -- you first put it into a JFrame, and then display that JFrame. You must do the same thing if you want to display it on button press -- you need to put it into a JPanel or other container that is held by a JFrame or JDialog, or JOptionPane.
Make sure that you have properly registered the button on your main GUI which opens WorkLoadFactor GUI to an action listener.
Since you have not included code from your main GUI I can't confirm this is the issue. However it is a commonly overlooked issue.
Heres some suggestions from the Java documentation tutorials:
"Problem: I'm trying to handle certain events from a component, but the component isn't generating the events it should.
First, make sure you registered the right kind of listener to detect the events. See whether another kind of listener might detect the kind of events you need.
Make sure you registered the listener on the right object.
Did you implement the event handler correctly? For example, if you extended an adapter class, then make sure you used the right method signature. Make sure each event-handling method is public void, that the name spelled right and that the argument is of the right type."
source: Solving Common Event-Handling Problems
Make a JFrame and add a JButton in it than add action listener in button and add this code in it like this:
This code makes a frame with a button and when button is pressed new window is opened.
public class Example extends JFrame {
public Example() {
super("Title");
setLayout(new FlowLayout());
JButton b = new JButton("Open new Frame");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
newWindow nw = new newWindow();
}
});
add(b);
}
}
newWindow Code:
public class newWindow extends JFrame {
newWindow() {
super("title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}
}
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.