Why JScrollPane does not react to mouse wheel events?

Walter beat me to analysing the issue 🙂 Adding a bit of detail: It’s correct that a JScrollPane supports mouseWheelHandling. According to the rules of mouseEvent dispatching, the top-most (in z-order) component gets the event, and that’s the scrollPane around the textArea. So if wheeling the textarea is not required, a simple solution might be … Read more

JTable Scrolling to a Specified Row Index

It’s very easy, JTable has scrollRectToVisible method too. If you want, you can try something like this to make scrollpane go to to the bottom if a new record is added : jTable1.getSelectionModel().setSelectionInterval(i, i); jTable1.scrollRectToVisible(new Rectangle(jTable1.getCellRect(i, 0, true))); Where i is last added record.

how to add jscrollpane to jframe?

JFrame uses a BorderLayout by default. It’s default position (if you don’t specify one) is CENTER. BorderLayout will only allow one component to occupy any of it’s 5 available positions. So when you do… jframe.add(scrollFrame); jframe.add(container); It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn’t actually … Read more

Make JScrollPane control multiple components

You should use JScrollPane#setRowHeaderView to set the component that will appear at the left hand side of the scroll pane. The benefit of this is the row header won’t scroll to the left as the view scrolls to the right… The example deliberately uses line wrapping… import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FontMetrics; import … Read more

Java Swing : why must resize frame, so that can show components have added

Do not add components to JFrame after the JFrame is visible (setVisible(true)) Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size. Use EDT (Event-Dispatch-Thread) call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by @Gilbert Le … Read more

JTable with horizontal scrollbar

First, add your JTable inside a JScrollPane and set the policy for the existence of scrollbars: new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); Then, indicate that your JTable must not auto-resize the columns by setting the AUTO_RESIZE_OFF mode: myJTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

How to make JScrollPane (In BorderLayout, containing JPanel) smoothly autoscroll

You can set the value of the horizontal scrollbar to control what is currently visible: JScrollBar horizontal = scroll.getHorizontalScrollBar(); horizontal.setValue( horizontal.getValue() + ??? ); You would need to use a Swing Timer to schedule the scrolling at an appropriate interval. Simple example of using a Timer to scroll text: import java.awt.*; import java.awt.event.*; import java.util.*; … Read more

How to set AUTO-SCROLLING of JTextArea in Java GUI?

When using JDK1.4.2 (or earlier) the most common suggestion you will find in the forums is to use code like the following: textArea.append(…); textArea.setCaretPosition(textArea.getDocument().getLength()); However, I have just noticed that in JDK5 this issue has actually been resolved by an API change. You can now control this behaviour by setting a property on the DefaultCaret … Read more

Why JScrollPane in JOptionPane not showing all its content?

As shown in this related example, you can override the viewport’s preferred size. import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.GroupLayout; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.SwingConstants; /** * @see https://stackoverflow.com/a/14858272/230513 * @see https://stackoverflow.com/a/8504753/230513 * @see https://stackoverflow.com/a/14011536/230513 */ public class DynamicGroupLayout { private static final int NUM = 30; private JTextField[] … Read more