jcomponent

Drawing a JComponent inside a JPanel

梦想与她 提交于 2019-12-06 14:10:23
I am trying to display a JComponent inside a JPanel. I am using null layout because location of the components can be changed during runtime and I have to control them. But the following code does not work. The JComponent only becomes visible on display if I explicity call the "paintComponent" method, which I think is not good practice. My JComponent Class public class MyIcon extends JComponent { private double xPos; private double yPos; private double radius = 30; public MyIcon(double xPos, double yPos) { this.xPos = xPos; this.yPos = yPos; this.setBounds((int)xPos, (int)yPos, (int)radius,

Change cursor for all buttons on Swing app

醉酒当歌 提交于 2019-12-06 05:43:42
问题 I have a Swing app with a main frame and some other forms loaded inside it. I Need to implement a general method to set the hand cursor for all buttons on any form. This is similar of what we do with css on web pages ( input[type=button] { cursor:pointer; } ) 回答1: Walking the tree like @Madprogrammer suggested is the method if you want to change the cursor dynamically and/or on a particular form. Just for fun (and to show-off SwingX again :) - if you want to install that cursor globally and

Drawing multiple graphic2d components into JPanel

我们两清 提交于 2019-12-06 04:13:12
I've read a lot of tutorials on drawing Graphics2D components and adding to JPanel/JFrame but I can't find how to add multiple these components into one JPanel simply. My code below adds only 1 component (line) and I can't find why it isn't possible to add more. What am I doing wrong? Desired behaviour: there should be 3 red lines. My whole code: package Examples; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.BorderFactory; import

How to center text and a JComponent in a JTextPane vertically?

谁说胖子不能爱 提交于 2019-12-05 18:09:04
Currently it looks so What to do so that it looks so? Below is my code: JFrame f = new JFrame(); JTextPane textPane = new JTextPane(); JTextField component = new JTextField(" "); component.setMaximumSize(component.getPreferredSize()); textPane.setSelectionStart(textPane.getDocument().getLength()); textPane.setSelectionEnd(textPane.getDocument().getLength()); textPane.insertComponent(component); try { textPane.getDocument().insertString(textPane.getDocument().getLength(), "text", new SimpleAttributeSet()); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace()

Make a simple GUI editor in Java Swing using Swing components

雨燕双飞 提交于 2019-12-05 11:09:48
I'm currently working on a project in which I need a very simple editor for GUI-like objects. This editor would be a canvas on which well known GUI widgets can be placed. For example one can place a button and a textfield on there, move them around and resize them. No interaction with the widgets themselves is needed. I've been trying to accomplish this by adapting a very simple paint tutorial, I thought it would be easy to implement it this way, yet I bump into many problems with drawing custom shapes and text on a canvas and dragging and dropping those shapes. I was wondering if I could

The JPanel contentpane confusion

旧街凉风 提交于 2019-12-05 08:47:39
I am learning Java Swing and I appended a menuBar to the frame. By default this should call jframe.getContentPane().add(child) . When I ran the script the menuBar didn't show up. But the button was at the very top "y=0" if that makes sense. Then I realized my mistake I actually had to put in a menu in the menubar. Then the menuBar showed up. So that got me thinking...is the "menubar" "contentpane" actually 2 panels? It is confusing the hell out of me. Because that acted a lot like a panel. But getContentPane() returns a Container, not a JPanel object so I'm confused. If so, does that mean that

Overriding getPreferredSize() breaks LSP

微笑、不失礼 提交于 2019-12-05 01:37:43
I always see advices in this site of overriding getPreferredSize() instead of using setPreferredSize() as shown in these previous threads for example. Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Overriding setPreferredSize() and getPreferredSize() See this example: public class MyPanel extends JPanel{ private final Dimension dim = new Dimension(500,500); @Override public Dimension getPreferredSize(){ return new Dimension(dim); } public static void main

Java get JPanel Components

送分小仙女□ 提交于 2019-12-05 00:59:25
I have a JPanel full of JTextFields... for (int i=0; i<maxPoints; i++) { JTextField textField = new JTextField(); points.add(textField); } How do I later get the JTextFields in that JPanel? Like if I want their values with TextField.getText(); Thanks Well bear in mind they didn't get there by them selves ( I think a read some questions about dynamically creating these panels at runtime ) In the answers posted there, someone said you should kept reference to those textfields in an array. That's exactly what you need here: List<JTextField> list = new ArrayLists<JTextField>(); // your code... for

How does JComponent.paintImmediately() work in Java Swing?

百般思念 提交于 2019-12-05 00:56:29
My understanding: Unlike most of the components/operations in Swing call to JComponent.repaint() is thread-safe i.e. though a repaint request is made from another thread (i.e. not from EDT), the actual painting happens in EDT only. Below code snippet demonstrates this. public class PaintingDemo { public static void main(String[] args) { final JFrame frame = new JFrame(); final JPanel p = new MyPanel(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { frame.add(p, BorderLayout.CENTER); frame.setSize(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame

Change cursor for all buttons on Swing app

人走茶凉 提交于 2019-12-04 09:53:06
I have a Swing app with a main frame and some other forms loaded inside it. I Need to implement a general method to set the hand cursor for all buttons on any form. This is similar of what we do with css on web pages ( input[type=button] { cursor:pointer; } ) Walking the tree like @Madprogrammer suggested is the method if you want to change the cursor dynamically and/or on a particular form. Just for fun (and to show-off SwingX again :) - if you want to install that cursor globally and then don't care about, install a ui-delegate which takes care of it. In SwingX, it's as simple as