How can I make the TextArea in Java black? - java

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.

Related

text field not appearing or not reacting on location code

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.

Putting a Panel on top of a JLabel backGround (Java)

I've been fruitlessly searching the internet and nothing that people suggest seems to have any effect for me.
I have a JFrame which I'm trying to put a JPanel in. That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground. The issue is every type of layout manager I've seen suggested just does not work as advertised for me. The best I've gotten to work so far is this approach:
public MenuBackgroundPanel(AsteroidsFrame frame)
{
this.gameFrame = frame;
this.setLayout(new OverlayLayout(this));
ImageIcon image = new ImageIcon(getClass().getResource("/resources/background1.gif"));
imageLabel = new JLabel(image, JLabel.CENTER);
mp = new MainMenuPanel(gameFrame);
mp.setMaximumSize(new Dimension(300,200));
this.add(mp);
this.add(imageLabel);
this.setVisible(true);
}
Unfortunately, I'm getting really strange alignments and trying to set location on the background (to actually get it to start at the JFrame's (0,0) or moving the button panel just seems to have no effect. Printing the location of each object says they're both at (0,0) but the image I'll link shows this is just not the case. My point is, I've tried things like JLayeredPane or setting the JLabel as the contentpane of the Jframe and making it transparent but nothing seems to do anything. One or the other of the two objects just covers the other completely.
As you can see the objects are not at all aligned.
Could anyone help me with this?
That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground
Easiest way for something like this when the child panel is fully contained in the label image is to just set the layout manager of the JLabel and then add your components to the label.
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
JPanel buttons = new JPanel();
buttons.setOpaque( false );
buttons.add(...);
background.add(buttons, new GridBagConstraints() );
Now the button panel will be centered on the label.
As you can see the objects are not at all aligned
If you want to use the OverlayLayout then you need to play with the alignmentX/Y properties of each component. You would probably want to set them both to .5. Check out: Java Layout with Component always in Top Right for an example of how changing these values can affect the layout.

Java how to preserve newline from String to JLabel?

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.

JComponent Titled Border

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.

How to decide the order of components added to JPanel

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

Categories

Resources