Create a JLabel dynmaiclly - java

Hiļ¼Œthere is a problem in my java application, the text in my Jbutton is too long so it only show"..." on the button. Now I want to add some component to help show the actual text on the button.
What method can I use to solve such problem rather than adjusting the text font or button size?
Thank you

Instead of a label, you should add a tooltip to the button:
jb.setToolTipText("The full text of the button");
A small downside is that this tooltip will also be shown if the button text is fully visible, and in that case the tooltip doesn't provide any additional information, which will be confusing. I don't know off my head how to solve this, but it's definitely possible.

Related

changing font size for specific text in jtextpane

Ok, I am writing a text editor using a JTextPane. I am trying to figure out how to change the font size for specific text that the user types in. I have a JButton that the user can click to bring up a font-size and font-type selection menu. When the user selects the proper font size and type and presses ok, I then proceed to get the font size and type inside a Font. All I need to do now is to set the Font of the selected text in the JTextPane without overriding the current style(s) of the text. I haven't found anything that enables me to do this. Maybe I just missed something somewhere...
Thanks in advance.
Note: I am a newbie on StackOverFlow so please edit my question if I did something wrong.
Did you tried with this.
replaceSelection()
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html#replaceSelection(java.lang.String)
Use a styled editor kit and fire the actions inside the actionperformed method.

I want to update a particular image on TextArea(swing) ,when I enter a particular Alphabet on my keyboard. How?

I am trying to print particular images(which include some drawn symbols) on textarea when I type a particular Alphabet on my keyboard. How?
You can't use a JTextArea since it can only display text.
You can use a JTextPane as it supports the display of Icons.
Check out: Auto Replace Smiles. It shows how to replace the two text characters :) with an Icon.
You are asking me to do your homework, but still. I will give you only the pseudo code, you will have to write it on your own.
Write a JFrame.
Add a JLabel to the JFrame (using a label is easier than using a text pane, and it is impossible to use a text area).
Add a Key Listener to either the JFrame or the JLabel depending on your application.
In the handler write code to add a ImageIcon to the JLabel when the key you want is pressed or whatever.
Cheers!

SWT button position after resizing

I'm working with SWT. I created a button, and it supposed to change its text after being pressed.
The second text is wider than the first one, and the button does not being re-sized appropriately.
so I used button.pack(), and setSize() functions. The real issue is actually with its alignment in the shell after this size changing.
it's being expanded to the right instead to the left.
I guess it's actually the normal behavior, but I'm not sure where to change it.
I tried to change things by creating a gridData but it only did worse.
The button parent is tabFolder, and the last consists of a gridlayout with two uneven columns, the spesific button is on the second column.
I tried to change things with the margins and few others, but without success.
currently all the configurations are default.
This is my button:
This is how I would like it to behave:
any help would be highly appreciated.
many thanks!
i would need some code to be sure, but it looks like you need to call tabFolder.layout(true) after changing the button text to compute the new position of your button

Keeping selection after clicking a JButton to style text

I'm making a fairly simple text editor, and I have a question about my style buttons. When I highlight text and click my "bold" button, the text bolds as expected, but my selection is not longer visible. I can still unbold the selection, italicize it, or underline it, but you just can't see what is selected. So, I'm wondering if there is a setting that will allow me to click the button, but keep my selection? I tried a JMenuItem instead of a JButton, and that seemed to work, but then it made my toolbar look quite bad. Sample code below.
//frame and pane creation up here
JToolBar tool = new JToolBar();
JToggleButton boldButton = new JToggleButton("Bold");
boldButton.addActionListener(new StyledEditorKit.BoldAction());
tool.add(boldButton);
Any help is appreciated.
So, I'm wondering if there is a setting that will allow me to click the button, but keep my selection?
boldButton.setFocusable( false );
As you noticed, the selection is still there but clicking on the toolbar button removes the focus from the text pane and hides the selection. You need to set the focus back using requestFocus. However, you will need to write your own action listener to add the focus code - you could extend BoldAction to do this.

JButton with text below the icon

In Java Swing I would like to create a JButton which contains an image(icon) and some text. In few words a JButton(text,icon).
I would like the text to be displayed below the image, and not beside, as in normal JButton layout. Does anyone have an idea on how to achieve this?
Have a look at the method setVerticalTextPosition in AstractButton using the constant
SwingConstants.BOTTOM.

Categories