jlabel

JLabel ToolTip interferes with MouseListener

删除回忆录丶 提交于 2019-12-05 08:42:59
I have Java Swing application ToolTipMouseTest The critical line is label.setToolTipText("label" + i); . Once it is commented out very click on a label produces 2 mousePressed in console. With this line enabled click on labels would produce nothing. Is this expected behaviour or a bug? My goal is to show tooltips without disabling MouseListener from working. Almost SSCCE, but without imports: public class ToolTipMouseTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ToolTipMouseTest(); } }); } public ToolTipMouseTest()

How to rotate Swing text?

99封情书 提交于 2019-12-05 06:22:24
Is there a way to rotate Swing text such as in a JLabel between 0 and 360 (or between -180 and 180) degrees in 1 degree steps? Yes. Look at Graphics2D.rotate(). For a JLabel, I think you could override the paintComponent() method to call rotate(x), then call the existing paintComponent(), then call rotate(-x). e.g. protected void paintComponent(Graphics g) { Graphics2D g2 = ( Graphics2D )g; g2.rotate(theta); super.paintComponent(g2); g2.rotate(-theta); } I haven't tried this. You might need to add an offset, see Graphics2D.rotate(double theta, double x, double y) I do not believe that Swing

JLabel hyperlink to open browser at correct URL

拈花ヽ惹草 提交于 2019-12-05 01:44:02
I need to create a label with Java Swing that is clickable and able to open the default browser on the desktop and redirect it to a specific url. My code is able to open up the browser but not redirecting it to the correct url (the default home page is loaded). My test code: import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.IOException; import java.net.*; public class LinkTest extends JFrame { public LinkTest() { JPanel p = new JPanel(); JLabel link = new JLabel("Click here"); link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); link.addMouseListener

Two icons in a JLabel?

一世执手 提交于 2019-12-05 01:15:22
I have an icon in a JLabel as shown below: Is it possible to add another icon (e.g. a flag representing a country) between the color icon and the text? For example, I want to add an icon depicting the U.S. flag between the red icon and US . Thanks! Yes, use nested JLabel with BoxLayout in the container label: JLabel container = new JLabel(); container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS)); JLabel icon1Label = new JLabel(); JLabel icon2Label = new JLabel(); icon1Label.setIcon(icon1); icon2Label.setIcon(icon2); container.add(icon1Label); container.add(icon2Label); Try

which JLabel has been clicked?

和自甴很熟 提交于 2019-12-04 21:23:34
I have 6 JLabel and each is having a different instance of a mouselistener class attached. How to know which JLabel has been clicked ? These JLabel form a two dimentional array. John Snow You use getSource to get a refrence to the object which is clicked on: label1.addActionListener(new yourListener()); label2.addActionListener(new yourListener()); public class yourListener extends MouseAdapter{ public void mouseClicked(MouseEvent e){ JLabel labelReference=(JLabel)e.getSource(); labelReference.someMethod(); } } The easiest way I did something like that was, to use JButtons and make them look

JLabel animation in JPanel

纵饮孤独 提交于 2019-12-04 19:28:41
After scratching around I found that it's best to implement a custom image component by extending a JLabel. So far that has worked great as I can add multiple "images" (jlabels without the layout breaking. I just have a question that I hope someone can answer for me. I noticed that in order to animate JLabels across the screen I need to setlayout(null); and setbounds of the component and then to animate eventually setlocation(x,y); . Is this a best practice or a terrible way to animate a component? I plan on eventually making an animation class but I don't want to do so and end up having to

How to display changing text in Java Swing (ie, display time with changing seconds)

江枫思渺然 提交于 2019-12-04 16:49:25
I'm looking for a way to display the current date and time on a JFrame as a JLabel but have it update automagically. Once it is drawn to the pane, I shouldn't need to worry about manually updating it because it will refresh say, every 2 seconds. Is there a proper way of doing this? Instead of manually changing the JLabel's text value every 2 seconds. Use the javax.swing.Timer to ensure that updates to the Swing component occur on the EDT. Timer t = new javax.swing.Timer(2000, new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ yourLabel.setText(...); } }); t.start();

How do you stop a JLabel changing its size when its text changes?

≯℡__Kan透↙ 提交于 2019-12-04 10:12:29
I'm generating some JComponents in code and using the GridBag layout to arrange them. My layout consists of 12 rows and 3 columns, with each row consisting of a JSlider, a JCheckBox and a JLabel. Here's the code I'm using to generate the UI: final int NUM_MOTORS = 12; // This is the panel I'm adding the components to. pnlMotorSliders.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); for (int i = 0; i < NUM_MOTORS; ++i) { c.gridy = i; // Create the slider JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 10, 4085, 10); c.fill = GridBagConstraints.HORIZONTAL;

background image hidding the other components like buttons labels and other, and vicce versa

ぐ巨炮叔叔 提交于 2019-12-04 07:35:20
问题 how to solve hidding of components in this code code is running without errors but background image not displayed how to change code to get the background image when using validation method, its creating error in validation() public class TEST{ public TEST() { String[] strm = {"Jan", "Feb", "Mar", "Apr", "May"}; String[] stry = {"2016", "2017", "2018","2019"}; String[] strf={"NEW Delhi", "Bangalore", "Chennai"}; String[] strt={"Goa","Kashmir","Hyderabad"}; JFrame f = new JFrame("test"); f

Use variables from other class from other file to another in java

心不动则不痛 提交于 2019-12-04 06:27:36
问题 There are two players who will input their name in the JTextFields. What I want to do is that the data I entered from the Welcome frame in Enter.java will be transferred to the JLabels in ActualGame.java. import java.awt.*; import java.awt.event.*; import javax.swing.*; import static javax.swing.JFrame.EXIT_ON_CLOSE; public class Enter extends JFrame implements ActionListener { private String one = ""; private String two = ""; private JTextField txtOne = new JTextField(); private JTextField