Fully REMOVE JLabel from JPanel...not setVisible(False) - java

I have a fairly simple question. I have a JPanel on a JFrame. I have a JLabel on the JPanel. How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime?
ImageIcon image7= new ImageIcon("archmageanim.gif");
JLabel label7 = new JLabel("", image7, JLabel.CENTER);
p.add( label7, "0 , 6" ); //This coordinate has to do with a layout manager I'm using - it
//I'm using - it works fine.
I have looked for this solution...but everyone says "the easiest way" is to set setVisible(false)...but that doesn't truly remove the object -_-. How can I REMOVE it?

Can't you just use this to find the parent Container of the JLabel and then use the remove method?
Container parent = label7.getParent();
parent.remove(label7);
parent.validate();
parent.repaint();
That should remove the label altogether and then refresh the parent Container.

It's this.
jpanel.remove(label7);
jpanel.revalidate();
jpanel.repaint();

jpanel.remove(component);
This is all you need to call to remove a component.

Related

Inserting an image as a label in java

label_007= new JLabel("My Label");
In lieu of the above Label, My Label I want to insert an image in the same position. How can I do that? I am just a novice in Java. Please help
ImageIcon icon = new ImageIcon('image.png');
JLabel image = new JLabel();
image.setIcon(icon);
The above code should work.
Make an image icon with an image file
Make a JLabel
Then set your JLabel's Icon
You can use ImageIcon for images:
ImageIcon ICON = new ImageIcon(URLClassLoader.class.getResource(IMAGE_PATH));
JLabel imageLabel = new JLabel();
imageLabel.setIcon(ICON);
URLClassLoader.class.getResource is using for packaging the application in runnable-jar file.
The IMAGE_PATH is relative to your project folders/packages. for example if the image is in com.app.images package, the path will be /com/app/images/image.png.
If you're novice in Java and particularly in Swing, I encourage you to check my swing projects- Minesweeper and Pacman. They are well-documented, I built them when I learned Swing.
Good luck :)

Java BorderLayout, south never displays itself

Im' working on a computer science project, and I'm having some issues with borderlayout:
that is my configuration (simplified)
public class MainPanel extends JPanel{
JSplitPane Pcenter;
JPanel PEnd;
public MainPanel(Container dad){
JTable myTable=new JTable(), anotherTable=new JTable();
JSplitPane left=new JSplitPane();
left.setTopComponent(new JScrollPane (myTable));
left.setBottomComponeny(new JScrollPane(anotherTable));
left.setOrientation(JSplitPane.VERTICAL_SPLIT);
this.setLayout(new BorderLayout());
Pcenter=new JSplitPane();
PEnd=new JPanel();
pend.add(new JButton("Store"));
Pcenter.setLeftComponent(left);
Pcenter.setRightComponent(new JScrollpane(new JLabel("right")));
this.add(Pcenter,BorderLayout.CENTER);
this.add(PPnd,BorderLayout.PAGE_END);
//EDIT:
PEnd.add(new JLabel("goofy"));
}
}
Now, my project is different, but this is my configuration
as I run the main (a jframe whith a JTabbed with this attached as a tab) it shows me only the the center and not the end one. But if I attach PEnd at the beginning it's sown as it have to.
EDIT 14-3-14
I dug into my code and I've seen that te problem is generated by the myTable's scrollpane so removing it will make PEnd shown but mytable represents itself in an horrible way
PEnd, the panel you are adding at the PAGE_END has nothing added to it so there is nothing to show, and has size 0.
Maybe with the line
pend.add(new JButton("Store"));
you meant
PEnd.add(new JButton("Store"));
As a side note, your variable names should start in lower case to follow standard Java style, to differenciate them from classes.

Force Horizontal Tab Text on Left Aligned JTabbedPane

I am trying to make a JTabbedPane in Java 7 on OSX that has tabs positioned to the left with their text horizontal (instead of vertical). However, with the code:
import javax.swing.*;
import java.awt.Dimension;
class Probs extends JDialog {
JTabbedPane options = new JTabbedPane();
Probs(JFrame owner) {
//main constructor
super(owner, "User Preferences", true);
//set the tabs to be left aligned
options.setTabPlacement(JTabbedPane.LEFT);
//construct the authorization panel
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("test", authorization);
add(options);
setSize(new Dimension(300,300)); //should use pack here
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
JFrame test = new JFrame();
new Probs(test);
test.dispose();
}
}
I get a dialog box which looks like: this image
I would like the tab text to be horizontal (the 'test' title on the tab be oriented horizontally instead of vertically).
I searched around on Google for a while and have only run into occurrences wherein people wanted to achieve vertical text on their tabs, I could not manage to locate any in which people wanted to have horizontal text (what I am trying to achieve).
In particular, I am trying to achieve something which exactly looks like the image mentioned in the first post of this question. It is basically the exact opposite of that question because the person in that tab started with what I am trying to achieve (I believe). Basically, I am trying to determine how to create the image displayed in the first post of that question.
Can someone please tell me how to have left-oriented tabs while preserving horizontal tab titles (as opposed to vertical)?
Thank you for your time and assistance.
Again, since I can't replicate the problem, Try this suggestion:
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("", authorization);
JLabel labTab2 = new JLabel("test"); // create a label
options.setTabComponentAt(0, labTab2); // set it to the component
The alignment is determined by your operating system. If you want to change the alignment of the tab text, you have to change the look and feel of your swing application. This worked for me. See here.
The system look and feel at MacOSX didn't support what you want in JTabbedPane. You must create a customized JComponent to do this or to set the look and feel of your application to cross platform (java metal) as stated before by #MonkeySupersonic.
I suggest the readings:
Apple Java Development Guide (section: User Interface Toolkits for Java) - https://developer.apple.com/library/content/documentation/Java/Conceptual/Java14Development/04-JavaUIToolkits/JavaUIToolkits.html
mac User Interface Guidelines - https://developer.apple.com/macos/human-interface-guidelines

Using JLabel for a background image

When I run the code it just opens an empty window
I also important whatever is necessary
relevant parts of the code:
public class Game extends JFrame implements ActionListener,KeyListener{
private JLabel background;
....
public Game(){
background=new JLabel(new ImageIcon("/graphics/board.gif"));
...
this.add(background);
this.setSize(800,600);
this.setVisible(true);...
I tried adding the JLabel to a JPanel and then add it to the frame but it still shows nothing in the window
Originally the code was:
JLabel background = new JLabel("/graphics/board.gif");
This would not set the image at the path described, Suggest that the following method is used (this could be simplified to just use a different JLabel constructor but steps shown for clarity)
Create and load the image and then set the icon for the Label As follows
ImageIcon icon = new ImageIcon("/graphics/board.gif");
JLabel background = new JLabel();
background.setIcon(icon);
Link to ImageIcon Java Doc
It is important to set in the layout the order in which the elements are displayed , maybe you have something that is displayed over the label..
I'm guessing you have a directory structure something like:
-c:\java
- source (for source and class files)
- graphic (for your images)
background=new JLabel(new ImageIcon("/graphics/board.gif"));
Don't specify the leading "/" in the file name. That tells Java to look at the root of the C drive, not at the directory where your class is executing from.
Also, don't use:
this.setSize(800,600);
The image does not stretch to fill the size of the frame. Intead you should be using:
this.pack();
so the frame will be the size of the image.

Adding Swing components to Eclipse RCP plugin

I'm having trouble embedding Swing components inside SWT (such as eclipse plugin..)
Currently what I have:
public void createPartControl(Composite parent) {
java.awt.Frame f = SWT_AWT.new_Frame(parent);
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Swing button");
JLabel label = new JLabel("Swing label");
panel.add(label,BorderLayout.NORTH);
panel.add(button,BorderLayout.CENTER);
f.add(panel);
}
This code snippet fails to load, the plugin crashes on the first line...
Any idea how to incorporate these components?
Thanks!
http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html
Minimally, embedding an AWT frame inside an SWT composite is just two simple lines of code
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
Frame frame = SWT_AWT.new_Frame(composite);
Since your code is failing at the first line then please first make sure that the parent Composite is created using SWT.EMBEDDED. If it is not then create a child composite using the SWT.EMBEDDED and then call
java.awt.Frame f = SWT_AWT.new_Frame(newChildComposite);
An instance of
org.eclipse.swt.Composite is created
with the SWT.EMBEDDED style. This
style signals that an AWT frame is to
be embedded inside the Composite. The
call to the static new_Frame method
creates and returns such a frame. The
frame may then be populated with AWT
and/or Swing components.
Taken from Article-Swing-SWT-Integration

Categories