I have been trying to create a chat client which makes use of a scrollable JPanel and at run-time adds JTextField objects to the panel.
Panel Initialising:
msgPanel = new JPanel(new MigLayout());
msgPanel.setPreferredSize(new Dimension(400,335));
JScrollPane msgPanelScroll = new JScrollPane(msgPanel);
msgPanelScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
msgPanelScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
mainPanel.add(msgPanelScroll,"span 3,alignx center,wrap");
Adding text at run-time
msgDisp = new JTextField();
msgDisp.setHorizontalAlignment(JTextField.RIGHT);
msgDisp.setEditable(false);
msgDisp.setText(msgField.getText());
msgPanel.add(msgDisp,"wrap,pushx,ax right");
msgPanel.revalidate();
But now I'm facing the following issues. The text field aligns to the center of the panel & on adding more fields the panel accommodates as it can and does not begin scrolling.
EDIT:
MigLayout issue fixed by using pushx and ax
mainPanel.add(msgPanelScroll,"span 3,ax center,wrap,w 400,h 335") fixed the size issue
Found my answer.
Related
I'm trying to fix the height of the "amountField" text field, but I can't.
I would like the height of amountField to have the same height as the JComboBox that it's above, so it looks better.
Right now, the JTextField looks very tall compared with the rest of design.
I've tried everything that I've read in this forum, but nothing seems to work.
I don't know if it's relevant, but this whole JPanel (WithdrawalScreen) is inside another JPanel with BorderLayout. This panel is the center part of it
Thanks
PictureHere
public class WithdrawalScreen extends JPanel {
Public JPanel init() {
this.setLayout(new GridLayout(0,1));
account = new JLabel("account");
accountSelect = new JComboBox(labels);
amount = new JLabel("amount");
amountField = new JTextField("");
submit = new JButton("SUBMIT");
this.add(account);
this.add(accountSelect);
this.add(amount);
this.add(amountField);
this.add(submit);
return this;
}
}
Try creating the Grid Layout with 5 rows and 1 column. I think the height is messed up because you are not setting the constructor arguments properly.
new GridLayout(5,1);
Grid layout will stretch the component and give the same size to all of its components. In order to keep the "default" size of each component, you can use BoxLayout with BoxLayout.Y_AXIS parameter in its constructor. Another way would be to use a dummy-nested JPanel with another layout. Let's say FlowLayout.
JTextField textField = new JTextField(10);
JPanel nestedPanel = new JPanel(new FlowLayout());
nestedPanel.add(textField);
gridLayoutPanel.add(nestedPanel);
JTextField will not be stretched. nestedPanel will be. Do some experiments yourself and you will find the way that fits your needs.
A link that will help you: A visoual guide to Layout Managers.
I want to add the possibility for my users to add a comment on a form. To display them, I created JPanel inside a simple JScrollPane. I set the layout of this JPanel to BoxLayout because I wish to add them all in only one column and it seemed to be the easiest way by calling BoxLayout.Y_AXIS in the constructor. I also tried GridLayout and GridBagLayout but it was not what I was looking for.
My problem is that when a JPanel has the BoxLayout layout, it's width automatically is the same as it's container, but my container is a JScrollPane and the caret hides the right side of my comment!
You can see the JTextField and a JButton on the bottom left, here's the code on the click event :
private void btnAjoutCommentaireActionPerformed(java.awt.event.ActionEvent evt) {
//I take the text from the JTextField and format it to html
String formattedComment = "<html><br><div style='width:280px;'>" +
txtArCommentaire.getText().replaceAll("\n", "<br>") +
"</div><br></html>";
JLabel label = new JLabel(formattedComment);
//I add a blue border
label.setBorder(new TitledBorder(new EtchedBorder(Color.lightGray, Color.blue), ConfigUser.getCu().toString()));
//this below doesn't work
label.setSize(280, 200);
//I tried adding a JPanel in between but it didn't really worked out
//JPanel panel = new JPanel();
//panel.setLayout(new GridLayout(1, 1));
//panel.setSize(297, 200);
//panel.add(label);
///pnlCommentaire is the JPanel inside the JScrollPane
pnlCommentaire.setLayout(new BoxLayout(pnlCommentaire, BoxLayout.Y_AXIS));
pnlCommentaire.add(label);
pnlCommentaire.revalidate();
pnlCommentaire.repaint();
}
As you can see I tried to adust the size in html using style='width:280px'and on the JLabel using label.setSize(280, 200); but none of them worked.
Do you have any idea on how I could resize this Jlabel?
EDIT :
I added a margin-right property to the div so that I can at least fully see the text in the JLabel but the right border is still hidden.
String formattedComment = "<html><br><div style='width:280px;margin-right:50px;'>" +
txtArCommentaire.getText().replaceAll("\n", "<br>") +
"</div><br></html>";
Everytime I click a button in my program, I add 5 new rows to the JPanel. Eventually, the rows overflow and I would like to add a JScrollPane so I can scroll down and see the new rows.
I know how to get it working for a TextArea but I can't seem to figure out how to make it work when I have a GridBagLayout. Below, I will attach the code for setting up my panels.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm);
JScrollPane scrollpane = new JScrollPane(panelForm);
panelMain.add(scrollpane);
When I run, my code I get a box enclosing the GridBagLayout, but the scrollpane is nowhere to be seen.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
You would typically add the scroll pane directly to the frame. There is no need for the "panelMain".
panelMain.add(panelForm);
That line is not needed. A component can only belong to a single parent. You later add "panelForm" to the scrollpane.
So the basic code would be:
JPanel panelForm = new JPanel(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane(panelForm);
frame.add(scrollpane);
but the scrollpane is nowhere to be seen.
The scrollbars only appear when needed by default. Adding empty panels will not cause the scrollbar to be displayed. Click on your button a few times and add your child components to the "panelForm" using the appropriate GridBagConstraints. You will then need to use:
panelForm.revalidate();
panelForm.repaint();
The revalidate() causes the layout manager to be invoked so the scrollpane can determine is scrollbars are required or not.
I'm developing Burp extension and add additional tab. I have to return java.awt.component, so i decided javax.swing.JPanel would be nice. It must be a JLabel and JTextField on my Tab, code here:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,Y_AXIS));
JLabel label = new JLabel("hostname : ");
panel.add(label);
JTextField tf = new JTextField("text");
panel.add(tfHost);
I wanted little text and textfield on top left, but my TextField stretched on all my screen. What do i have to do to fix it? Maybe i have to change layout manager?
The problem is a BoxLayout will allow components to grow to fill the available space to the panel.
So the easiest solution is to add your panel to another panel that will respect the size of the BoxLayout panel.
Something like:
JPanel wrapper = new JPanel(); // uses FlowLayout by default.
wrapper.add( panel );
frame.add( wrapper );
Now when you add the wrapper panel to the frame, the wrapper panel will grow in size, but it will not affect the components added to the wrapper panel.
So I've been learning Java for the very first time and it's time for me to attempt my first project. And I'm stuck at the "first hurdle" haha.
The issue I have is the fact that I don't actually know how to space J Items apart.
I have a 250,350 window for a Log In form with a JLabel, a JTextField for username and JLabel JPassword for Password with a JButton at the bottom.
What I want to do now is style it so that the spacing between the top and the bottom of the form makes it so that the form is centered as well as adding a line's height space between the JLabel and the JTextField. (Basically a \n type deal but that isn't working.)
Hopefully this makes sense, if not, I apologise and I'll try to rephrase/add code!
public Game() {
this.setSize(250,350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Sticket Cricket - Login");
JPanel loginMenuPanel = new JPanel();
loginButton = new JButton("Login");
usernameField = new JTextField();
usernameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.requestFocus();
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel("Password: ");
this.add(loginMenuPanel);
loginMenuPanel.add(usernameLabel);
loginMenuPanel.add(usernameField);
loginMenuPanel.add(passwordLabel);
loginMenuPanel.add(passwordField);
loginMenuPanel.add(loginButton);
this.setVisible(true);
}
Short Answer:
Create a JPanel, set the layoutmanger of the panel (some examples, GridLayout, BorderLayout, Check out the tutorial here where more of these are explained)
Then add your components to this panel accordingly
For the layout you are looking for it would possibly be easier to use an IDE to create this, I find Net Beans to be the easiest for doing this.
My recommendation would be for you to create a JPanel with a grid layout of 2 columns and 2 rows, to this add you JLabels and Text fields for the logon name and password.
Then create another JPanel possibly BorderLayout or Flow Layout and add the above panel to this then add this parent panel to the frame.