Adding Label to JPanel and Add method not suggested in Eclipse - java

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 {

Related

BoxLayout and this reference escape

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.

My buttons wont appear until i hover over it with my mouse

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.

JTabbedPane repaint does not work

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.

jpanel as inner class

I need to write a simple tennis game.
To move between different windows(panel with main menu, panel with game, panel with settings) I decided to use inner classes extends JPanel and replace it when some events like start new game occurs.
but the problem is - it doesn't see my inner class. I mean I add it to JFrame
mainframe.add(new MainMenuPanel());
but there is nothing on the screen when I run program. What's the problem?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainFrame{
JFrame mainframe;
public static void main(String[] args){
new MainFrame();
}
public MainFrame() {
mainframe = new JFrame();
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(300, 400);
mainframe.setTitle("X-Tennis v0.1");
mainframe.add(new MainMenuPanel());
mainframe.getContentPane().setLayout(new GridLayout());
mainframe.getContentPane().setBackground(Color.WHITE);
mainframe.setVisible(true);
}
public class MainMenuPanel extends JPanel {
JPanel mainmenupanel;
JLabel label1;
JButton btnNewGame,btnJoinGame;
ImageIcon iconNewGame,iconJoinGame;
public MainMenuPanel(){
mainmenupanel = new JPanel();
label1 = new JLabel("X-TENNIS");
label1.setFont(new Font("Comic Sans MS",Font.ITALIC,20));
label1.setForeground(Color.BLUE);
btnNewGame = new JButton("New Game", iconNewGame);
btnNewGame.setFocusPainted(false);
btnNewGame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(mainframe, "New game");
//delete current panel and add another to mainframe
}
}
);
btnNewGame.setPreferredSize(new Dimension(140,30));
btnJoinGame = new JButton("Join game",iconJoinGame);
mainmenupanel.add(label1);
mainmenupanel.add(btnNewGame);
}
}
}
There is no need for mainmenupanel within the MainMenuPanel class as MainMenuPanel is a JPanel itself
Simple add all the components in MainMenuPanel directly to itself
You create a new JPanel, mainmenupanel, inside MainMenuPanel but never add that to the container itself. You could do
add(mainmenupanel);
If you intend for this JPanel to occupy the full area of the parent, then you can simply add your components directly to your instance of MainMenuPanel as indicated by #Mad
First you should add your component to the ContentPane. In Swing, all the non-menu components displayed by the JFrame should be in the ContentPane.
mainframe.getContentPane().add(new MainMenuPanel());
Edit: I was wrong about the content pane, see #MadProgrammer comment.
Then you have to add the JPanel that you create in MainMenuPanel to the MainMenuPanel instance itself.
add(mainmenupanel);
But you should probably get rid of that intermediary container itself and add your labels to the MainMenuPanel instance itself:
add(label1);
add(btnNewGame);
mainmenupanel.add(label1);
mainmenupanel.add(btnNewGame);
try this :
super.add(label1);
super.add(btnNewGame);

Java Swing: Adding a JLabel to a JPanel

I'm simply trying to add a JLabel to an existing JPanel. This seems really simple, and I've searched all around. I think that this is right, but the label is not appearing on my panel. Anybody see what I am missing? Thanks!
ResultsPanel myPanel = new ResultsPanel(pnlResults); //pnlResults is an existing JPanel
myPanel.addLabel(pnlResults);
public class ResultsPanel extends JPanel {
JPanel myPanel;
public ResultsPanel(JPanel thisPanel) {
myPanel = thisPanel;
}
public void addLabel(JPanel myResults) {
JLabel myLabel = new JLabel("test", JLabel.LEFT);
myPanel.setLayout(new FlowLayout());
add(myLabel);
}
}
EDIT: In response to the immediate replies below, I agree that this seems to be total overkill. I went down this path because the following code also does not result in a JLabel being added to my JPanel:
JLabel myLabel = new JLabel("test");
pnlResults.add(myLabel);
I would much rather use this code, so feel free to comment on this if you think it is more likely to work (with some modifications, of course). Thanks again!
This seems to be jumping through hoops just to do a basic thing; simply call
JLabel label = new JLabel("Test text");//initialize the label
//do some stuff with label here maybe...
panel.add(label);//now add it
There is no need to make a class extends JPanel, and contain a JPanel; if a class extends JPanel, to get the JPanel instance, simply use this (so addLabel would instead do this.setLayout(blah)). But of course there is no need to even subclass JPanel for something as simple as adding a JLabel
Overall, here's pretty much the simplest swing application:
JFrame frame = new JFrame("Basic Swing");//Make a frame
frame.setSize(300, 300);//Give it a size
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//Make it go away on close
JPanel panel = new JPanel();//Make a panel
frame.add(panel);//Add it to your frame
JLabel label = new JLabel("Hello StackOverflow!");//Make a label
panel.add(label);//Add it to the panel (which is on the frame)
frame.setVisible(true);//Show the frame
Firstly, you've extended from JPanel
Secondly, you've supplied you're own JPanel
Now, from your code snippet, there's no way to determine if either ResultsPane or myPanel have been added to a Container of any kind, but from what you're saying, I'd suggest that would be your primary problem.
Do you really want/need to have an actual JLabel object? If not, you can label your panel as follow:
public void addLabel() {
myPanel.setBorder(new TitledBorder("test"));
}
Try this
JLabel myLabel = new JLabel("test text");
myLabel.setSize(myLabel.getPreferredSize());
panel.add(myLabel);
panel.revalidate();
panel.repaint();

Categories