Jpanel elements stuck in the center - java

I have added a text field to the mainPanel inside the frame, but when I try to change it's location the text field is stuck in the middle of the window.
This is my code:
`
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
JTextField textField = new JTextField("This is a text field", 20);
mainPanel.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.setBackground(new Color(10, 10, 10));
mainPanel.add(textField);
textField.setLocation(10, 10);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("CSGO translator app");
frame.pack();
frame.setVisible(true);
`
I have tried to change the layout and to use the setLocation() method, but neither of these work.
I am expecting to be able to move the text field to the top of the window.

Related

JPanel won't show in JFrame

I'm trying to create a JPanel (non-resizable) showing a grid of buttons but when I try to add the JPanel to a JFrame it won't show.
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
frame.setSize(681,920);
frame.setResizable(true);
JLabel label = new JLabel();
label.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
JButton btn = new JButton();
btn.setContentAreaFilled( false );
btn.setBorder( null );
btn.setBounds(214,210,0,0);
label.add(btn);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
The output should be a resizable frame with inside a 3x4 grid of buttons.
If I don't use the panel and I put the line frame.setResizable(false) it works as expected but I need to add more stuff to the frame so I need to put the buttons safe in a panel.
Both panel and label are added to your frame, to make sure they are added write
JLabel label = new JLabel("JLABEL");
and
panel.setBackground(Color.BLUE);

Java GUI Logic - Not understanding adding to JFrame

I have a pretty straight forward question. Can some please please explain to me why the following JFrame is not showing Hello (100,100 pixels on the left side of the screen and World (100,100 pixels) on the right side of the screen since I am using border layout.
I created a JFrame
Assigned it a layout of borderlayout
Created 2 panels with 2 labels and assigned the panels to be aligned left and right.
added the panels to the JFrame
Displayed the JFrame
What am I missing?
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(100,100));
panel1.setBackground(Color.BLUE);
JLabel label1 = new JLabel("Hello");
label1.setBackground(Color.YELLOW);
label1.setForeground(Color.WHITE);
panel1.add(label1,BorderLayout.LINE_START);
frame.add(panel1);
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(100,100));
panel2.setBackground(Color.RED);
JLabel label2 = new JLabel("World");
label2.setBackground(Color.CYAN);
label2.setForeground(Color.WHITE);
panel2.add(label2,BorderLayout.LINE_END);
frame.add(panel2);
You are setting the Layout-Constraints on the wrong panel.
Instead of panel2.add(label2,BorderLayout.LINE_END); it should be panel2.add(label2) and instead of frame.add(panel2); it should be frame.add(panel2, BorderLayout.LINE_END);.
Same for panel1.

Trying to add ScrollPane in Jpanel with null layout inside BorderLayout

I am trying to add a scrollbar in jpanel with null layout.
I want to create a form. This should should display few buttons at the bottom at all times.Any content inside form should maintain it's size and ratio even if the parent container is resized.
Here is what I've come with. I have a panel with borderlayout and added buttons at the south of border. Then created another jpanel to contain form that is added at the center of parent jpanel. Since I want form to maintain it's ratio I went with null layout for inner panel. But I want it to display scrollbar when content is not fully visible. enter image description here
Now adding inner jpanel into scrollpane and adding scrollpanel into parent panel (.add(scrollpane, BorderLayout.CENTER)) doesn't give desired format.
Is there any thing that I can do to get desired format?
Here is code Sample:
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(new Dimension(1000, 700));
Container c = jFrame.getContentPane();
c.setLayout(new BorderLayout());
bottomPanel(c);
centerPanel(c); //scrollbar should go in this panel
jFrame.setVisible(true);
}
private static void centerPanel(Container c) {
JPanel centerPanel = new JPanel();
centerPanel.setLayout(null);
JButton button = new JButton("This jObject should not resize when window resizes and also should maintain relative position.");
button.setBounds(new Rectangle(10, 10, 600, 50));
JButton button1 = new JButton("Just like it works in this code. Just Add ScrollPane to centerPanel That is in green backround");
button1.setBounds(new Rectangle(10, 70, 600, 50));
JButton button2 = new JButton("For clearity");
button2.setBounds(new Rectangle(10, 130, 600, 50));
centerPanel.add(button);
centerPanel.add(button1);
centerPanel.add(button2);
centerPanel.setBackground(Color.GREEN);
c.add(centerPanel, BorderLayout.CENTER);
}
private static void bottomPanel(Container c) {
JPanel bottomPanel = new JPanel(); //Buttons that goes at the bottom of screen will go in here
JPanel bottomInnerPanel = new JPanel();
bottomInnerPanel.setLayout(new BorderLayout());
bottomPanel.setLayout(new GridLayout());
bottomInnerPanel.add(new JButton("Add"), BorderLayout.WEST);
bottomInnerPanel.add(new JButton("Search"), BorderLayout.EAST);
bottomPanel.add(bottomInnerPanel);
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
c.add(bottomPanel, BorderLayout.SOUTH);
}

Picture and text in same window

This program is supposed to open a window, add a picture, and then add the text "hello world" above the picture. The text appears when i do frame.add(label) and then try to add the picture (like the code shows), but even when I do the opposite and add the picture first I only get a gray schreen. Can anybody show me how I can get both the picture and the text?
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
frame.add(label);
frame.add(new JLabel(new ImageIcon("file")));;
}
}
You should use overlay layout, but it is applicable on JPanel.
So add a JPanel to your frame then apply the layout, finally add the components.
Your code may be like that:
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
panel.add(label);
panel.add(new JLabel(new ImageIcon("file")));
frame.add(panel, BorderLayout.CENTER);
}
}
A label can have both text and icon, and the relative position can be customized.
JLabel label = new JLabel ("hello world", new ImageIcon("file"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
//frame.add(new JLabel(new ImageIcon("file")));;
The default layout is BorderLayout, and add(label, BorderLayout.CENTER).

Swing GUI problems with BorderLayout

I'm currently trying to get along with Layouts, considering that I never really understood them and only did nullLayout instead, absolutely positioning all elements then.
However, I currently have a suitable small project, where I am trying to learn it, which is some small chat service.
Here is a picture right now:
And here is a picture, of how I imagine it to be finished (Please note that this is just some concept, but it should give you the right idea. I'm not a graphic artist):
Here is my current code:
public class Gui {
JFrame frame;
JTextArea textfield;
JTextField enterMessage;
public Gui(){
frame = new JFrame();
frame.setSize(600, 400);
textfield = new JTextArea();
textfield.setText("Textfield");
textfield.setSize(400, 300);
JPanel messagePanel = new JPanel();
JTextField chatMessage = new JTextField();
chatMessage.setText("Send me");
JButton send = new JButton();
send.setText("Send");
messagePanel.add(chatMessage, BorderLayout.WEST);
messagePanel.add(send, BorderLayout.EAST);
frame.add(textfield, BorderLayout.WEST);
frame.add(messagePanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
My idea, together with the understanding of BorderLayouts so far was to put the Textfield, where the chat dialog ends up in later on, right inside the frame, on the WEST-side.
The button to send and the field to enter some text will be inside a panel, with an own borderlayout, while the button has some smaller part on the right and the rest of the width is being filled with the textfield.
The whole panel then ends on the SOUTH-side of the frame.
However, right now I have the problem, that the elements keep shrinking to the least possible size.
I tried to fix this with setSize(); , but that does not have an impact at all, it is just being completely ignored.
Any help to point me into the right direction?
Initially, you've got one simple problem:
// should be new JPanel(new BorderLayout())
JPanel messagePanel = new JPanel();
Then, after that, generally BorderLayout likes to stretch the component in BorderLayout.CENTER. So you want to put your textfield and chatMessage in the center.
public Gui(){
frame = new JFrame();
frame.setSize(600, 400);
textfield = new JTextArea();
textfield.setText("Textfield");
// textfield.setSize(400, 300);
JPanel messagePanel = new JPanel(new BorderLayout());
JTextField chatMessage = new JTextField("Send me");
JButton send = new JButton("Send");
messagePanel.add(chatMessage, BorderLayout.CENTER);
messagePanel.add(send, BorderLayout.EAST);
frame.add(textfield, BorderLayout.CENTER);
frame.add(messagePanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
Once you do that, you should get something like this:
But, as a few words of advice:
Don't rely on setSize of a JFrame. Instead, you should use setPreferredSize on a single component which the entire UI should size itself around. (Probably the main text area.) The size of a JFrame includes, for example, the title bar.
You should consider wrapping your JTextArea in a scroll pane. You can then instead setPreferredSize on the viewport.
After you have a component with a preferred size, call pack() on the JFrame before calling setVisible(true). This will size it automatically.
Something like:
frame = new JFrame();
// frame.setSize(600, 400);
...
JScrollPane pane = new JScrollPane(
textfield,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// specifying initial size for the
// visible portion of the scroll pane
pane.getViewport().setPreferredSize(new Dimension(320, 200));
frame.add(pane, BorderLayout.CENTER);
frame.add(messagePanel, BorderLayout.SOUTH);
// entire UI sizes around the scroll pane view
frame.pack();
frame.setVisible(true);
Try BoxLayout insted BorderLayout in messagePanel:
messagePanel.setLayout(new BoxLayout(messagePanel,BoxLayout.LINE_AXIS));
messagePanel.add(chatMessage);
messagePanel.add(send);
And for textField:
frame.add(textfield, BorderLayout.CENTER);
Try setting preferred size dimensions of the elements.
textfield.setText("Textfield");
textfield.setPreferredSize(new Dimension(600, 300));
//some other code
JTextField chatMessage = new JTextField();
chatMessage.setPreferredSize(new Dimension(500, 25));
//some other code
As pointed out by Sridhar, BorderLayout does not always respect the dimensions of sub-panels. To fix this, you should initialize your sub-panels (in this case textfield and messagePanel) using setPreferedSize() instead of setSize().
change your constructor to
public Gui() {
frame = new JFrame();
frame.setSize(600, 400);
textfield = new JTextArea();
textfield.setText("Textfield");
textfield.setSize(400, 300);
// set border layout to JPanel
JPanel messagePanel = new JPanel(new BorderLayout());
JTextField chatMessage = new JTextField();
chatMessage.setText("Send me");
JButton send = new JButton();
send.setText("Send");
// add JTextField to CENTER and button to EAST
messagePanel.add(chatMessage, BorderLayout.CENTER);
messagePanel.add(send, BorderLayout.EAST);
// add textArea to CENTER of JFrame
frame.add(textfield, BorderLayout.CENTER);
frame.add(messagePanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
and it will work..

Categories