I have a JPanel of null layout called MainPanel. Onclick of a button I am adding JTextpane on mainPanel. On first click I am creating a textpane of background color white. On second click I am creating another textpane of color blue. What I want is to place the blue textpane upon the white textpane, but the blue textpane is going behind the white textpane. How can I place it on white pane?
Code is very simple here. Onclick I am creating a new JTextpane, setting dimensions to it and placing it on the mainPanel.
Placing a sample screenshot which describes the issue better. Here the blue textpane has gone behind white textpane. I want it above white texpane. How do I do that?
If you want to stick with your current Components, (I think you should use kellax's solution, but I don't know if there's an extra requirement that's forcing you to use your current approach) you can look into Container.setComponentZOrder(Component comp, int index) to directly determine the order in which Components are displayed.
You will have to replace your JPanel "MainPanel" with a LayeredPanel.
Then you can say:
JLayeredPane mainPanel = new JLayeredPane();
JTextPane whitePane = new JTextPane("White text pane on top");
JTextPane bluePane = new JTextPane("Blue text pane behind");
mainPanel.add(whitePane, 2, 0);
mainPanel.add(bluePane, 1, 0);
Edit:
You can read more about the LayeredPane here: LayeredPane
Related
strange thing, i want to add text field next to the label. When i'm doing that it's not reacting on .setLocation command and background color does not change. Dont know why.
But when i set frame Layout to null , than background command working and changing the color , but text field are not showing. Strange. i tried with adding text field through panel, not working, by simple frame.add(textField) , not working.
public class EcrWindow extends JFrame {
JFrame ecrFrame;
JLabel ecr;
static JTextField ecrTitle;
public static void main(String[] args)
{
new EcrWindow();
}
EcrWindow()
{
JPanel p = new JPanel();
ecrFrame = new JFrame ("ECR WINDOW");
ecrFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
ecrFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
ecrFrame.setResizable(true);
ecrFrame.getContentPane().setBackground(Color.RED);
//ecrFrame.setLayout(null);
ecr = new JLabel("Emergancy Change title");
ecr.setSize(ecr.getPreferredSize());
ecr.setLocation(100,50);
ecrFrame.add(ecr);
ecrTitle = new JTextField();
ecrTitle.setColumns(30);
//ecrTitle.setSize(ecrTitle.getPreferredSize());
ecrTitle.setLocation(150,50);
p.add(ecrTitle); // adding text field to the panel, and panel adding to the frame
ecrFrame.add(p);
// ecrFrame.add(ecrTitle);
ecrFrame.setVisible(true);
}
}
The ContentPane of the Frame is not visible as soon as you add the Panel p to the frame. The content pane will be hidden by the panel which will consume the whole available space in the frame.
When you set the Layout to null, you do not see the Panel p any more as there is no information where it is rendered in the frame (the TextBox should also disappear when you set the Layout to null). This is why the red background is visible then.
Please try to add the following line to your code before you add the Panel p to the frame:
p.setBackground(Color.RED);
Then you should see a red background which is actually the Panel p.
Regarding the layout, you shouldn't use setLocation(). It is much better to use a different layout mechanism with a proper LayoutManager.
See also this answer.
You can use setLocation() if you use absolute positioning. But that would mean that you effectively write a layout manager by hand. I would advise you to read the guide Laying Out Components Within a Container - there are various explanations and examples you can build up upon.
Trying desperately to put a label over another label in netbeans because the one label is acting as the background image and I want the label in the foreground to have a different image.
Every time i drag another label on top, the JFrame gets bigger and the label tries to slot down the side.
Here is what I've got (I want the label where the red circle in the middle is not on the right down the side):
:
If you want to put one component on another, then you can use Layered Panes.
A layered panel will let you specify that some child components should be layered above other child components.
Update:
An example:
JLayeredPane pane = new JLayeredPane();
JLabel lbOne= new JLabel("Label one");
JLabel lbTwo = new JLabel("Label two");
pane.add(lbOne, 0);
pane.add(lbTwo , 1);
You just need to set the JFrame layout to null..
If you use panel below set the panel layout also null hope its work for you .
JLabel newLabel = new JLabel();
String a = b;//b is String from database.
newLabel.setText(a);
I need to generate text pulled from my database which contains multiple line, but when i put them into the label, all of the text became same line.
I tried using JTextArea and it works, however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel... which I wanted all the content to be aligned to the left. Any help would be much appreciated! thanks
however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel
The is related to the setAlignmentX(...) method of each component.
A JLabel uses 0.0f (for left alignment). A JTextArea and JScrollPane use 0.5f (for center alignment).
Using components with different alignments will cause alignment issues when using a BoxLayout.
The solution is to change the alignment of the text area or scroll pane (whichever component you add to the BoxLayout).
So the basic code would be:
JLabel label = new JLabel(...);
JTextArea textArea = new JTextArea(...);
textArea.setAlignmentX(0.0f);
JPanel panel = new BoxLayout(...);
panel.add(label);
panel.add(textArea);
Now both components will be left aligned.
I am making a simple swing application and I want to add some titled borders to my components. The border on both of my JScrollPanes work fine, but the JTextField and the JButtons don't. Allow me to share some screen shots.
I just have simple code for this. i.e
TitledBorder border = new TitledBorder("Border");
convert.setBorder(border); //convert is the JButton
I don't see why it would not work for one thing, and work for the other. Can anyone help me out?
A JTextField and JButton both use a Border already. So the titled border works but it changes the appearance of the component because you lose the default Border.
I also agree that normally you don't use a TitledBorder for an individual component but I suppose you could try to use a CompoundBorder to see if it looks any better:
CompoundBorder border = new CompoundBorder(titledBorder, button.getBorder());
button.setBorder( border );
but then the problem with the above approach is that you lose dynamic repainting of the border when you press/release the mouse on the button.
I want to make my TextArea black instead of white. I wish to make the Window fullscreen where the whole screen would be the TextArea that's black and would have white letters.
How can I achieve this?
Try this..
JTextArea txt = new JTextArea();
txt.setBackground(Color.BLACK);
txt.setForeground(Color.WHITE);
For swing components
JTextArea txt = new JTextArea();
//Set background black
txt.setBackground(Color.BLACK);
//Set Foreground(text) white
txt.setForeground(Color.WHITE);
Same goes for the awt components
TextArea txt = new TextArea();
//Set background black
txt.setBackground(Color.BLACK);
//Set Foreground(text) white
txt.setForeground(Color.WHITE);
The setForeground and setBackground methods belong to the JComponent/Component class and hence accessible to every component, as all the Swing/AWT component, at some place up the hierarchy, will extend these classes.