appear and disappear for a scroll depending on a conditional - java

everyone, excuse my language I speak Spanish and I use google translator
I have a question I can appear and disappear as one scroll lacelda depending on size or table for example when the cell arrives at a height of "300" scrolling appears visible, if not reach 300 not appear
How I can do that?
regards

There is a Swing class called JScrollPane. There are two functions called:
scrollBar.getHorizontalScrollBar().getValue();
scrollBar.getVerticalScrollBar().getValue();
You'll either need an event listener or a loop in a separate thread to check if the scroll bar is past a certain point.
The code to make it disappear is just:
scrollBar.setVisible(false)
The code to make it reappear:
scrollBar.setVisible(true);
scrollBar.validate();
scrollBar.repaint();

Related

How to add a "Add a text" in a JFrame

I'm having difficulty in getting resources that will help me with an issue. I want to add a button that will let me insert text anywhere in my JFrame so I can click and add a text
Before that, I have been using JTextField and JTextArea, but these are useful in a predefined space (I believe).
What I want is like the classic Add Text button in Paint ("A" button) in which when you click on it, you will have the text cursor (like an "I") and then click somewhere in the frame so I can actually add text in that area. Which I call "adding text dynamically". Unfortunately I don't have any code to show because I'm not sure how to build the code from scratch or maybe use a method in Swing, but I'm more than keen to explain better if this is too generic for you guys.
You're going to need to be familiar with Java graphics and associated methods, particularly drawString(), so start by reading the official Graphics Tutorial.
Then learn about MouseListeners, which will help you recognize when and where the user clicks in your JFrame.
Then learn about KeyListeners, which will allow you to detect when your user is typing. You may wish to use a key bind instead, but as described, your UI is relatively simple with only one focusable component, so keylisteners are probably OK.
This general strategy should help you take a flying leap at the problem:
detect mouseclick with mouselistener
draw an insertion bar
detect keypress with keylistener
use graphics.drawstring to draw the character specified by the keypress
redraw the insertion bar
Make sure your keylistener handles the enter key, so that you know when to erase the insertion bar. You're going to paint your JFrame in between each of those steps.
The result will be crude, to say the least, and you'll notice that you have to handle a lot of issues that crop up along the way before things start to look professional. For example, you'll have to deal with font metrics. Not every character is going to be the same width, so you have to calculate the X argument to your drawString() based on the charWidth of whatever character the user types. You might do well to start with a monospaced font for that reason.

How to change one half of a split pane while keeping the other the same when an action occurs in javafx

I am trying to create an application in javaFX that has two main halves to it. One side where you have a set of buttons or controls, and the other where it displays a different set of data for each button pressed, almost as if one side changes scenes, but the point is, layout panes count as scenes and I am still trying to figure out how to dynamically change one side while the other remains static. If you know how to accomplish this(Ideally with a split pane), then that would be a great help, as I am yet to find this question answered.
Thanks
SplitPane has a .getItems() method.
Use that and .set() the content you want at the correct index.

Adding tooltip text to JLabel prevents containing JPanel from throwing MouseEvents

I have a JPanel (pNums) which contains another JPanel (pGrid). pGrid itself contains a grid (JLabel[][] in a GridLayout) of labels. There is a mouse listener which catches events from pGrid and does fairly important stuff with them (as in, the entire functionality of the program relies on the mouseClicked() event). This works perfectly, exactly the way I wanted it to... until I add tooltips to the labels.
As soon as I call JLabel.setToolTipText("SomeString") the listener stops reacting to events (I have tried most, if not all of the mouse events, none of them seem to be called).
I am sure that it is the tooltips by the way, commenting out the setToolTipText() completely fixes the problem. Of course, since I needed the tooltips, it also causes a whole host of other problems.
I've looked around and while I haven't found anything quite right, I get the impression that I just chose a really bad way to do what I wanted. But I also want to know for sure.
Can I get both the event and the tooltip or should I go back to the drawing board.
I think you might be able to "fix" this issue, with setting delay on tooltip appearance. But once it will appear user will have to click to hide it anyway.
http://docs.oracle.com/javase/7/docs/api/javax/swing/ToolTipManager.html
The reson for this might be, that tooltip itself needs mouse click, to hide.

Render Newly Visible Portion Upon Scrolling on JScrollPane

I have a large JPanel embedded in a JScrollPane. When I move the scrollbar, I notice that the visible portion does not render itself and I get glitches. Whereas when I resize the frame, I can see the new visible portions rendered. So I need to know which methods are fired upon frame resize to repaint the view. What listeners/methods should I use?
So I need to know which methods are fired upon frame resize
You don't need to know that. All you need to do is change the value of the scrollbar or the position of viewport and the component should repaint itself properly. If it is not painting properly, then you have a problem with something else. Maybe
incorrect custom painting code
the code is not invoked on the EDT
If those suggestsion doen't help then you need to post a proper SSCCE that demonstrates the problem because we can't keep guessing what your code is doing.
Did you revalidate the panel?
It might be that something is not right in the code of yours.
I have been using lots of scrolls and never had an issue as you describe.
Maybe a code sample showing the problem would be nice.
Good luck, Boro

Java JPanel redraw issues

I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().

Categories