JButton added to JPanel doesn't show - java

I can't seem to add a JButton to a JPanel.
I have a PropWindow (JFrame) that has a PropView (JPanel) in it. the PropView-JPanel seems to be added correctly because I can draw shapes on it with paint().
But when I use this to try adding a button it just won't show up att all :/
JButton testButton;
public PropView(int width, int height) {
super(true);
setLayout(null);
setSize(width, height);
//TestButton
testButton = new JButton("Test");
testButton.setLocation(10,10);
testButton.setSize(100, 50);
testButton.setVisible(true);
add(testButton);
setFocusable(true);
setVisible(true);
}
The JFrame and the JPanel are both 250x600 px.

I can't tell from the code snippet you posted but just in case: make sure you call pack () on the frame after you have added the panel or any other components.
Also, it's usually discouraged to extend a JPanel or JFrame, unless you have a good reason to do it, just a heads up.
Here you have a short tutorial about displaying frames:
And some sample code in it that might help:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);

Make sure you added PropPanel to PropWindow using myPropWindow.getContentPane().add(myPropPanel), not just myPropWindow.add(myPropPanel).

Related

JFrame turns blue when adding tabs

I currently have a Jframe that I want to add to a tab instead.
(I used a frame just for testing purposes, to make sure the look and feel is correct, but when trying to add it to a JTabbedPane, the frame starts to look blue (weird top aswell).
I tried copying my settings from my original frame to the new frame but that did not help.
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("1", frame.getContentPane());
JFrame FinalFrame = new JFrame();
FinalFrame.setDefaultLookAndFeelDecorated(true);
FinalFrame.setSize(WIDTH, HEIGTH);
FinalFrame.setLocation(100, 150);
FinalFrame.setTitle("Primal-Pvm Notification center");
FinalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FinalFrame.add(tabs);
Side by side view of the problem and the frame before adding it to the tab:
Edit: Answer by George Z. helped me out a lot.
Like he said to solve the problem:
Don't add things to your main frame but add them to a Jpanel and add that to a JTabbedPane.
If you have a Jpanel that you are adding to a tab that contains an override in the paintComponent, you have to create that class as the Jpanel so:
JPanel panel = new LineDrawer([Enter parameters]);
panel.setLayout([Enter Layout]);
The way you are approaching this seems to be pretty complex hence this weird behavior. (Looks like a look and feel problem? - show the part of the code that sets it)
However, I suggest you to create only one JFrame (this question explains why you should do that), set the layout of its content pane to BorderLayout and keep it like this. Its a rare situation to mess up with content panes. After that create independent JPanels representing the tab(s) you would like to have. Finally create a JTabbedPane with these panels and add it to the content frame of the JFrame.
A small example would be:
public class TabbedPanelExample extends JFrame {
public TabbedPanelExample() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTabbedPane pane = new JTabbedPane();
pane.addTab("FirstTab", createFirstTab());
pane.addTab("SecondTab", createSecondTab());
add(pane);
setSize(400, 400);
setLocationRelativeTo(null);
}
private Component createFirstTab() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Some Component"));
panel.add(new JTextField("Some Other Component"));
return panel;
}
private Component createSecondTab() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Some Component"));
panel.add(new JButton("Some Other Component"));
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new TabbedPanelExample().setVisible(true);
});
}
}
Post edit based on this comment:
Well I do have a Jframe with a lot of elements added to it so it kinda
is a hassle to switch it all to panels;
A JFrame cannot have a lot of elements. You take a look on how to use root panes. The container that "has a lot of elements" of a JFrame is its rootpane which is mostly completed by its contentpane. When you frame.add(component), you add the component to its content pane. Guess what? A JFrame's content pane is a JPanel. So are a already to panels.
Now in order to make this work, try to do as i said and frame.setLayout(new BorderLayout(); //changes layout to contentpane. Assuming you have a bunch of components (lets say comp1,comp2) and you are adding them like:
frame.add(comp1);
frame.add(comp2);
You must do the following in order to make it clear. Create a JPanel and instead of frame.add(comp1), do panel.add(comp1). So this JPanel has all the components you added in JFrame. After that create your JTabbedPane:
JTabbedPane pane = new JTabbedPane();
pane.addTab("tab", panel);
and finally add this pane to the content pane of your JFrame:
frame.add(pane);
In conclusion, you will move all the components you have added to your frame into a JPanel, add this JPanel to a JTabbedPane, and finally add this JTabbedPane to the frame (contentpane). Frame has only one component.

Change dimensions of JButton

I have problem with this. The button is taking up the entire JFrame. I've tried changing the dimensions of the JFrame and the JButton but with no changed. It's completely hiding a JTable underneath. Could someone please tell me what I'm doing wrong.
JFrame FRAME = new JFrame();
JButton BUTTON = new JButton("OK");
FRAME.add(new JScrollPane(TableName));
BUTTON.setPreferredSize(new Dimension(20,30));
FRAME.add(BUTTON);
FRAME.setSize(700, 600);
FRAME.setVisible(true);
FRAME.setLocationRelativeTo(null);
The default layout manager for a a JFrame is a BorderLayout. You are attempting to add two component the CENTER which is not allowed. Only the last one added will be displayed.
You need to specify constraints when adding components to a BorderLayout. Your code should be something like:
frame.add(new JScrollPane(TableName), BorderLayout.CENTER);
button.setPreferredSize(new Dimension(20,30));
frame.add(button, BorderLayout.PAGE_START);
Also, variables names should NOT be upper cased. Follow Java convention.
Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples. The tutorial code will also show you how to better structure your program so you follow Swing coding conventions.
Don't add the button straight to the frame, create a JPanel first, add the button to the panel then add the panel to the frame.
JFrame frame = new JFrame();
// frame.setBounds(x axis, y axis, weight, height)
frame.setBounds(10,10,304,214);
frame.getContentPane().setLayout(null);
JButton button = new JButton("Button");
button.setBounds(98, 75, 126, 39);
frame.getContentPane().add(button);
frame.setVisible(true);

Put a JLabel over two JButtons

Okay so before you ask, yes I'm not using any Layout Manager. No, that doesn't make this bad design (as I've seen people in here saying because someone simply didn't use one). The thing is i want the label to always (and I mean always) show over the two buttons (over the gap left by them which makes it impossible to put it as an Icon or a text on the JButton).
JFrame frame = new JFrame("ColorTap");
private void init() {
JButton jb1 = new JButton(""), jb2 = new JButton("-");
JLabel label = new JLabel("TEXT HERE");
label.setForeground(Color.white);
label.setFont(new Font("Arial Bold",Font.ITALIC,30));
label.setBounds(60,249,200,100);
frame.setLayout(null);
jb1.setBounds(0, 0, 300,298);
jb2.setBounds(0, 302, 300, 300);
jb1.setBackground(Color.black);
jb2.setBackground(Color.black);
jb1.setBorderPainted(false);
jb2.setBorderPainted(false);
frame.add(label);
frame.add(jb1);
frame.add(jb2);
frame.setResizable(false);
frame.setSize(300, 628);
frame.setLocation(550, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
After this what's stranger to me is that the button on the bottom stays under the label and not the one on the top... HELP! Thanks
Swing is optimized to paint components in two dimensions. That is it assumes components will never overlap. Overlapping JButtons cause a problem because of the rollover effects which only cause the buttons to be painted, not the label, so the button is painted over the top of the label.
So you need to tell Swing that components do overlap so Swing can make sure components are painted in the proper ZOrder:
JPanel panel = new JPanel()
{
#Override
public boolean isOptimizedDrawingEnabled()
{
return false;
}
};
Now you can set the layout manager of the panel and add your components to the panel and they will be painted properly.
See: How to put a JButton with an image on top of another JButton with an image? for a working example that DOES use layout managers.

Get the real size of a JFrame content (NEW)

I am writing a Game in Java, and I don't want to use a layout manager for my JFrame. My class extends JFrame, and looks something like this:
//class field
static JPanel contentPane;
//in the class constructor
this.contentPane = new JPanel();
this.contentPane.setLayout(null);
this.setContentPane(contentPane);
I want my JPanel be exactly 600x600 px, but when I set the size of my JFrame by calling the this.setSize(600,600) method, the JPanel size is less than the 600x600 px because the border of the JFrame window is included too.
How can I set the size of the JPanel to be exactly 600x600 px?
P.S. I have seen all of the previous post and none of them work for me.
For example:
Get the real size of a JFrame content does not work for me.
What else can I do?
How can I set the size of the JPanel to be exactly 600x600 px?
Override the getPreferredSize() method of your custom game panel to return the size you want the panel to be.
#Override Dimension getPreferredSize()
{
return new Dimension(600, 600);
}
Then the basic code to create your frame will be:
GamePanel panel = new GamePanel();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Now all your game logic will be contained in the GamePanel class.
If you don't need the Frame-Decorations (icon, title, min/max/close-buttons and the border), you can make a Frame undecorated, then it has exactly the size you gave it
java.awt.Frame.setUndecorated(boolean)

Java, Put a panel on the background image of frame

I am trying to write a retro snake game. I set a background image of an old Nokia, and I want to play the snake from its screen. However, I cannot put my gamepanel on my background image.
Can I do that?
I used this code for my background image.
Thanks.
JFrame frame = new JFrame("Retro Snake");
frame.getContentPane().add(new GamePanel());
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("Images\\background.jpg")))));
} catch (IOException e) {
e.printStackTrace();
}
frame.setLocation(500, 100);
frame.pack();
frame.setVisible(true);
Don't add the JPanel to a contentPane that you are only going to discard on the next line or two (as you're currently doing).
Give your new contentPane-JLabel a BorderLayout via setLayout(new BorderLayout())
add the gamepanel to the JLabel above, Borderlayout.CENTER.
Be sure that you make your gamepanel JPanel non-opaque by calling setOpaque(false) on it, so that the background JLabel shows its image.

Categories

Resources