JFrame turns blue when adding tabs - java

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.

Related

How to make a JButton that will create other buttons within a JPanel?

I am creating a user system to hold multiple details of multiple users. so i would like to create a button that would be able to create another button. when the second button is pressed a form will open for the user to fill. I have already created the form for the user to fill but i cannot manage to make the button to create more buttons to work. I have coded this but it does not show the button on the Jpanel.
I have created the following code:
private void mainButtonActionPerformed(java.awt.event.ActionEvent evt) {
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
jPanel3.add(b);
b.setVisible(true);
}
This doesn't seem to create a new button within jPanel3. Have I typed up the code incorrectly or is there an alternative correct way of doing this?
I would like 3 buttons in a row and then a new row of buttons.
Your code and question is missing too much information to be answered completely or well. About all I can say is
Always call jPanel3.revalidate() and jPanel3.repaint() on the container after adding or removing components from it as this tells the container's (here jPanel3) layout managers to re-layout all components and then re-draw them.
The container's layout manager is key for this to work well -- we have no idea what it is at the moment, and some layout managers will allow you to do this easily (e.g., FlowLayout, GridLayout) while others won't (e.g., GroupLayout).
There's no need for b.setVisible(true); since your newly created JComponent (JButton here) is already visible by default.
You appear to assume that it's using null layouts since you're calling setBounds(...), and this is a Bad Idea™. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
When asking such questions, try to create and post with the question a small but complete program that we can test and run, and that illustrates your problem, a minimal example program (please click on the link).
For example, my MCVE that shows how your code can work:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class AddButton extends JPanel {
private JPanel jPanel3 = new JPanel(); // panel to hold buttons
public AddButton() {
// create JButton that will add new buttons to jPanel3
JButton addMoreButtonsBtn = new JButton("Add More Buttons");
// give it an ActionListener
addMoreButtonsBtn.addActionListener(e -> {
final JButton newButton = new JButton("Click Here");
// when you click it, it removes itself (just for grins)
newButton.addActionListener(e2 -> {
jPanel3.remove(newButton);
// again revalidate and repaint
jPanel3.revalidate();
jPanel3.repaint();
});
// add to jPanel3, the "container"
jPanel3.add(newButton);
// revalidate and repaint the container
jPanel3.revalidate();
jPanel3.repaint();
});
// create a JPanel and put the add more buttons button to it
JPanel bottomPanel = new JPanel();
bottomPanel.add(addMoreButtonsBtn);
// give jPanel3 a layout that can handle new buttons
// a gridlayout with 1 column and any number of rows
jPanel3.setLayout(new GridLayout(0, 1));
// add it to the top of another JPanel that uses BorderLayout
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(jPanel3, BorderLayout.PAGE_START);
// and add that to a JScrollPane, so we can add many buttons and scroll
JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
// make the vert scrollbar always visible
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// force GUI to be larger
setPreferredSize(new Dimension(400, 200));
// give the main JPanel a BorderLayout
setLayout(new BorderLayout());
// and add scrollpane to center
add(scrollPane, BorderLayout.CENTER);
// add bottom panel to the bottom
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
AddButton mainPanel = new AddButton();
JFrame frame = new JFrame("AddButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

Adding JPanel to JFrame losing ability to organize components - Java

Description: I have been working on this small project in which I need to send a a specific JPanel from a class to the main JFrame based upon a parameter passed (1-4). In my main class I have a JFrame set up so I can visually check the panel being passed.
What doesn't work
Inside the "Accessor Class", I cannot seem to position the JComboBox in the middle of the Panel. Additionally, i'm not too sure I can do any kind of positioning. I implemented a button earlier with the exact same code (I replaced the JComboBox) and I couldn't resize the button either. HOWEVER... I can change the color of it.
GridBagLayout should center on default. Why is this being overridden? If you look at the picture/link provided, it shifts itself to the top center. I simply cannot move it.
Is this problem a result of the way I receive the JPanel from the class. Is there perhaps a better way I could call the panel.
Sorry for a lack of clarity. Struggling to comprehend some of the underlying concepts here in Java.
Any help is appreciated.
This is the main class
public static void main(String[] args) {
Accessor accessorOne = new Accessor(1); //Creates the Panel with param 1
JFrame frame = new JFrame("Testing");
frame.setLayout(new GridLayout(2,2));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.add(accessorOne); //Adds the Panel to the last spot in the JFrame
frame.setSize(650, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
This is the "Accessor" class that defines the panel
public class Accessor extends JPanel{
JPanel panel = new JPanel();
public Accessor(int num){
if(num == 1){
panel.setLayout(new GridBagLayout());
String[] choice1 = {"Testing One", "Testing two" };
JComboBox choiceBoxOne = new JComboBox(choice1);
choiceBoxOne.setBackground(Color.red); //These changes are correctly reflected!
panel.add(choiceBoxOne);
choiceBoxOne.setLocation(300,300); //ERROR -> Setting this value changes nothing!
add(panel);
}
// Other num options
}
}
This is the photo of the Jframe
Problem is you are using panel within class Accessor(also a panel with default layout as FlowLayout). So instead of making another panel instance, you could have just use GridBagLayout to instance of Accessor and add controls directly to Accessor instead of new panel.
Again absolute positioning works with null layouts(not recommended).
Also don't forget to use GridBagConstraints along with GridBagLayout.

Centering a fixed size canvas in a JFrame

I'm in the process of creating a circuit editor (similar to any regular paint software with a basic menu and a canvas with specifiable dimensions). I am currently trying to transform the previously unscrollable canvas (JPanel) to a scrollable one.
The obvious design error at the moment is that while the scrollbars seem to correctly reflect the internal size of the canvas (which can of course be way bigger than the JFrame), due to the canvas JPanel being added in the CENTER of the BorderLayout of the master panel, it always resizes along with the JFrame.
public final class MainFrame extends JFrame
{
public MainFrame()
{
JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
// Populate Menu Panel
// ...
JPanel canvasPanel = new JPanel();
canvasPanel.setBackground(Color.white);
Dimension canvasDims = new Dimension(800,600);
canvasPanel.setPreferredSize(canvasDims);
canvasPanel.setMinimumSize(canvasDims);
canvasPanel.setMaximumSize(canvasDims);
JScrollPane canvasScrollPane = new JScrollPane(
canvasPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel masterPanel = new JPanel(new BorderLayout());
masterPanel.add(menuPanel, BorderLayout.NORTH);
masterPanel.add(canvasScrollPane, BorderLayout.CENTER);
setContentPane(masterPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 700);
setMinimumSize(new Dimension(500, 500));
setLocationRelativeTo(null);
setVisible(true);
}
I've read quite a few posts regarding centering JPanels and scrollbars but with both complexities added together, things might be a bit more complicated as I haven't yet found a solution.
What I'm really trying to achieve is to have the canvas' JPanel fixed in whatever size the user might have specified and centered in the middle as well as the scrollbars behaving as one would expect like in the beloved windows' paint:
How would you go about doing this/fixing my design? Any help would be greatly appreciated!
P.S. Happy new Year :)
JPanel fixed in whatever size the user might have specified and centered in the middle as well as the scrollbars behaving as one would expect
So you need to nest panels so the canvas panel can be displayed at its preferred size, while the parent panel resizes with the size of the frame.
An easy way to do this is with a JPanel that uses a GridBagLayout. Then you add the canvas panel to this panel using the default GridBagConstraints.
So the basic structure of the panels would be:
JPanel canvas = new JPanel();
canvas.setPreferredSize( new Dimension(300, 300) );
canvas.setBackground(Color.RED);
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(canvas, new GridBagConstraints() );
frame.add(new JScrollPane(wrapper));
Note: there is no need for your "masterPanel". The default layout manager for the content pane of a JFrame is a BorderLayout, so you just add the "menuPanel" and "scrollPane" directly to the frame with the proper BorderLayout constraints.

Java: How to make a JScrollPane around a BorderLayout

I am trying to make my first GUI-program and sofar everything went well, but now I have a problem:
First my Window had a minimum size and everything worek well, but now I dont want a minimum size, therefore I want a ScrollBar (vertical and horizonal) to be able to to see everything. I am trying to make this with a JScrollPane. The Problem is, that I have my program structured with a BorderLayout and I am not able to connect my JScrollPane with my BorderLayout. "JScrollPane Constructor is undefinied for BorderLayout". So I
initialized a JPanel and set the layout to my BorderLayout.
windowContainer = new JPanel(new BorderLayout(10, 10));
Then I can connect the "windowContainer"(JPanel) with my JscrollPane
windowScrollPane = new JScrollPane(windowContainer);
After changing the rest of the code (changed "getContentPane.add..." to "windowContainer.add...") I didnt become errors but the JScrollPane didnt work. In my BorderLayout (LINE_START) is a JPanel with a minimim width of "300", so at least if the window is thinner than 300px the ScrollBar should appaer.
I spend a lot of research on the iinternet but everything what i found was "How to create a JScrollPane in a BorderLayout" and not "How to create a JScrollPane around the BorderLayout".
To clarify it i will uploat a picture (the red things are the JScrollBars).
Sorry I wasnt allowed to upload pictures, so please look here: http://www.fotos-hochladen.net/view/jscrollpanepu20315v9x.png
And i dont know how much code i have to give you, because everything would be to much, so just say something if you need more.
Here is again the important code about it:
...
windowContainer = new JPanel(new BorderLayout(10, 10));
windowScrollPane = new JScrollPane(windowContainer);
frame.add(windowContainer);
...
PS: This is my first post, so please correct me if I did something wrong (about the post). And sorry for my english.
Try this code. Method initComponent() use in contructor, or in place where you build view. Below I put example of JFrame with BorderLaylout as you want:
public class TestWindow extends JFrame{
int containerHeigh=300;
int containerWitdh=400;
private JPanel container;
private JPanel westPanel;
private JPanel eastPanel;
private JPanel northPanel;
private JPanel southPanel;
private JPanel centerPanel;
private JScrollPane scroll;
public TestWindow(){
super("test");
initComponents();
}
private void initComponents(){
container=new JPanel();
westPanel=new JPanel();
eastPanel=new JPanel();
northPanel=new JPanel();
southPanel=new JPanel();
centerPanel=new JPanel();
//...fill panels of container
westPanel.setBackground(new Color(95,183,70));
eastPanel.setBackground(new Color(0,164,232));
northPanel.setBackground(new Color(255,255,185));
southPanel.setBackground(new Color(34,177,76));
centerPanel.setBackground(new Color(152,114,203));
scroll=new JScrollPane();
scroll.setViewportView(container);
BorderLayout containerLayout=new BorderLayout(containerHeigh, containerWitdh);
container.setLayout(containerLayout);
container.add(westPanel, BorderLayout.WEST);
container.add(eastPanel, BorderLayout.EAST);
container.add(northPanel, BorderLayout.NORTH);
container.add(southPanel, BorderLayout.SOUTH);
container.add(centerPanel, BorderLayout.CENTER);
add(scroll);
setVisible(true);
}
public static void main(String...args){
new TestWindow();
}
}
If you want you can use NetBeans for create Panels and other element of desctop applications. I generally build in NetBeans simple Panels, Dialog and then combine together dynamically in application. This provide me get user interface exactly what I want and prepare really fast.

overlapping JPanels in the GUI

I am using BorderLayout in my application. I have a main panel to which I add two JPanels at the center. I want one of them to be transparent.
My code is :
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.add(getSituationPanel(), BorderLayout.CENTER);
Code for these two functions is :
public JScrollPane getGraphPaneScrollPane() {
if (graphPaneScrollPane == null) {
graphPaneScrollPane = new JScrollPane();
graphPaneScrollPane.setViewportView(getGraphEditorPane());
}
return graphPaneScrollPane;
}
private JScrollPane getSituationPanel(){
if(situationPanel == null){
logs.debug("Initializing Situation Panel");
situationPanel = new JScrollPane();
situationLabel = new JLabel("");
situationLabel.setVerticalTextPosition(JLabel.BOTTOM);
situationLabel.setHorizontalTextPosition(JLabel.CENTER);
situationLabel.setVerticalAlignment(JLabel.TOP);
situationLabel.setHorizontalAlignment(JLabel.CENTER);
situationLabel.setBorder(BorderFactory.createTitledBorder(""));
situationLabel.setBackground(Color.WHITE);
situationLabel.setOpaque(true);
situationLabel.setVerticalAlignment(SwingConstants.TOP);
situationPanel.setViewportView(situationLabel);
}
return situationPanel;
}
Now I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI, because getGraphPaneScrollPane is the canvas, which I use to draw nodes.
I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI,
The panel that is on top is the panel that needs to be transparent. If the panel on top is opaque then you will never see the panel under the top panel.
So making changes to the layout is the last thing I want.
Well that is what you are going to need to do. You can't just add two panels to one panel and expect it to work the way you want it to. Most Swing layout managers are designed to lay out components in two dimensions, not on top of one another.
Your current code is:
mainPanel.setLayout(new BorderLayout());
mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.add(getSituationPanel(), BorderLayout.CENTER);
You could try using the OverlayLayout, it is designed to lay out panels on top of on another. The code should be something like:
JPanel overlay = new JPanel();
overlay.setLayout( new OverlayLayout(overlay) );
overlay.add(getSituationPanel(), BorderLayout.CENTER); // add transparent panel first
overlay.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(overlay);

Categories

Resources