I am trying to create JButton such as there must be a number painted on the top and right corner of JButton.
For example, in case of notification buttons there is a message in the button, how is that possible? Can the help be taken of paint method to draw the label?
there are three ways, by using
GlassPane
JLayer (Java7) based on JXLayer(Java6)
JButtton (all JComponents) is container too, there is easy and possible use standard LayoutManagers (only JFrame == BorderLayout and JPanel == FlowLayout have got implemented LayoutManager in API directly), then basically everything is possible
JButton and any JComponent extend Container class, so you should be able to add elements into JButton as if it were a simple panel. So in your case you can add a JLabel with your text into a button.
Also consider implementing Icon to decorate the button; ColorIcon is a simple example. You can use the color to signify buttons that are likely to need attention, and you can use drawString() specify a number.
Related
I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".
I'm writing a Chinese Chess program in Java and would much appreciate some guidance on designing/implementing the GUI.
The board is to be divided into a 9x10 grid, with an "image" of the chess piece occupying each cell. The cells also need to be 'registered' when clicked so I know which piece was clicked.
1) I'm thinking GridLayout for the layout manager for the JPanel representing the board. How do I add an image to each component?
public void paintComponent(Graphics g) {
Image dog = new ImageIcon("dog.png").getImage();
add(dog)
}
Does not work as dog is not a Component.
2) How would I register for clicks in each cell?
Use a JLabel containing an Icon. Then add the label to the grid layout. Read the Swing tutorial on How to Use Icons for more information.
Also read the section on How to Write a Mouse Listener for listening to clicks on the label.
Or you could use a JButton with an Icon and then use:
button.setBorderPainted(false);
so you don't see the action of clicking the button. Then you would use an ActionListener. The tutorial also has a section on using an ActionListener.
Yes, GridLayout seems appropriate for this use.
See the constructor JButton(Icon).
See this answer for an example that carves up an existing image tile set for use in JLabel and JButton instances.
I have to realize this design with Java Swing (see screen shot).
Is it possible? How can an element appear off the frame?
Add a JToolBar (non-floatable) to JFrame or parent JPanel (in the BorderLayout.NORTH position).
Put an Icon inside a JButton and add to tool bar.
Set "MODE DIAL" (or whatever) as the tool tip for the button.
Repeat for each icon/button required.
Use a PLAF to tweak the look.
You need to use a JWindow to contain your component.
Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/DropTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
The Component Mover can do this for you.
Use the GlassPane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.
I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).