jlabel

How to right-justify icon in a JLabel?

六眼飞鱼酱① 提交于 2019-11-26 14:42:58
问题 For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING) , the icon is painted right after text, no matter how wide the label is. This is particularly bad for a list, as the icons would be all over the place depending on how long the text is for each item. I traced the code and it seems to be that in SwingUtilities#layoutCompoundLabelImpl , text width is simply set to SwingUtilities2.stringWidth(c, fm, text) , and icon x is set to follow text without considering label

Can I have a textfield inside a label?

别来无恙 提交于 2019-11-26 13:53:39
What I would like to do is display the following in a form: Open [15] minutes before class Where [15] is a text-field. Is this possible? Use a 'composite component' by adding the required parts to a JPanel . E.G. import java.awt.FlowLayout; import javax.swing.*; class TimeBeforeClass { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JPanel gui = new JPanel(new FlowLayout(FlowLayout.LEFT, 3,3)); gui.add(new JLabel("Open")); gui.add(new JSpinner(new SpinnerNumberModel(15,0,20,1))); gui.add(new JLabel("minutes before class")); JOptionPane

How to set Icon to a JLabel from an image from a folder?

让人想犯罪 __ 提交于 2019-11-26 13:52:28
I am trying to set an icon to a JLabel from a folder of images whenever an item is selected from a JComboBox. The name of items in the JComboBox and name of the images in the folder are same. So whenever an item is selected from the JComboBox, the corresponding image with the same name should be set as an icon to the JLabel. I am trying to do something like this. private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){ updateLabel(cmb_moviename.getSelectedItem().toString()); } protected void updateLabel(String name) { ImageIcon icon = createImageIcon("C:\

Update a Label with a Swing Timer

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 12:49:22
问题 I\'m having some trouble with this piece of code. I\'m starting a timer with a random number, and I want to update a JLabel with the countdown, every second. But I haven\'t figured out how to do so, since the only listener the timer triggers is at the end of it (that I know). here\'s the code: int i = getTimer(maxWait); te1 = new Timer(i, this); label.setText(i+\"\"); te1.start(); ... public int getTimer(int max){ Random generator = new Random(); int i = generator.nextInt(max); return i*1000;

Rotate JLabel or ImageIcon on Java Swing

删除回忆录丶 提交于 2019-11-26 12:47:24
问题 first of all, this is the first week that I use swing, then sorry if my question is too obvious. Also, I need solutions that use the standard java libraries, since this is for homework and I\'m not allowed to use strange libraries. I\'m using JLabel with ImageIcon to show images on a JFrame. Now I want to rotate the image on screen to arbitrary angles. I found something about Graphics2D but i don\'t find the way to do that. Since solutions that I found doesn\'t work or I don\'t understand

Image resizing and displaying in a JPanel or a JLabel without loss of quality

南笙酒味 提交于 2019-11-26 12:31:23
问题 I\'m developing a java program to capture employee images at the time of registration using a webcam. I can obtain the picture without any problem, and save it in my C: drive but upon retrieval of the image only a part of the image is displayed on the label. Is there a way of re sizing the JPEG before saving it? or before displaying it? like shrinking it without a quality loss.... Thankz a lot Cheerz! :)! okay guys... here goes:- I have commented the code in the way I have used them. //This

Multiline text in JLabel

拜拜、爱过 提交于 2019-11-26 11:52:32
How can I make the text of a JLabel extend onto another line? You can do it by putting HTML in the code, so: JFrame frame = new JFrame(); frame.setLayout(new GridLayout()); JLabel label = new JLabel("<html>First line<br>Second line</html>"); frame.add(label); frame.pack(); frame.setVisible(true); if you want your jLabel Text to resize automaticly for example in a stretchable gridbaglayout its enough just to put its text in html tags like so: JLabel label = new JLabel("<html>First line and maybe second line</html>"); Web Think Type the content (i.e., the "text" property field) inside a <html><

Newline in JLabel

谁说我不能喝 提交于 2019-11-26 11:48:34
How can I display a newline in JLabel ? For example, if I wanted: Hello World! blahblahblah This is what I have right now: JLabel l = new JLabel("Hello World!\nblahblahblah", SwingConstants.CENTER); This is what is displayed: Hello World!blahblahblah Forgive me if this is a dumb question, I'm just learning some Swing basics... freitass Surround the string with <html></html> and break the lines with <br/> . JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER); You can try and do this: myLabel.setText("<html>" + myString.replaceAll("<","<").replaceAll(">", "

Stretch a JLabel text

偶尔善良 提交于 2019-11-26 10:57:19
Is there a way to make a JLabel's text stretch to 100% height? I need the text to update when the component's size changes as well. I saw some solution that could work; It involved calculating and setting the font size so it appears the right height. I would have also have to add listeners for when the height changed to make it respond and I do not know exactly where I should do that. I am hoping for a better solution with layout managers, but couldn't find anything. Any ideas? trashgod In the approach shown below, the desired text is imaged using TextLayout using a suitably large Font size

How returns XxxSize from JComponent(s) added to the JLabel

放肆的年华 提交于 2019-11-26 10:56:57
how can I correctly returns XxxSize from JComponent(s) added to the JLabel 1st. figure >> lets LayoutManager works like as for JPanel, JLabel returns Size(0, 0) 2nd. figure >> added some PreferredSize to the JLabel 3rd. figure >> calculated PreferredSize from JComponent(s) added to the JLabel 4th. figure >> lets LayoutManager works changed JLabel to JPanel, now LayoutManager correctly calculated Dimension without using any XxxSize notice sice there is used Nimbus L&F, same output is there for all accesible L&F import java.awt.*; import java.awt.event.*; import java.util.LinkedList; import java