I am making Calculator app.
I want digits (Number)to be written on the right side of textArea and signs(+,-,/,*) on the left side of same textArea.
How to do this?
I do not think you can have multiple alignments inside textarea, but instead you can use JTextPane here is an exemple Multiple Alignement or create your own calculator display :
1-create a container (BorderLayout for exemple)
2-add two JLabels to the container to display text dynamically
3-make the first JLabel with right Alignement setHorizontalAlignment(SwingConstants.LEFT)
4-make the second JLabel with left Alignement
setHorizontalAlignment(SwingConstants.RIGHT)
I hope this is what you are looking for, after all this is just a suggestion !
Related
I have a JPanel that can have either 1 or 2 buttons, depending on what's going on in the program at the time. I'm using Box Layout.X_AXIS to line up the 2 buttons configuration, and it works great. When I switch to 1 button however, the single button is on the far left of the window. I've tried a bunch of different things to get the button centered, but the only thing that seems to work is this:
JButton yesBtn = new JButton("Continue");
btnPane.setLayout(new BoxLayout(btnPane, BoxLayout.Y_AXIS));
yesBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
The problem with this is that the vertical location of the button changes doing it this way, so it doesn't match where the 2 buttons would sit vertically. Is there a way to center the button using the X_AXIS layout?
I've tried a bunch of different things to get the button centered,
The easiest way is to add "glue" BEFORE the first component and AFTER the last component. Then it will work for 1 component or multiple components.
Read the section from the Swing tutorial on Invisible Components as Filler for more information and examples.
I'm making a JFrame that contains a lot of JButtons and JTextfields which contain data from a database. In the design everything is okay, but when I run my program the JButton and the JTextfield change their places and I don't know why.
Here is a screen shot from the design window and the run window:
Seems like you have aligned the textfields to each other and the buttons too. But you should have aligned one button to one textfield. One way to do this would be to use a GridBagLayout instead of the FreeDesign option in NetBeans. To do this right-click onto your frame and select Set Layout>Grid Bag Layout.
You can then right-click again and select Customize Layout... in order to place your components as you wish.
I'm working on a simple app that has:
2 TextFields with Labels: name and description
Add button
Panel that is filled with name/description labels and with radio buttons to select records and manipulate with them (edit / delete -> these buttons appear when radio button is selected).
I tried the following layouts composition:
2 Panels with BoxLayout (YAXIS) which contain text field and their labels
Panel with GridBagLayout to have 2 panels with text fields and a button to add data.
Panel with BoxLayout (YAXIS) to be filled with records after button press
Panel with GridBagLayout that contains radio button and 2 labels with results of name and description text fields.
Here is a screenshot of what I came to:
As you can see there's a problem - Labels have no word wrap. If I use JTextArea for word wraping, then white background appears (setBackground(null) doesn't help).
I think that GridBagLayout is not good choice here or even there are too many inner panels. In fact it seems this task is common enough. What are good practices for apps UI building like that one?
The Problem you got with your JLabel is easy to handle. Because JLabels accept HTML Tags you can use this mechanic to automaticly wrap your text:
label.SetText(String.format("<html><div WIDTH=%d>%s</div><html>", width, text));
For the general Layout I prefer the GridbagLayout. With the GridbagLayout you only need on Layout-Typ. GridBagLayout is flexible and easy to use for your case.
If the Layout will get more complex you will probably need to stack different Layouts, to get the best handling.
I am trying to recreate the Skype handles instant messaging using Swing components. I am using JList with a custom ListCellRenderer to render each cell in the list. The ListCellRenderer extends a JPanel, the JPanel simply contains a label (where I will put the username) and a JTextArea which is where the users' messages will go. The JTextArea is what Im having problems with.
Here's an image of what I have a the moment -
Ive removed the scrollpane that automatically comes with the textarea in netbeans.
I am showing the Navigator, the Design view and the actual program (the list has two elements) in this image.
The the text in textarea is actually much longer than in this image but it is not word wrapping. I have set lineWrap and wrapStyleWord to true in the properties box for this textarea but it doesn't seem to take any effect. I then tried to set maximum size using the properties box and that doesn't have any effect either.
Is there any way to control the padding/margins around components with netbeans gui designer. The automatic placement it gives me for spacing between components is either several pixels too small or two large. I need exact placement on the list's cell components.
For reference here is how skype's convesation panel looks (ive added in the red "Brian cs"'s as thats how I will be doing it in my program). As you can see the sentences wrap and there is an appropriate amount of space between cells. So anyone know how to achieve this using Swing?
The JTextarea is not the problem. The JList sets the heigth for each row. See JList.setFixedCellHeigth or setPrototypeCellValue
My original plan today was to learn how to use the JProgressBar, but now I am stuck with a new problem which I was not expecting. So here is how my JFrame looks like right now.
The problem is that the JTextField right under the JTextArea is not wrapping around. It is going beyond the size of the JFrame. I want it to resize relative to the JFrame. How can i do that? I have tried the JLabel but it does the same thing, only it adds ellipses in the end where the text overflows.
Try using a JTextArea if you intend for the field to expand vertically to accommodate the text.
JTextFields are intended for a single line of text.