JTable with horizontal scrollbar - java

Is there any way to enable horizontal scroll-bar whenever necessary?
The situation was as such: I've a JTable, one of the cells, stored a long length of data. Hence, I need to have horizontal scroll-bar.
Anyone has idea on this?

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);

Set the AutoResizeMode to OFF in the properties of the jTable

For reference, here's a minimal example of the accepted approach. Moreover,
You can adjust the size of individual columns as shown in Setting and Changing Column Widths, as well as here and here.
You can adjust the overall size of the enclosing scroll pane as shown in Implementing a Scrolling-Savvy Client, as well as here and here.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
/**
* #see https://stackoverflow.com/a/37318673/230513
*/
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new AbstractTableModel() {
private static final int N = 32;
#Override
public int getRowCount() {
return N;
}
#Override
public int getColumnCount() {
return N;
}
#Override
public Object getValueAt(int rowIndex, int colIndex) {
return "R" + rowIndex + ":C" + colIndex;
}
};
JTable table = new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.add(new JScrollPane(table));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}

For me it works:
table.setAutoscrolls(true);

I had to do several things to get mine working
Set autoResize to AUTO_RESIZE_OFF.
Set preferredSize on JTable to null.
Set horizontalScrollBarPolicy on JScrollPane to AS_NEEDED.

Related

how i resize when mouse over using swing?

I'm trying to resize a label on MouseEnter, but on MouseExit, I want it back to the previous state. How would I do this?
I want the label to be bigger when it is moused over, but when the mouse exits, the label will back to normal size.
Can anybody explain to me how to do that?
If it's possible, I want to see the resize slowly.
This is the code:
package kk
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ScrollGroup extends JPanel {
private static final int N = 8;
private static final int NN = N * N;
private static final int GAP = 5;
private static final int SIZE = 100;
public ScrollGroup() {
this.setLayout(new GridLayout(N, N, GAP, GAP));
for (int i = 0; i < NN; i++) {
final JLabel label = new JLabel();
label.setOpaque(true);
label.setBackground(Color.getHSBColor((float) i / NN, 1, 1));
label.setPreferredSize(new Dimension(SIZE, SIZE));
this.add(label);
label.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e) {
}
});
}
}
private void display() {
JFrame f = new JFrame("ScrollGroup");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane(this);
GroupLayout layout = new GroupLayout(f.getContentPane());
f.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup().addComponent(sp)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup().addComponent(sp)));
f.pack();
f.setSize(N * SIZE, N * SIZE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ScrollGroup().display();
}
});
}
}
I'm trying to resize a label in MouseEntred,
Define "resize".
You are adding your JLabels to a panel using a GridLayout. All the labels are already set to the maximum size permitted by the space available to the panel, so what do you expect the resize to do?
If you want it to appear that the label is getting bigger, then maybe you can assign a MatteBorder to each label. You can make the MatteBorder whatever size you want and then set the color equal to the background color of the panel.
If you want to animate then then you can use a Swing Timer. In the mouse#ntered you start the Timer. Every time the Timer fires you change the MatteBorder to be one less pixedl until the size is zero and you stop the Timer. On mouseExited, you just restore the default Border.
See the sections from the Swing tutorial on How to Use Timers and How to Use Borders for more information.

JTable in JScrollPane, how to set background?

I am using a JScrollPane to wrap a JTable. Depending on the configuration, there is some space that is not occupied by the table. It is drawn gray (it looks like it is transparent and you can just see the component in the back). How can I set this area to be a certain color?
Here is a SSCCE to illustrate.
import java.awt.Color;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class DialogDemo extends JDialog {
public static void main(final String[] args) {
final DialogDemo diag = new DialogDemo();
diag.setVisible(true);
}
public DialogDemo() {
super();
setTitle("SSCCE");
final Vector<Vector<String>> rowData = new Vector<Vector<String>>();
final Vector<String> columnNames = new VectorBuilder<String>().addCont("Property").addCont("Value");
rowData.addElement(new VectorBuilder<String>().addCont("lorem").addCont("ipsum"));
rowData.addElement(new VectorBuilder<String>().addCont("dolor").addCont("sit amet"));
rowData.addElement(new VectorBuilder<String>().addCont("consectetur").addCont("adipiscing elit."));
rowData.addElement(new VectorBuilder<String>().addCont("Praesent").addCont("posuere..."));
final JTable table = new JTable(rowData, columnNames);
JScrollPane pane = new JScrollPane(table);
// ************* make that stuff white! *******************
table.setBackground(Color.white);
table.setOpaque(true);
pane.setBackground(Color.white);
pane.setOpaque(true);
// ************* make that stuff white! *******************
add(pane);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
class VectorBuilder<T> extends Vector<T> {
public VectorBuilder<T> addCont(final T elem) {
addElement(elem);
return this;
}
}
}
And here you can see the area, which I want to "colorize". In the SSCCE, I try to do that by using setOpaque(boolean) and setBackgroundColor(Color) of the table and scroll pane, with no success.
Can you tell me, what I am doing wrong?
Instead of this:
table.setBackground(Color.white);
table.setOpaque(true);
pane.setBackground(Color.white);
pane.setOpaque(true);
call:
pane.getViewport().setBackground(Color.WHITE);

JComboBox fails to expand in JTable TableHeader

I have read through the majority of the JTable/JComboBox responses to other questions of this ilk, but haven't yet found a solution to my problem.
I have created a table that has JComboBox TableHeader elements. None of the JComboBox elements will open to display a list of items. How do I get the item lists for the individual JComboBox elements to display?
Please note that a distinguishing element of this question is that the JComboBox is in the TableHeader, not embedded within a JTable cell.
Any help is appreciated.
SSCE
import java.awt.Component;
import java.awt.Dimension;
import java.util.Enumeration;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class ComboHeaderTest extends JScrollPane {
private static final Dimension DEFAULT_SIZE = new Dimension(200, 200);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ComboHeaderTest().initComponents();
}
});
}
private ComboHeaderTest() {
final String[][] data = { {"Header 1", "Header 2", "Header 3"},
{"A", "B", "C"},
{"D", "E", "F"},
{"G", "H", "I"}};
setViewportView(getTable(data));
setPreferredSize(DEFAULT_SIZE);
}
private void initComponents() {
JFrame frame = new JFrame("ComboHeaderTest");
frame.add(this);
frame.pack();
frame.setVisible(true);
}
private JTable getTable(final String[][] data) {
final String[] items = data[0];
final ComboHeaderRenderer[] columnHeaders = new ComboHeaderRenderer[items.length];
for(int i = 0; items.length > i; ++i) {
columnHeaders[i] = new ComboHeaderRenderer(items);
}
final JTable table = new JTable(data, columnHeaders);
final Enumeration<TableColumn> tableEnum = table.getColumnModel().getColumns();
for (int columnIndex = 0; tableEnum.hasMoreElements(); ++columnIndex) {
final TableColumn column = tableEnum.nextElement();
final ComboHeaderRenderer combo = columnHeaders[columnIndex];
column.setHeaderValue(combo.getItemAt(columnIndex));
column.setHeaderRenderer(combo);
}
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setFillsViewportHeight(true);
table.setSize(DEFAULT_SIZE);
table.validate();
return table;
}
private static class ComboHeaderRenderer extends JComboBox implements TableCellRenderer{
public ComboHeaderRenderer(final String[] entries) {
for (int i = 0; entries.length > i; ++i) {
addItem(entries[i]);
}
}
#Override
public Component getTableCellRendererComponent(final JTable table, final Object value,
final boolean isSelected, final boolean hasFocus, final int row, final int column) {
setSelectedItem(value);
return this;
}
}
}
This actually works exactly as expected. I think the clue is TableCellRenderer.
Renderer's are typically non-interactive components. They are usually just a "snap-shot" of the component painted on to a surface and there is typically no way for a user to interact with them.
This is the expected behavior.
In order to supply editable functionality to the table header, you're going to need to supply your implementation of a JTableHeader
Have a look at this example for some ideas
Here is an example that uses a JComboBox in a JTable TableHeader.
For other types of components is easier, have a look here.

JSpinner: Increase length of editor box

I have a JSpinner that displays decimal values from 0.0 to 999.0. It seems to work fine, except for when it displays a number in the editor box that is four-digits long, such as 123.4; it then cuts off part of the final digit because it is not long enough.
So my question is: Does anyone know how to increase the length of the editor window of a JSpinner?
Thanks!
You can get to the text field which in fact is a JFormattedTextField by
First calling getEditor() on your JSpinner to get the spinner's editor
cast the returned object to JSpinner.DefaultEditor
Then call getTextField() on this. Then you can set it's preferredSize if desired.
Edit: as noted by trashgod though, using a proper layout is paramount and being sure that the layouts you use are the best is probably the best way to solve this issue.
Edit 2: The above is wrong as setting the textfield's preferred size does nothing. You can however set the preferred size of the editor itself, and that works. e.g .,
import java.awt.Dimension;
import javax.swing.*;
public class SpinnerBigTextField {
private static void createAndShowGui() {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, 999.0,
0.5));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 100));
panel.add(spinner);
JComponent field = ((JSpinner.DefaultEditor) spinner.getEditor());
Dimension prefSize = field.getPreferredSize();
prefSize = new Dimension(200, prefSize.height);
field.setPreferredSize(prefSize);
JFrame frame = new JFrame("SpinnerBigTextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
The first Hovercraft answers is not bad at all. You can not change the size directly, but you can do something like this:
JComponent editor = mySpinner.getEditor();
JFormattedTextField tf = ((JSpinner.DefaultEditor) editor).getTextField();
tf.setColumns(4);
Where you can define the columns numbers showed by the editor. It will change the size of the spinner.
As FontMetrics vary from one platform to the next, it's better to rely on the component's own calculation of preferred size. This example shows a spectrum of JSpinner sizes for various min and max values. Note in particular that FlowLayout "lets each component assume its natural (preferred) size."
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
/** #see http://stackoverflow.com/questions/7374659 */
public class SpinnerTest extends Box {
private static final double STEP = 0.1d;
private static final String FORMAT = "0.0000000000";
public SpinnerTest(int axis) {
super(axis);
for (int i = 0; i < 8; i++) {
int v = (int) Math.pow(10, i);
this.add(genParamPanel((i + 1) + ":", -v, v));
}
}
private JPanel genParamPanel(String name, double min, double max) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JLabel label = new JLabel(name, JLabel.TRAILING);
JSpinner js = new JSpinner(new SpinnerNumberModel(min, min, max, STEP));
js.setEditor(new JSpinner.NumberEditor(js, FORMAT));
panel.add(label);
panel.add(js);
return panel;
}
private void display() {
JFrame f = new JFrame("SpinnerTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new SpinnerTest(BoxLayout.Y_AXIS).display();
}
});
}
}
JSpinner spn=new JSpinner();
spn.setPreferredSize(new Dimension(100,25));
Here,look This is the easy Answer.
Changing the max value of a spinner will increase the size of the text box to accommodate the large number. If you do not wish to make the max value larger, i would recommend what #JorgeHortelano suggested...
JComponent editor = mySpinner.getEditor();
JFormattedTextField tf = ((JSpinner.DefaultEditor) editor).getTextField();
tf.setColumns(4);

How to set the orientation of JTextArea from right to left (inside JOptionPane)

I have JScrollPane with JTextArea inside it and I am trying to set the JTextArea's orientation from right to left so the text inside it will start from the right and the scrollbar will be on the left
I've tried the following but they didn't affect the direction of the orientation:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
EDIT:
the two answers camickr & trashgod provided work fine but not in my program where I use my JTextArea as an object Message and pass it to OptionPane.
EDIT2:
I figured out that setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); doesn't work if I apply it on the JOptionPane contents .. is there an alternative solution to this issue?
Similar to my code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
private JTextArea txt = new JTextArea();
public TextArea()
{
setLayout(new GridLayout());
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(txt);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setPreferredSize(new Dimension(200,200));
this.add(scroll);
}
private void display()
{
Object[] options = {this};
JOptionPane pane = new JOptionPane();
int option = pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
}
public static void main(String[] args)
{
new TextArea().display();
}
}
and the scrollbar will be on the left
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
so the text inside it will start from the right
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
The text starts on the right side, but still gets append to the end as you type instead of being inserted at the beginning of the line.
Update:
I don't know why it doesn't work in an option pane. Here is a simple solution:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test
{
public static void main(String args[]) throws Exception
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JTextArea textArea = new JTextArea(4, 20);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel panel = new JPanel();
panel.add( scrollPane );
scrollPane.addAncestorListener( new AncestorListener()
{
public void ancestorAdded(AncestorEvent e)
{
JScrollPane scrollPane = (JScrollPane)e.getComponent();
scrollPane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
public void ancestorMoved(AncestorEvent e) {}
public void ancestorRemoved(AncestorEvent e) {}
});
JOptionPane.showMessageDialog(null, panel);
}
});
}
}
This seems to work.
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/** #see http://stackoverflow.com/questions/6475320 */
public class RTLTextArea extends JPanel {
private static final String s = "مرحبا العالم";
private JTextArea jta = new JTextArea(7, 5);
private Locale arabic = new Locale("ar", "KW");
private ComponentOrientation arabicOrientation =
ComponentOrientation.getOrientation(arabic);
public RTLTextArea() {
this.setLayout(new GridLayout());
this.add(new JScrollPane(jta));
this.applyComponentOrientation(arabicOrientation);
for (int i = 0; i < 8; i++) {
jta.append(s + "\n");
}
}
private void display() {
JFrame f = new JFrame("RTLTextAre");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new RTLTextArea().display();
}
});
}
}
this line
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)
change the correct order of the words.
i have this result
KBytes 80.78
The following lines solved my problem:
jTextArea1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jTextArea1.setText(<text>);
They serve to:
setComponentOrientation() changes the orientation of the TextArea; and,
setText() refreshes TextArea immediately so it displays properly
Simply setting ComponentOrientation to RIGHT_TO_LEFT is not sufficient by itself. repaint() doesn't force the text to realign itself. A quick solution for me was to update the contents of the TextArea. That forced the text to realign itself.

Categories