I searched for hours for an answer,tried every method I know about and didnt find an answer.
I'm working on eclipse, my class is extending JFrame and im trying to replace two components that I added to the frame(using gridbaglayout).
When I'm removing the first one,I can't get to add the second one to where the first one was.
The components are JButton with images.
how do I get to add and remove components wherever I want? (already tried using GridBagConstraints to add it to the place I just removed a component from)
As a workaround you could add to your main panel (using GridbagLayout) inherited panels with the buttons in it. Then when you want to replace these buttons (or whatever component) you dont replace them on main panel. You replace them in the inherited panels. Since you are not giving us code, a kind of pseudocode would be like:
JButton myBtn = new JButton(); //Theinitial button
JPanel mainPanel = new JPanel(new GridBagLayout()); //main panel
JPanel inheritedPanel = new JPanel(new BorderLayout())//borderlayout to fill the entire panel.
inheritedPanel.add(myBtn,BorderLayout.CENTER);
mainPanel.add(inheritedPanel, myConstraints);
JButton replacementBtn = new JButton;
inheritedPanel.remove(myBtn);
inheritedPanel.add(replacementBtn);
inheritedPanel.repaint();
inheritedPanel.revalidate();
The components are JButton with images.
Just replace the image by using the setIcon(...) method.
Related
When I run my program I log in and then it goes to the main menu, from the main menu it should then go to the store room when store button is pressed but nothing shows up.
Here is the code to change jpanels
jPanel2.setVisible(false);
remove(jPanel2);
controlPanel = new JPanel();
ShopStock panel = new ShopStock();
controlPanel.add(panel);
controlPanel.setVisible(true);
What I tried to do is make the current jpanel visibility set to false, then I removed it from the screen, I then added a new jpanel to the screen, added the new class to the panel and made it visible, but nothing appears when the store button is pressed.
You need to add the new panel to the container. I suppose you are extending a JFrame or JPanel because you have the remove(jPanel2);
jPanel2.setVisible(false);
remove(jPanel2);
controlPanel = new JPanel();
ShopStock panel = new ShopStock();
controlPanel.add(panel);
controlPanel.setVisible(true);
add(controlPane);
validate(); //Or revalidate(); if validate doesn't work.
Hope it helps, it would be a good idear to check the CardLayout as RealSkeptic told you.The CardLayout is designe to interchange different panels and will have a better behaviour.
I'm trying for quite some time now to find what the problem but to no avail ,its a quite simple one really, the BorderLayout won't add the button to the correct place on the screen (South).
I don't want to add the button to the JPanel itself, I want to make a secondary panel, add that panel to the main panel and the button to the secondary panel, here the simple code:
public class panelClass extends JPanel{
JPanel secondaryPanel = new JPanel();
JButton btn = new JButton("Test");
public panelClass(){
add(secondaryPanel);
secondaryPanel.setLayout(new BorderLayout());
secondaryPanel.add(btn, BorderLayout.SOUTH);
}}
The button shows up in the upper-middle part of the screen, like a regular
FlowLayout, instead in the lower-middle part like I expected.
this line is the problem
add(secondaryPanel);
default layout of jpanel is flow layout .so when you add secondary panel to main panel secondary panel added to upper-middle position of main panel.that's why you see button shows up in the upper-middle part of the screen, like a regular flowLayout.if you set a background color to secondary panel you can clearly see the problem yourself
add appropriate layout to your main panel .for example
setLayout(new BorderLayout());
add(secondaryPanel,BorderLayout.CENTER);
I am a beginer and I dont know how to add more objects into JFrame.
How could I add more than one JPanel objects into JFrame?
Below is what I have tried.
Thanks for your help.
public class Init extends JFrame{
public Init(){
super("Ball");
Buttons t = new Buttons();
JumpingBall b1 = new JumpingBall();
JumpingBall b2 = new JumpingBall();
t.addBall(b1);
t.addBall(b2);
add(b1);
add(b2);
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
Assuming that JumpingBall extends JPanel, you might want to have a look at the java layout managers here: Link.
The default Layout for a JFrame is the BorderLayout and if you didn't specify where you want to add your component, The BorderLayout will put it in the center by default. In BorderLayout, you cannot have more that one component in the same area. So, in your example you will end up having only the second JumpingBall panel in your frame. If you want to have more than one component at the center, then you will have to create a JPanel and add those components to it using different Layout. The common three Layouts are the BorderLayout, FlowLayout and GridLayout Please have a look at the provided link above to see how the components are arranged.
You can add a number of JPanel objects in a JFrame, using the add method. If only one is displayed, you might need to change your Layout options or use a Layout Manager (Look here for more).
You are seeing only one because it overlapping each other. Just provide setbound(x,y,x1,y1) for you panel component and you will see your panel at location.
or use setLayout(new FlowLayout()); which is going to order your component in respective to other so you will not override each-other.
I have a text field to represent a name, and a combobox for registration type. Then, next to that I have a check box, but it's supposed to be underneath the other two fields. Here is what I have coded:
public RegPanel()
{
//create a new panel
new GridLayout(2,1);
//create one of two subpanels
subPanel = new JPanel(new FlowLayout());
//create a textfield
regTextField = new JTextField(20);
//create a combobox and don't let anyone add to it
regComboBox = new JComboBox(regOptions);
//create a border for the subpanel
subPanel.setBorder(BorderFactory.createTitledBorder("Registrant's Name & Type"));
//add regTypePanel and regBox to the panel
subPanel.add(regTextField);
subPanel.add(regComboBox);
//create a second subpanel with a flowlayout
subPanel2 = new JPanel(new FlowLayout());
//create a checkbox
regCheckBox = new JCheckBox("Dinner and Keynote Speech");
subPanel2.add(regCheckBox);
//add the subpanels to the main panel
add(subPanel);
add(subPanel2);
}
Any ideas what I am missing? Sorry for the crappy layout, I can't figure out how to fix the view.
So I realized I hadn't set the GridLayout right, so I changed that to "setLayout(new GridLayout(2,1));
But now on my gui, it totally screwed up the position of all the other elements.
Anyway new GridLayout(...) do nothing unless you use it in setLayout(...).
You can try using Box.createVerticalBox() (sample) instead of GridLayout to have your components in vertical alignment.
In your case, you are using
RegPanel (which layout?)
subPanel (FlowLayout)
regTextField
regComboBox
subPanel2 (FlowLayout)
regCheckBox
Which layout does your main RegPanel have? It has the default JPanel layout (if RegPanel is a subclass of JPanel), which is a FlowLayout. So, your RegPanel shows the two subPanels besides each other, which looks similar as if you had only one Panel with all the components. So, your RegPanel needs a LayoutManager, too - the GridLayout(2,1) seems okay (if you don't want to align the components in the two lines).
In my current project, I'm only ever using GroupLayout (apart from one occasional BorderLayout). It takes a bit to get used to (and a wrapper class to make the code easier to write and read), but for such form stuff, it seems ideal (when limited to the build-in Layout managers).
Also you might want to use a BorderLayout and realize that you nest one layout in another layout to achieve a different effect.
Have to mention MigLayout here as a great all-purpose layout manager -- it is extremely flexible and easy to use once you get to know it.
I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.
My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.
playButton = new JButton("Play");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
curTrackLabel = new JLabel("No Track Selected");
JFrame myFrame = new JFrame("MediaPlayer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("MediaPlayer");
myFrame.setLocation(400,300);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
myFrame.add(topPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
topPanel.add(buttonPanel, BorderLayout.CENTER);
topPanel.add(curTrackLabel, BorderLayout.NORTH);
playButton.addActionListener(new playButtonHandler());
pauseButton.addActionListener(new pauseButtonHandler());
stopButton.addActionListener(new stopButtonHandler());
myFrame.pack();
myFrame.setVisible(true);
Maybe try
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.html to learn more about BorderLayouts.
Now for your layout scheme it should be something along the lines of:
Top Level Container: JFrame
JFrame contains a JPanel (Call this
JPanel 1) with a BorderLayout.
The three buttons should be in a
SEPARATE jPanel (JPanel 2). JPanel
1 should add the three buttons as
BorderLayout.CENTER. In this way,
the window will resize if the button
changes its width and/or hright.
The JLabel should be added as
BorderLayout.LINE_START.
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:
Use JPanel and nest JPanels as necessary
BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
JFrame should only be used as a top level container (for more complex GUIs, this is true).
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.