I have a class called "Console", with the following structure:
public Console(Game game) {
super(new GridBagLayout());
m_game = game;
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextArea(20, 75);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
add(textField, c);
}
And then a method in my Game class:
public void createAndShowGUI() {
JFrame frame = new JFrame("My game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newConsole);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
The problem I'm having, is I don't want the Scroll Pane to be able to scroll horizontally, only vertically. The horizontal scrollbar appears when I append something to the Text Area that's too large to fit in the window. Is there a way I can prevent horizontal scrolling, and instead just have the Text Area print out whatever's too large to fit on the next line?
Example:
(The example Text Area can only fit 20 characters before it needs to allow horizontal scrolling)
Instead of
Hello, my name is Bob.
This would appear
Hello, my name is B
ob.
You can set JScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER) to ensure there is never a horizontal scroll. In addition, your textArea will need JTextArea.setWrapStyleWord(true) and JTextArea.setLineWrap(true);
Related
Created a JFrame and tried to change the background but it does not work.
I have looked at so many solutions here and on the Internet and it doesnt work , What is wrong in my code
JFrame frame = new JFrame("Process");
JLabel label = new JLabel("Please wait...");
JProgressBar pb = new JProgressBar();
pb.setIndeterminate(true);
pb.setBackground(new java.awt.Color(248, 201, 171));
frame.setBackground(new java.awt.Color(242, 186, 152));
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBackground(new java.awt.Color(242, 186, 152));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10); // make spaces between components on screen
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 20;
panel.add(label, c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 20;
panel.add(pb, c);
frame.add(panel);
frame.setVisible(true);
try frame.getContentPane().setBackground(new java.awt.Color(242, 186, 152));
don't work on the frame, work on the frame.getContentPane(). This is also where your sub panel and layout should be set. Besides, you use gridbag constraints but you never set a gridbag layout first. Meanwhile, you add to the frame and even if you did add to the content pane, you also didn't specify a content pane layout.
After doing changes in my code I found that the problem is caused by the GridBagConstraints c = new GridBagConstraints();
so i changed this by using panel.setLayout(null); then used setBounds to adjust the position of the components label.setBounds(100, 30, 250, 10);
and everything os ok
I have 5 JPanels lined up vertically. Each JPanel is filled with the same elements, but different values (JPanel, JButton, JLabel). I want them to look like this:
Panel Button Label
Panel Button Label
Buts it's turning out like this
Panel Button Label
Panel Button Label
The spacing is a little off, but the code is exactly the same for each container. How can I fix this?
public class AnswerChoice extends JPanel {
private static final long serialVersionUID = 1L;
private AnswerButton button;
private JLabel answerLabel;
public AnswerChoice(String imageURL) {
setBackground(Color.RED);
setLayout(new GridBagLayout());
button = new AnswerButton(imageURL);
answerLabel = new JLabel();
answerLabel.setFont(new Font("Times New Roman", Font.PLAIN, 32));
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setBackground(Color.ORANGE);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.weightx = 0.2;
add(panel, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 2;
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 30);
add(button, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 3;
gbc.weightx = 0.8;
add(answerLabel, gbc);
}
}
The JLabel does have text in it but I set it somewhere else.
but the code is exactly the same for each container.
The layout is done independently for each container based on the components added to the containter. So the size of each component on the container matters. Each container doesn't know that you have 4 other containers.
I have 5 JPanels lined up vertically.
So then you need to create a single panel using a GridBagLayout and add all 15 components to that panel. Then all 3 columns will be sized based on the components in each of the 5 rows.
Or I see in your logic that you try to assign relative sizes to each of the 3 components as .2, 0, .8. In this case you could use the Relative Layout on each of the panels. Using the Relative Layout you would display the button at its preferred size and then use 0.2f and 0.8f as the contraints for the panel and label respectively.
I'm making a program that uses a GridBagLayout in a container. My primary use for this is to have two JPanels, which occupy 75% and 25% of the horizontal space of the window. For some reason though, the two panels look more like 90/10, and when resizing, the smaller one rapidly changes in size, between it's apparent minimum size, and what I believe is the desired 25%.
Here is the relevant code.
frmReedreadV = new JFrame();
frmReedreadV.setBounds(x, y, width, height);
frmReedreadV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmReedreadV.getContentPane().setLayout(new BoxLayout(frmReedreadV.getContentPane(), BoxLayout.Y_AXIS));
JPanel stretchyPanel = new JPanel();
frmReedreadV.getContentPane().add(stretchyPanel);
stretchyPanel.setLayout(new CardLayout(0, 0));
JPanel textAndUsers = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
textArea = new JTextArea();
textArea.setMargin(new Insets(2, 5, 5, 2));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
gbc.weightx = 0.8;
textAndUsers.add(scrollPane, gbc);
list = new FriendsList(listUpdate);
gbc.weightx = 0.2;
textAndUsers.add(list.frmUserList, gbc);
stretchyPanel.add(textAndUsers);
FriendsList is a JList contained in a JPanel.
There are other buttons and text fields in the main CardLayout content pane, but those shouldn't affect what is inside of this GridBagLayout, correct?
I made another copy of this JPanel as a standalone application, and it displays and resizes perfectly. See here:
JFrame frame = new JFrame();
frame.getContentPane().setLayout((new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 550, 600);
JPanel stretchyPane = new JPanel();
frame.getContentPane().add(stretchyPane);
stretchyPane.setLayout(new CardLayout(0, 0));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextArea text = new JTextArea();
text.setMargin(new Insets(2, 5, 5, 2));
JScrollPane panel1 = new JScrollPane(text);
FriendsList panel2 = new FriendsList(new Object());
c.fill = GridBagConstraints.BOTH;
c.weightx = .8;
c.weighty = 1;
panel.add(panel1, c);
c.weightx = .2;
//c.fill = GridBagConstraints.HORIZONTAL;
panel.add(panel2.frmUserList, c);
stretchyPane.add(panel);
frame.setVisible(true);
What could be causing the difference between the two, since I've replicated my original line by line into the copy?
The weightx and weighty properties might appear to act as proportional sizes, but that is not what they do. In fact they determine the distribution of extra space in the layout.
If you set everything to its preferred size by calling pack() on your JFrame, there will be no extra space. Which means the weightx and weighty properties have no effect while it's in that state.
Once the user starts resizing the window to be larger, there will be extra space, and only then will GridBagLayout consult the weightx and weighty properties to determine how to apportion that extra space to each column and row. Until then, it's entirely possible for a component with a small weightx to be wider than a component with a larger weightx, if their preferred sizes dictate it.
Hopefully this simple program will demonstrate this concept. Try using the mouse (or keyboard) to resize the window to be wider, and observe how each of the textfields grows:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagProportions {
static void buildAndShowWindow() {
JTextField small = new JTextField("small (0.8)", 5);
JTextField large = new JTextField("LARGE (0.2)", 30);
small.setMinimumSize(small.getPreferredSize());
large.setMinimumSize(large.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.left = 6;
gbc.insets.top = 6;
gbc.insets.bottom = 6;
gbc.weightx = 0.8;
panel.add(small, gbc);
gbc.weightx = 0.2;
gbc.insets.right = 6;
panel.add(large, gbc);
JFrame frame = new JFrame("GridBagLayout Proportions");
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
buildAndShowWindow();
}
});
}
}
So what can be done about it? Well, this is one layout scenario that GridBagLayout cannot do. I would try using a SpringLayout:
SpringLayout layout = new SpringLayout();
JPanel textAndUsers = new JPanel(layout);
SpringLayout.Constraints scrollPaneConstraints =
new SpringLayout.Constraints(scrollPane);
Spring scrollPaneWidth = scrollPaneConstraints.getWidth();
SpringLayout.Constraints listConstraints =
new SpringLayout.Constraints(scrollPaneWidth,
scrollPaneConstraints.getY(),
Spring.scale(scrollPaneWidth, 0.25f),
scrollPaneConstraints.getHeight());
layout.putConstraint(SpringLayout.EAST, textAndUsers, 0,
SpringLayout.EAST, frmUserList);
layout.putConstraint(SpringLayout.SOUTH, textAndUsers, 0,
SpringLayout.SOUTH, scrollPane);
textAndUsers.add(scrollPane, scrollPaneConstraints);
textAndUsers.add(frmUserList, listConstraints);
Notice that the creation of listConstraints specifies a width argument which is Spring.scale(scrollPaneWidth, 0.25f). This ensures the list is always one-fourth as wide as the scrollPane containing the JTextArea.
SpringLayout is tricky to use, in that you have to make sure to link the far edges of the layout container to child components explicitly, because SpringLayout won't grow to accommodate all the child components automatically. That's what the putConstraint calls are doing.
I am trying to create a login page. I wrote code for two textboxes and one button. One textbox next to Username and other one next to Password. One "Sign In" button below. But I am not sure why the textbox's and button are not shown on my output. I only get the Username and password label's on my ouput screen.
Strange thing is whenever I stretch my output frame, (I mean either pulling the screen horizontally or vertically) the two textboxes and the button shows up.
Please check my code and let me know what's wrong. I was trying to put pictures to make easier to understand but I do not have enough reputation. Please help.
import javax.swing.*;
import java.awt.*;
public class HomeScreen{
public static void main(String args[]){
JFrame frame = new JFrame("Medical Store");
frame.setVisible(true);
frame.setSize(600,400);
JPanel panel = new JPanel(new GridBagLayout());
frame.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
JLabel label1 = new JLabel("Username");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,10,10);
panel.add(label1,c);
JLabel label2 = new JLabel("Password");
c.gridx = 0;
c.gridy = 1;
panel.add(label2,c);
JTextField textbox1 = new JTextField(10);
c.gridx = 1;
c.gridy = 1;
panel.add(textbox1,c);
JTextField textbox2 = new JTextField(10);
c.gridx = 2;
c.gridy = 1;
panel.add(textbox2,c);
JButton button1 = new JButton("Sign In");
c.gridx = 1;
c.gridy = 2;
panel.add(button1,c);
}
}
You're calling setVisible(true) before adding all components, and so your GUI is doing just that, drawing itself before components are added.
JFrame frame = new JFrame("Medical Store");
frame.setVisible(true);
// all components added here
Solution: make the setVisible(true) call at the end after adding all components.
JFrame frame = new JFrame("Medical Store");
// all components added here
frame.setVisible(true);
Now all components should be visualized.
Other quibbles:
Avoid calling setSize(...) on anything. Instead let the layout managers and component preferred sizes do that for you.
Call pack() on the JFrame just prior to setting it visible so that the above will happen.
I am trying to learn how to make JAVA programs and I am working with Swing. I am trying to place a button in the top left corner of the window and it keeps going to the top center.
public void createGUI(){
JFrame frame = new JFrame("My Project");
frame.setDefaultCloseOperation(3);
frame.setSize(400, 350);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
addButtonGUI(panel, new JButton(), "test", 1, 1);
}
public void addButtonGUI(JPanel panel, JButton button, String text, int x, int y){
GridBagConstraints gbc = new GridBagConstraints();
button.setText(text);
button.setEnabled(true);
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 2;
gbc.weightx = 1.0D;
gbc.fill = 2;
panel.add(button, gbc);
}
What am I doing wrong or is there a better way to do this?
Please help
You need to set the layout of the JPanel to GridBagLayout to use GridBagConstraints:
JPanel panel = new JPanel(new GridBagLayout());
Also as you only have one effective 'cell' you need to use an anchor and set weighty for the JButton to allow movement in the Y-axis.
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weighty = 1.0;
Also I would set the fill setting to NONE:
gbc.fill = GridBagConstraints.NONE;
so that the button does not occupy the full width of the panel. (2 = HORIZONTAL fill).
instead of
addButtonGUI(panel, new JButton(), "test", 1, 1);
}
what would happen if you used
addButtonGUI(panel, new JButton(), "test", 0, 0);
}