I'm running into a problem that I can't seem to figure out nor find the answer anywhere on the web.
I've got a JLayeredPane and when it only has one child Panel I am able to correctly set the cursor using setCursor(). The cursor shows up and everything is fine. But when I add an additional JPanel into the JLayeredPane the cursor no longer shows up
for example this works:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
// m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // WORKS
but this doesn't:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // FAILS
Anyone know how i can get custom cursors working within a JLayeredPane
If you take a look at javax.swing.JLayeredPane source code, you will see its constructor defined like that:
public JLayeredPane() {
setLayout(null);
}
which clearly indicates that it needs to handle components layout by itself.
Hence you can guess (although it is not documented, I would consider it a documentation bug) that you should not change the layout of JLayeredPane.
While the topic is old, none of the answers was satisfying. I resolved the problem calling to the setCursor method of the JLayeredPane in this way:
this.getParent().setCursor( Cursor.getDefaultCursor() );
Where "this" is the component I want to change the cursor to. Its parent is the JLayeredPane (since it is added to it).
Works fine for me when using the demo code the the How to Use Layered Panes tutorial.
Based on the 3 lines of code the only difference I can see from the tutorial is that you are using a layout manager.
Compare your code with the tutorial to find other differences.
Have you tried taking the first working code, but placing the m_mediaPanel on level 1? This probably won't work either. I think this is due to the fact that the panel that is on top determines the cursor. On level 0 the layered pane itself can determine this.
Related
I am in the process of creating my first JavaFX application. I use SceneBuilder to design the GUI. I am facing the following problem:
I have dragged a Pane inside the GridPane. The Pane seems to have to follow the constraints given by RowConstraints and ColumnConstraints. The problem is that these are not aligned with my pane:
I did not expect this to be a big problem, but this results in a gap that is visible when I run my code:
I am talking about the white-colored part in this figure - I want this to be blue. Also, I want to see the entire pane in my window.
I played around and tried to figure this out on my own, but I don't see what I am doing wrong. Any ideas?
What seems to fix the issue is the following:
Inside the Inspector, open the Layout-entry for ColumConstraints. Change HGrow and HAlignment from INHERIT to ALWAYS and LEFT, respectively.
But if anyone knows more about this, feel free to post a separate answer!
Im making a small program that shows teams/ players/ then their twitter feeds, tweets, etc. My problem seems to be unrelated to that though, it just my jPanel isn't updating. I got pretty far in the project, and i keep throwing random code segments in my method to make it so once i click something, the "left" jpanel either is replaced with the jbuttons, or the jbuttons are put into it, ive tried both. Heres the code segment ive been trying to get to work, which it does, just not as intended.
public void mousePressed(MouseEvent e) {
System.out.println("You clicked on " + ap.getTeams() [addPlayers.OPTIC].getTeamName());
for(int i = 0; i<4; i++){
//JPanel temp = ap.makePanel(ap.getTeams()[ap.OPTIC].getPlayers().get(i).getTwitterScreenName());
//temp.setBounds(0,(i*125), 450,125);
//left.add(temp);
JButton b = new JButton("Test");
b.setBounds(30,30,30,30);
left.add(b);
left.revalidate();
add(left);
add(b);
right.add(b);
left.setVisible(false);
left.setVisible(true);
System.out.println(i);
}
}
}
the commented out code is my main code, but i am just using jbuttons to test. As you can see, i just keep adding random bits, hoping something will work. Ive also tried invoke later, but sadly, that didn't work either.Not sure why the jpanels not updating, but any feedback would be great, thanks.
Avoid null layout and setBounds since this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
The best way to swap JPanels or any component is to use a CardLayout.
If still stuck, then post a minimal example program. It's the best way for us to actually see and experience your problem.
Don't add a component to more than one container like you're doing with your b JButton variable. You're in fact adding it to three containers.
You seem to be trying to add four JButtons to containers, giving them all the same bounds, one exactly on top of the other?? Sorry, but the more I look at your code, the more screwy it becomes. Again, stop this nonsense, learn about the layout managers at the Swing tutorials, and use them.
"and i keep throwing random code segments in my method to make it so ..." -- throwing things at the wall to see what sticks is not a good heuristic for creating a program. Start with the knowledge base -- here the Swing tutorials and Java API, then plan your program structure, and then create your code.
Edit
You ask:
..... All im asking is whats the problem with the whole thing not updateing?
If you don't use a CardLayout and change components manually, then you would need to call revalidate() and repaint() on the container after the change, and the container should update with its new components. If this does not fix the problem, then likely you have an issue in code not shown us, and you will again then want to create and post a minimal example program. Again it's the best way for us to actually see and experience your problem.
I am designing a Jframe using netbeans. I do have few questions.
Can we create a label for a field in a desired location(For eg.,we have a field named height, I need to display a label below it indicating height is in cm) conditionally?
Can we disable a field based on a condition?(by disable I mean it shouldn't be displayed in my frame)
Can someone suggest me whether we can achieve them through some examples.
Tried this, after some helpful suggestions
private void englishRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JLabel userlabel;
if (englishRadioButton.isSelected())
{
userlabel = new JLabel("Inches");
userlabel.setBounds(311, 59, 64, 36);
//userlabel.setLocation(307,55);
//userlabel.setSize(70,40);
userlabel.setForeground(Color.green);
userlabel.setText("Inches");
userlabel.setVisible(true);
System.out.println(englishRadioButton.getBounds());
inchesTextField.setVisible(true);
}
}
The textfield is visible only when I click the English radio button,at the same time I need to get a label but it's not displayed with the above code. Can I know where I am going wrong?
Please see the attached screenshots
When English button is clicked, I need a label beneath the second textfield as inches, I am disabling the text field when Metric is displayed. I am able to achieve the later one but not the former one
Thanks!!
Yes, relative placement of components is easily achieved with use of layout managers.
Yes, all components have a setEnabled(...) and a setVisible(...) method either of which can be called at any time during a program's run. The former helps you activate/inactivate components and the latter helps make them visible/invisible. If you want to swap complete "views", use a CardLayout.
Regarding:
Can someone suggest me whether we can achieve them through some examples.
Please, you first as I strongly believe that the onus of effort here should be yours, the questioner's, since you're the one asking the questions, and the one with the most to learn by coding as much as possible. Let's see your attempts and we can help you with them. Otherwise the best examples are to be found at the Swing Tutorials.
For links, please look here: Swing Tag Info.
Edit
You ask:
I tried the above posted code,conditionally disabling the text field works well but getting a label doesn't work. Can you please suggest on that?
I don't see you adding your JLabel to any component. If you are going to create a component on an event, you must add it to a component whose ancestor hierarchy eventually reaches a visible top-level component such as a JFrame. Then after adding a component to a container (say a JFrame), you must call revalidate() on the container to have its layout managers re-layout its components, and then repaint() to repaint any "dirty" pixels.
I again will re-iterate that you're far better off not using null layout and absolute positioning, but rather using layout managers and relative positioning. If you want a label with and without visible text, it's often best to add an empty JLabel to the GUI on GUI creation, and just set its text when needed, as long as the label is located somewhere that allows its text to shrink and expand.
Also, as to your current problem, you might wish to show a picture of what you're trying to achieve, and what you're getting. Or if you can't post a picture here yet, post a link to an image or images you've created, and then we'll post it for you.
This is the code I'm dealing with: http://pastie.org/1501054 When you run this, for some reason, the two panels overlap. Why is this so? Any way I can fix it?
The ActionListener that is provided as an argument is irrelevant to this part of the program.
Also, where can I find a good swing tutorial that uses Eclipse?
I am not seeing overlapping panes when I run your code. I wonder if you can post a screenshot of the effect that you see.
As for tutorials in Eclipse. I would suggest using Windows Builder pro in Eclipse for building Java GUI. This is an excellent product (free) and very good documentation on the site.
While I'm not sure what you want the final result to look like, here's a few of my suggestions.
In the snippet where you add the warning panel to the JFrame
warningPanel.add(warningLabel);
contentPane.add(warningPanel);
pack();
contentPane being the container returned by JFrame.getContentPane()
JFrames by default use the BorderLayout, and so,
contentPane.add(<someComponent>)
is identical to
contentPane.add(<someComponent>, BorderLayout.CENTER)
You also add the mainPanel the same way, and you can't have two components with the same constraints, so instead set the warningPanel to BorderLayout.NORTH
contentPane.add(warningPanel, BorderLayout.NORTH)
And also remove the call to pack() in that code snippet, since you call it later on in your code.
Hope this helps.
PS
As for GUI building in Eclipse, this previous question on Eclipse GUI Builder plugins maybe of use. I can't speak for tutorials on using Swing in Eclipse, but a quick google search digs up this tutorial using the Eclipse Visual Editor project
When you say you want a good swing tutorial for eclipse, do you want a GUI builder or a tutorial on Swing? Swing is all about layouts. Once you get that down, it's a piece of cake. Just start with flow layouts.
If you want a GUI builder, use Netbeans. It's incredible!
As for the overlap, it's not an overlap, but an overwrite. You can only have one panel in BorderLayout.CENTER, the default location.
If you want them side by side, do getContentPane().setLayout(new FlowLayout());
Or just add(panel, BorderLayout.SOUTH)
Yes I see the warning label is over lapping the first row
That is because You didnt specify the layout for your contentpane.
Heres how you fix it
contentPane.setLayout(new GridLayout(2,1));
add this line under
Container contentPane = getContentPane();
and I think you should change your warning panel to gridlayout 1 and 1 not 0 and 1 because you still have 1 row and 1 column
How to implement a panel that would support line wrapping and line breaking?
I would only add textual labels and line breaks to this panel.
The labels should flow from left to right, wrapping to the next "line" if
needed. The line breaks would cause a jump to the next line.
I would also like to make the panel vertically scrollable.
The solution should work in Java 5. SwingX can be used.
Clarification: The textual labels are actually JXHyperlinks (from SwingX),
i.e. the panel contains clickable labels. This is the reason I cannot just use
JTextArea.
UPDATE: I missed the request for hyperlink support. Don't know how to do that w/o using the EditorPane.
JTextArea does exactly what you've described.
JTextArea textArea = new JTextArea();
JScrollPanel sPane = new JScrollPane(textArea);
alt text http://img187.imageshack.us/img187/3238/wraprn0.png
This sample is not from a panel, that is a container, but from a JLabel, that is intended to show content.
You could use HTML in your content and use a <br> on each break. You should programmatically calculate the breaks according with your rules on component resize.
Here's the code:
import javax.swing.*;
import java.awt.*;
public class Wrap {
public static void main( String [] args ) {
JFrame frame = new JFrame("Wrap test");
String text = "<html>This<br>is<br>a<br>multiline<br>label</html>";
frame.add( new JLabel( text ) );
frame.pack();
frame.setVisible( true );
}
}
I found JTextPane, which I had overlooked before for some reason. This class does what I need.
Thanks for your help though. :)
Although it may not be a solution you're in search of, but from the requirements you have, it seems like a custom LayoutManager may be able to achieve what you are after. By designing and assigning a custom Layout Manager which allows line breaks to a Container (such as Panel), it should be possible to have a Panel which allows line breaks.
The Laying Out Components Within a Container article from The Java Tutorials will provide general information on how Layout Managers work in Java, and in particular, the Creating a Custom Layout Manager will provide information on how to make a custom Layout Manager to apply to an Container.
The behavior of the FlowLayout (the default Layout Manager for Panel) seems fairly close to the behavior you may be after. Adding functionality to line break seems like the missing piece.
Suggestion: Perhaps the custom Layout Manager can have the ability to add a line break by having a Component that represents a line break, which can be added to a Container by using the add() method.
For example, have a class constant Component in the custom Layout Manager, such as (a hypothetical) LineBreakLayout.LINE_BREAK, and adding that to the Container can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:
Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));
The above hypothetical LineBreakLayout will then render the first Label in one line and the second Label in the second line.