JLabel and JTextArea not working - java

I am making a window that has a large text area and a small text area under it. This is what i have so far:
This is the code I have for it:
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon ii=new ImageIcon("textEffect.png");
Image i=ii.getImage();
g.drawImage(i,0,0,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//main method
public static void main(String[] args){
new Window();
}
//Makes window and starts bot
public Window(){
super("Pollockoraptor");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setOpaque(false);
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setLineWrap(true);
input.addKeyListener(this);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.LEFT);
window.add(scroll);
window.add(label);
window.add(input);
window.setBackground(new Color(97,118,131));
add(window);
setVisible(true);
}'
How do I make the larger textarea partially transparent so I can see the background and how do I move "Say:" to be infront of the smaller textarea?

JPanel uses a FlowLayout by default. You'll want to change the layout manager, personally, I'd recommend something like GridBagLayout, but that's just me.
See Laying Out Components Within a Container for more details
To make the JTextArea see through, you're going to have to make the JScrollPane and it's JViewPort transparent as well.
Swing only knows how to paint fully opaque or fully transparent components. You can create a translucent effect by making the component transparent and overriding it's paintComponent method and using an AlphaComposite or painting a with a Color which has an alpha value set to something below 255
For example...
Can not draw image on JTextArea background when using Nimbus Look And Feel
JTextArea not selectable, but still showing a "ghost" cursor
Swing Graphics on JFrame
And general advice...
Key Bindings over KeyListener
Override getPreferredSize of your custom component to get better results when been laid out
Use JFrame#pack over setSize, this will calculate the window size based on the needs of the content and take into account the window frame decorations as well

How do I make the larger textarea partially transparent so I can see the background
Check out Backgrounds With Transparency for problems when using a transparent background and a general purpose solution you can use to you don't need to do custom painting all the time.

Related

Can I add a rectangle to a Jpanel in java swing and specify constraints for it using GridBagConstraints?

I'm pretty new to GUI and Java as a whole so I hope that I can explain this well enough and understand people's answers.
For a school project, I need to put a bunch of stuff on some rectangles but I'm having issues even adding one rectangle properly.
From researching online, this is what I have (the JPanel and GridBagConstraints are just there to show what I'd like to use):
public class GUI extends JPanel
{
public static void main (String [] args)
{
GUI g = new GUI();
JFrame window = new JFrame("Java Window");
window.setSize(1280, 960);
window.add(g);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel layout = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
}
#Override
public void paintComponent(Graphics g)
{
Color boxColour = new Color(194, 190, 190);
super.paintComponent(g);
g.setColor(boxColour);
g.fillRect(10, 10, 100, 100);
}
}
So right now, the rectangle appears in the window. But how can I add constraints to it? Is that possible? I would think that I should use JPanel to keep everything more organized since there will be many components so I tried adding this:
layout.add(g);
window.add(layout);
window.setVisible(true);
However, the rectangle no longer appeared. What am I doing wrong and how can I fix it and add constraints to my shapes? Thanks!
In your first scenario, the default layout manager of the frame is the BorderLayout and you are adding your "g" panel to the BorderLayout.CENTER. So based on the rules of the BorderLayout your "g" panel will take up all the space available in the frame. So you have plenty of space to paint your rectangle.
However, in your second scenario, your "layout" panel is using the default layout manager of a JPanel which is a FlowLayout which respects the preferred size of any component added to it.
The preferred size of the "g" panel is 10 x 10. So, when you add the "g" panel to the "layout" panel there is nothing to see because all you custom painting is done outside the bounds of the panel.
You need to override the getPreferredSize() method of your GUI panel to return a preferred size of (120, 120) so you can see your rectangle painted with a 10 pixel border around all the edges.
Read the section from the Swing tutorial on Custom Painting for more information and working examples demonstrating how to override the `getPreferredSize() method.
You will also need to read the Swing tutorial on How to Use GridBagLayout for example of using the constraints to add multiple components.

Java Swing JPanel Sizes

I would like to be able to have a JPanel within my JFrame of a fixed size 400x400.
I would also like the to be a 20px wide border all around it.
The main problem is the following code doesnt stick it its size.` JScrollPane runningAni = new JScrollPane(new views.cRunningAnimation(
model));
runningAni.setMaximumSize(new Dimension(400,400));
this.setSize(new Dimension(600,600));
this.add(runningAni,BorderLayout.CENTER);`
When doing this the runningAni panel just strethces accross the whole frame.
public void paint(Graphics g) {
this.setBackground(new Color(0,255,0));
}
I know this because my full frame paints itself green rather than just the JPanel (The above paint code is for my panel not the frame)
How would i create the panel so it always stays the same size and so there is always a 20px colored border around it?
BorderLayout ignores the size. You need to set a LayoutManager that either allows you to set the size to a fixed size or one that cares for the sizes set. There are different layout managers that allow this (e.g. GrindBagLayout or no layout manager at all). Some are not that easy to use (e.g. GridBagLayout). What to use depends on the rest of the layout.
You could probably use a layout panel that contains your custom panel. The layout panel needs an appropriate layout manager and could be put into the center of the BorderLayout. This would mean nearly no modifications to existing layout code.
The whole point of BorderLayout is to fill the center with the center component.
Don't override the paint() method to set the color of the panel. Use:
panel.setBackground(...);
When you create the panel.
How would i be able to set a border around my Jpanel
See How to Use Borders.
Just set your layout to null, to what ever class your adding your JPanel.
Then use the setBounds() method to set your location and size!
For example:
public class Main extends JFrame{
YourPanelClass panel = new YourPanelClass();
public Main(){
// I didn't want to put all the, everyday JFrame methods...
setLayout(null);
/*
First two coordinates indicate the location of JPanel inside JFrame.
The seconds set of coordinates set the size of your JPanel.
(The first two coordinates, 0 and 0, tell the JPanel to start at the
top left of your JFrame.)
*/
panel.setBounds(0, 0, 100, 100);
add(panel);
}
}
And i would GREATLY recommend using the paintComponent() method.
For instance:
(Obviously you put this in your JPanel's class.)
public void paintComponent(Graphics g){
super.paintComponent(g); // don't forget this if you are going to use this method.
//Basically this makes your JPanel's background green(I did it this way because I like doing it this way better.)
g.setColor(new Color(0, 255, 0));
g.fillRect(0, 0, getWidth(), getHeight());
}
Please don't forget to thumbs up if this helped!
setPreferredSize()
setMinimumSize()
setMaximumSize()
should do the trick

Draw graphics beneath components

So I am currently making a login screen that has a cool looking background effect made using the Graphics object and a 'game loop'. When I add in a JTextField though, it is seen underneath everything and not above. Is there anyway to to make the graphics draw underneath all components inside of the JFrame?
Here is an image of the graphics:
The text field is there, just underneath everything being drawn to the surface of the frame. I want to somehow reorder this so it draws underneath components.
Here is my current frame code:
frame = new JFrame("Login");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(450, 200));
frame.getContentPane().setBackground(Color.black);
frame.setBackground(Color.black);
frame.setLayout(new FlowLayout());
JTextField user = new JTextField(20);
user.setLocation(100, 200);
user.setVisible(true);
frame.add(user);
frame.pack();
frame.setVisible(true);
frame.createBufferStrategy(2);
buff = frame.getBufferStrategy();
Painter painter = new Painter();
frame.add(painter);
Any help please?
AnimationTest shows one approach. It overrides paintComponent() and invokes super.paintComponent() to ensure that components are rendered atop the background. Click anywhere to position a text field; resize to see how the default FlowLayout works. JPanel is double buffered by default using the existing buffer strategy.

Preserve border after removing title bar from JFrame

I want to remove title bar from JFrame, so I call setUndecorated(true) on that JFrame, but I would like to preserve border (nifty gradient) on that JFrame, which is present when decoration is on? Can I do that? Something like getting border instance for LookAndFeel default or make gradient border myself?
The default system LookAndFeel window borders are drawn by system, not Java, so there is no way to remove title bar from the window alone. The only thing you can do is undecorate your window and draw border by yourself (and yes, to fully copy system border you will have to put a lot of effort into it).
Maybe something like that could be available in SWT, but to use it you will have to abandon standart Swing.
You can accomplish this visually by creating a JPanel and giving it a border, then setting the panel as your frame's content.
public class Undecorated {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel borderedPanel = new JPanel();
//Use any border you want, eg a nice blue one
borderedPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.BLUE));
frame.setContentPane(borderedPanel);
frame.setUndecorated(true);
frame.setSize(new Dimension(200, 200));
frame.setVisible(true);
}
}

JFrame execution time adding JPanel

I'm working with a JFrame adding JPanel instances dynamically in the following way:
private void addBox(int x, int y){
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
panel.setSize(10, 10);
panel.setVisible(true);
panel.setLocation(x, y);
this.getContentPane().add(panel);
}
The problem is, when I use addBox method, the JPanel instance does not appear in the JFrame. The only way I can see the box I need to manualy resize the window.
Note: I tried using this.pack();, but this did not work.
You need to call revalidate() and repaint() after such structural changes to the GUI.
Note that setSize and setLocation should preferrably be handled by the layout manager.
Related link:
jGuru: What is the difference between repaint() and revalidate() in Swing components?
What are the purpose of the boxes?
If they are purely visual, and you don't intend to add components to them, it would be better to define a Box class (or use a Rectangle2D) and draw or fill them at time of paintComponent().
Alternately, draw them to the Graphics object of a BufferedImage and add the image to a JLabel, as shown here.
This example showing add/remove/pack may help.
private void addBox(int x, int y){
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
add(panel);
//If there isn't another JPanel, then this way you'll occupy
//the whole JFrame area; by defalut, JFrame has BorderLayout,
//and only one JComponent can occupy the central area
revalidate();
repaint();
}

Categories

Resources