Java BorderLayout, south never displays itself - java

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.

Related

Access Panel from Main Form from a Panel Form

I am trying to access a panel from my Main JForm which I use as a dynamic panel that repaints different Panel Forms.
this is the hierarchy of the forms.
Package_Main-Main_Form(JForm)-Dynamic_Panel(Panel)-DashBoard(Panel_Form)
Package_Panels-Panel_A-Panel_B
What I did was Display JForm first and repaint its JPanel with DashBoard.
now heres the problem.
I have a button inside my dashboard and when I tried to import Package_Panels.PanelA, but it doesn't work properly.
heres the first code I tried:
This code was added inside DashBoard Button.
PanelA x = new PanelA ();
this.removeAll();
this.add(x);
this.revalidate();
this.repaint();
x.setVisible(true);
I had doubts about this one since I knew if i use this.function, it points out the DashBoard Panel.
anyone knows a way to access the JForm's panel?
UPDATE: I found a way around this by using this.getParent() but other ideas are welcome. I don't to be stuck doing this over and over again.
heres what I did:
Panel_A x = new Panel_A();
this.setVisible(false);
this.getParent().add(x);
this.getParent().revalidate();
this.getParent().repaint();
x.setVisible(true);
//this.getParent().remove(this); - Does this really work? I dont want to keep this instance open.

BoxLayout for a JFrame

Could you help me understand what is going on here. I consulted Javadoc: JFrame has setLayout method. So, what sharing error springs out is a mystery to me.
public class View extends JFrame {
public View(){
// LayoutManager for the whole frame.
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
}
}
Result
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(BoxLayout.java:465)
at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:249)
at java.awt.Container.invalidate(Container.java:1583)
at java.awt.Component.invalidateIfValid(Component.java:2957)
at java.awt.Container.setLayout(Container.java:1484)
at javax.swing.JFrame.setLayout(JFrame.java:605)
at View.<init>(View.java:16)
at Init.main(Init.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Try this one on JFrame#getContentPane()
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
Read more How to Use BoxLayout
All the components are added in JFrame's content pane.
Read more Adding Components to the Content Pane
Here is the pictorial representation how JFrame looks like
EDIT
From comments:
Well, not clear anyway. I analyze it like this: BoxLayout class needs to know it target. JFrame has setLayoutt method and needs to know its layout.
this.setLayout(manager) internally calls getContentPane().setLayout(manager);
The below line
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
is converted to below line that is not correct.
this.getContentPane().setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
For more detail have a look at Source code

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.

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

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.

Categories