I am trying to make two buttons in JPanel side by side. I have the following code as of now:
JFrame frame = new JFrame("Display Shapes");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton button = new JButton();
button.setBounds(50,100,80,30);
button.setText("Load Shapes");
panel.add(button);
JButton button2 = new JButton();
button2.setBounds(100,100,80,30);
button.setText("Sort Shapes");
panel.add(button2);
frame.add(panel);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
When I run the code, below is the output I see. I am not sure why the second button is not appearing correctly. What could be the reason for this?
The place where you are writing button.setText("Sort Shapes");, it will be button2.setText("Sort Shapes");
It is changing the text of the first button whose text had been set earlier
Related
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);
I am making my first program in Java, which would be a simple text editor..
Well, I have a JPanel and I want to add a JTextArea in it..But the JTextArea fits all the JPanel,so I cant add anything else like a Button in my JPanel..
I have tried to resize my JTextArea but nothing happens..
The code I have used:
TextArea text = new TextArea();
text.setSize(300, 200);
JFrame.add(text);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
final JTextArea area = new JTextArea(2,20);
frame.getContentPane().add(area);
JButton button1 = new JButton("press me");
JButton button2 = new JButton("press me");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
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..
I am trying to use a nested JPanel, which I can then reuse in different parts of my application, eg navigation bar at the top of the page. I am having trouble setting the orientation of the items, eg I want the button to be above the textfield.
If I create them individually and add them directly to the JPanel they come out one on top of the other as inteneded, as below:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
gui.add(button, BorderLayout.NORTH);
gui.add(button1, BorderLayout.SOUTH);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side, as below:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel container = new JPanel();
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
gui.add(container);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
I have tried setting the componenetOrientation,
container.setComponentOrientation(ComponentOrientation.);
but there is no option for vertical
I have tried setting the componenetOrientation
Please note the problem has nothing to do with component orientation: it's a layout manager problem as explained below.
However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side
Here:
JPanel container = new JPanel();
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
The default layout manager for panels is FlowLayout and it will ignore BorderLayout constraints. You'll have to set BorderLayout as layout manager not only to gui panel but to container panel as well.
JPanel container = new JPanel(new BorderLayout());
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
Try setting a layout for your JPanel. There are many Layouts available in the package java.awt. Some of them are BorderLayout, GridBagLayout, CardLayout, FlowLayout, GridLayout.
I have just added One line to your code, and now, it puts the buttons in the way you want:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel container = new JPanel();
container.setLayout(new GridLayout(2,1)); // This is the line that I have added.
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
gui.add(container);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
If you want to add more buttons, you can edit that line. The first Parameter of the constructor method of GridLayout is the number of vertical columns, and the second is the number of horizontal columns.
im starting to work with jpanel, but after a lot of reading and going crazy, i couldnt understand how to make it work...
so im trying to open a frame with 2 buttons, i was able to do that, but i cant postion them in the frame, here is my code:
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton but = new JButton("text");
but.setLocation(20, 20);
JButton but2 = new JButton("list");
but2.setLocation(50, 50);
JPanel panel= new JPanel();
panel.add(but);
panel.add(but2);
frame.add(panel);
frame.setVisible(true);