I have a jlist that contains items written in arabic, and unfortunately if any item is long, all items disappear after loading, because the scroller doesn't autmaticly scroll to right, and I should manualy scroll the list horizontaly to right.
I have set the JList's componentOrientation to right.
Any idea?
Assuming the JList is in a JScrollPane, you can set the value of the horizontal scroll bar:
final int maximum = scrollPane.getHorizontalScrollBar().getMaximum();
scrollPane.getHorizontalScrollBar().setValue(maximum);
Like in the example here:
final JFrame frame = new JFrame("JList horizontal auto-scroll to right");
frame.setBounds(100, 100, 80, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final String[] model = {"aaa", "bbbb", "ccccccccccccccccccccccccc"};
final JList<String> jList = new JList<String>(model);
jList.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
final JScrollPane scrollPane = new JScrollPane(jList);
frame.getContentPane().add(scrollPane);
final int maximum = scrollPane.getHorizontalScrollBar().getMaximum();
scrollPane.getHorizontalScrollBar().setValue(maximum);
frame.setVisible(true);
For me, the window looks like this:
screenshot frame
Related
I have the following code taken from GeeksforGeeks that displays the contents of a 2-d array in JTable using JScrollPane:
public class JTableExamples {
// frame
JFrame f;
// Table
JTable j;
// Constructor
JTableExamples()
{
// Frame initiallization
f = new JFrame();
// Frame Title
f.setTitle("JTable Example");
// Data to be displayed in the JTable
String[][] data = {
{ "Kundan Kumar Jha", "4031", "CSE" },
{ "Anand Jha", "6014", "IT" }
};
// Column Names
String[] columnNames = { "Name", "Roll Number", "Department" };
// Initializing the JTable
j = new JTable(data, columnNames);
j.setBounds(30, 40, 200, 300);
// adding it to JScrollPane
JScrollPane sp = new JScrollPane(j);
f.add(sp);
// Frame Size
f.setSize(500, 200);
// Frame Visible = true
f.setVisible(true);
}
What I am trying to do is add a simple Component (like JButton) underneath the table but it does not seem to work. I tried modifying the code by adding the JButton to JPanel and adding JPanel to the frame:
JButton button = new JButton("Back");
JPanel panel = new JPanel();
panel.add(button);
f.add(sp);
f.add(panel);
But this simply deletes the entire table and replaces it with a single button. I also tried adding the button to JPanel and adding that JPanel to JScrollPane:
JButton button = new JButton("Back");
JPanel panel = new JPanel();
panel.add(button);
sp.add(panel);
f.add(sp);
But this did not seem to change anything. I also tried to tinker with preferred and maximum size of JScrollPanel to no avail - it always occupies the entire screen and prevents JButton from appearing on the screen.
Not shooting for design here, just functionality: have a JButton appear underneath my JTable. Any suggestions will be greatly appreciated. Thank you in advance!
The default layout manager of a JFrame is the BorderLayout.
f.add(sp);
f.add(panel);
When you don't specify a constraint for the BorderLayout the CENTER is assumed. You can only have a single component added to the CENTER.
Instead your code should be:
f.add(sp, BorderLayout.CENTER);
f.add(panel, BorderLayout.PAGE_END);
Note the default layout manager for a JPanel is the FlowLayout. So the button will be horizontally centered in the panel.
Also, instead of using a JPanel, try adding the button directly to the PAGE_END of the frame to see the difference.
Read the section from the Swing tutorial on Using Layout Manager for more information and examples for using each of the different layout managers to understand the differences of the above suggestions.
Edit:
Is there a way to decrease the height of the table
If you know you have a small table then you can use:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
This will make the scroll pane the size of the table.
Then you use:
//f.setSize(500, 200);
f.pack();
Now all components will be displayed at their preferred size.
I want to increase the width that the table is covering On Jpanel.
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setTitle("Person Table");
jf.setSize(1100, 700);
JPanel jp1 = new JPanel();jp1.setBackground(Color.green);
JPanel jp2 = new JPanel(); jp2.setBackground(Color.red);
jp1.setSize(1100, 400);
jp2.setSize(980, 200);
JTable jt = new JTable(data,columnNames); jt.setSize(900, 350);
jt.setBackground(Color.ORANGE);
// Add table to JScrollpane
JScrollPane sp = new JScrollPane(jt); sp.setSize(1000, 380);
sp.setBackground(Color.CYAN);
jp1.add(sp);
jf.add(jp1);
jf.add(jp2);
jf.setVisible(true);
This is the output
I noticed that IF I don't use Scroll pan the Column name disappears and size increases..
But I also want the column name to appear..
Keep the scroll pane, and replace jp1 with it,
AKA Change:
jf.add(jp1);
to
jf.add(jsp);
This will make the table take up the whole green area*. If this isn't what you want, use nested layouts.
*Depending on the LayoutManager. In this case, FlowLayout is used which does not resize the component. Were you using a GridLayout or BorderLayout, the entire green area would be filled.
I want to layout a JList on one line horizontally, and have it scroll only horizontally. I've found list.setLayoutOrientation(JList.VERTICAL_WRAP); which works well if there are few enough items. However, when the list needs to scroll, the scrollbar covers the last (and only) row of the list, so you can't see it at all. How can I prevent this?
My test code:
JList<String> list = new JList<>("TIVFBJPAVUOHCVINPNYLMSMNNDUSHVSWUYUSNZXTYTXJMJPTISAVVYHOPBFIAXSUUQYYPVGAKEEWOTRCBWQWRXQTYJLCTTHTXPMZWDLQRRUZJSVWDMLYNRUDZXRTEJWAZUOBQCWNCYEPVCPXVWOGVZPOEKPWZZFDGZZGXPBFZQQVKFIXCYFTHRPJJMOYISEUCUTJGZQI".split("[A-D]"));
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(1);
frame.add(new JScrollPane(list), BorderLayout.NORTH);
The result:
Force the scroll pane to include the horizontal scrollbar in its size calculation:
JList<String> list = new JList<String>( ... );
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(1);
JScrollPane scrollPane = new JScrollPane( list );
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add( scrollPane );
EDIT: Sorry for many edits. I forgot what I wrote by myself.
I use JPanel that has BoxLayout as root Panel for JFrame. I'm adding to this root Panel two other Panels: buttonPanel with FlowLayou and tabbedPane. Each tabbed Pane is created dynamically by pressing second button at the top. In tabbedPane there is a templatePanel with BoxLayout that contains three other general JPanels: Checkboxes Panel with FlowLayout, tablePanel with BorderLayout and another one with BoxLayout.
I'm adding a JTable to tablePanel with BoderLayout.CENTER and after running program JTable is way too big vertically and I need to resize frame. I need to add rows dynamically so I create an empty JTable with my custom DefaultTableModel (I overloaded isCellEditable method, nothing more) and then by checking checkboxes I fill it with data.
JTable is also way too big than maximum rows number it is designed to hold.
What I mean:
How can I shrink it?
I create templatePanel with class's constructor (extends JPanel) by just add.(templatePanel)
code:
public TemplatePanel()
{
model = new DefaultTableModel(new Object[][] {}, new String[]
{"<html>...</html>", "<html>...</html>",
"...", "...", "<html>...</html>",
"<html>...</html>", "...", "...",
"<html>...</html>"})
{
#Override
public boolean isCellEditable(int row, int column)
{
return column == 1 || column == 3;
}
};
templatePanel = new JPanel();
tablePanel = new JPanel();
templatePanel.setLayout(new BoxLayout(templatePanel, BoxLayout.PAGE_AXIS));
tablePanel.setLayout(new BorderLayout());
checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new FlowLayout());
1 = new JCheckBox("...");
2 = new JCheckBox("...");
3 = new JCheckBox("...");
4 = new JCheckBox("...");
5 = new JCheckBox("...");
6 = new JCheckBox("...");
checkBoxPanel.add(1);
checkBoxPanel.add(2);
checkBoxPanel.add(3);
checkBoxPanel.add(4);
checkBoxPanel.add(5);
checkBoxPanel.add(6);
1.addItemListener(this);
2.addItemListener(this);
3.addItemListener(this);
4.addItemListener(this);
5.addItemListener(this);
6.addItemListener(this);
table = new JTable(model);
scrollPane = new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setTableHeader(new JTableHeader(table.getColumnModel())
{
#Override
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
d.height = 50;
return d;
}
});
TableColumn firstColumn = table.getColumnModel().getColumn(0);
TableColumn secondColumn = table.getColumnModel().getColumn(1);
TableColumn thirdColumn = table.getColumnModel().getColumn(2);
TableColumn ninthColumn = table.getColumnModel().getColumn(8);
firstColumn.setPreferredWidth(170);
secondColumn.setPreferredWidth(50);
thirdColumn.setPreferredWidth(30);
ninthColumn.setPreferredWidth(100);
table.setRowHeight(30);
tablePanel.add(scrollPane, BorderLayout.NORTH);
templatePanel.add(checkBoxPanel);
templatePanel.add(tablePanel);
add(templatePanel);
}
The basic logic should be:
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
This will get rid of the extra vertical space.
If I understand correctly, you want the frame of the table to be smaller? Is that correct? If you're using BorderLayout, center will cover the entire frame unless you add something in the cardinal directions. Create a box and add it SOUTH to create a cushion between the bottom and the table.
Again, If I'm not understanding this properly I apologize.
Edit:
Have you tried using setPreferredSize(new Dimension(800, 500)) on the scrollpane?
I'm trying to create a scroll bar for my text area. However, the scroll bar isn't appearing. Can anyone give me any tips. This is the from the method which creates the panel where the scroll bar will be.
displayCD = new JPanel();
displayCD.setSize(new Dimension(500, 500));
jta = new JTextArea();
jta.setMaximumSize(new Dimension(500, 500));
scrollPane = new JScrollPane();
scrollPane.getViewport().add(jta);
displayCD.add(scrollPane);
see this example. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS property.
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] argv) throws Exception {
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
}
Reference: http://www.java2s.com/Code/JavaAPI/javax.swing/JScrollPaneVERTICALSCROLLBARALWAYS.htm
You need to provide the textArea to the constructor of the scrollpane. Please check this link, for more information: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html.
JScrollPane scrollPane = new JScrollPane(textArea);
Check out How to Use Scroll Panes. Here follows their example:
//In a container that uses a BorderLayout:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
setPreferredSize(new Dimension(450, 110));
...
add(scrollPane, BorderLayout.CENTER);
Try to pass the JTextArea in the JScrollPane's constructor, and, most of all, try to give it meaningful size hints (rows and columns).
Applying this to your specific case:
jta = new JTextArea(5, 30);
scrollPane = new JScrollPane(jta);
displayCD = new JPanel();
displayCD.add(scrollPane);
The scroll bar should appear, if your text contents exceed the default dimensions. To always show the bars, try the setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy methods, or other JScrollPane constructors.
Regarding the size of the text area:
Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.