Issue with JScrollPane - java

I've written a code which displays search results which are images in JTable..
The main problem is that even after adding JScrollbar, the scorll bar does not appear even when its needed...
Here is a snapshot of my code..
this.queryName = qname;
JFrame frame = new JFrame(title);
frame.setLayout(new GridLayout(2, 1)); //.. UPPER FOR QUERY AND LOWER FOR RESULT
this.ijtable = new ImageJTable(result, Setting.getColumnCount());
ijtable.setVisible(true);
JScrollPane pane = new JScrollPane(ijtable);
JLabel query = new JLabel(new ImageIcon(ImageResize.resize(queryName, Setting.getQueryWidth(), Setting.getQueryWidth())));
query.setVisible(true);
frame.add(query);
frame.add(ijtable);
frame.add(pane);
frame.setSize(Setting.getFrameWidth(), Setting.getFrameHeight());
frame.setVisible(true);

It's probably because you are adding both ijtable and pane. Only add pane because that control has ijtable inside it.

frame.add(ijtable);
Try removing this line. By adding the table to the frame, you're removing it from the scroll pane.

Related

Vertical ScrollBar isn't appearing

I just began Swing. I'm trying to make a gallery app. I'm getting images that I manually imported and I display them. Depending on the amount of columns I put in parameters, the images' dimensions are calculated to be displayed properly. But, after a certain amount of rows, I want a scrollbar that could scroll and show the rest of images.
The images are displayed properly, like I wished but I tried to implement the scrollbar and it isn't appearing.
Could you tell me what's wrong in my code ?
GUI(String title, int width, int height, int columns) {
this.frame = new JFrame();
this.frameWidth = width;
this.columns = columns;
// set Frame's size
frame.setSize(width, height);
// set Frame's action on close button clicked
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set Frame's title
frame.setTitle(title);
frame.setResizable(false);
// set Frame's position at screen's center
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, (dim.height / 2 - frame.getSize().height / 2));
panel = new JPanel();
frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar, BorderLayout.EAST);
displayImages();
frame.setContentPane(panel);
}
displayImages() :
private void displayImages() {
for (File currentImage : getImagesList()) {
// 5 is flowlayout's default borders
// 2 because we got left and right borders
int totalBorders = (columns * 5) * 2;
int buttonWidth = (frameWidth - totalBorders) / columns;
ImageIcon image = new ImageIcon(new ImageIcon(currentImage.getAbsolutePath()).getImage().getScaledInstance(buttonWidth - 20, buttonWidth - 50, Image.SCALE_DEFAULT));
JButton button = new JButton(currentImage.getName());
button.setPreferredSize(new Dimension(buttonWidth, buttonWidth));
button.setIcon(image);
button.setHorizontalTextPosition(AbstractButton.CENTER);
button.setVerticalTextPosition(AbstractButton.BOTTOM);
button.addActionListener(e -> onImageClicked(currentImage.getName()));
panel.add(button);
}
}
Thanks, best regards
panel = new JPanel();
frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar, BorderLayout.EAST);
displayImages();
frame.setContentPane(panel);
The above code is mostly wrong. You need to:
set the layout of the panel and add the buttons to the panel
add the panel containing the buttons to the scroll pane
The basic code should be something like:
//panel = new JPanel( new GridLayout(0, 1) );
layout = new GridLayout(0, 1);
panel = new JPanel( layout );
displayImages();
//frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane(panel);
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollBar, BorderLayout.EAST);
//displayImages();
//frame.setContentPane(panel);
Also, there is no need to play with the sizes of each component on the panel. The GridLayout will make all the buttons the same size.
Edit:
When you want to increase the columns in the GridLayout you then just do:
layout.setColumns( layout.getColumns() + 1 );
panel.revalidate(); // this invokes the layout manager
//panel.repaint(); // sometimes needed to force repainting of panel.

Aligning JLabel to Left or Right inside BoxLayout with Y_AXIS Constraint of JPanel

I have a JPanel with Constraint's of Y_Axis so that whenever I add a new Component it will automatically be Added on a new Line.But the Problem is that the Label inside is not Aligned to Left or Right. It is displayed at some distance above the JTable. How can JLabel be displayed at desired Alginment.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
Then I added a JLabel inside panel.
JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
panel.add(labelSemester);
After label, I added a new JTable inside panel,
// Column Names for the Table
Object[] col_names = {"ID", "Name", "CH", "Marks", "Grade"};
// row data for the table
Object[][] table_rows = {{"CS123","Introduction to Computing",3,80,"A-"}};// One row only
JTable table = new JTable(table_rows, col_names);
panel.add(new JScrollPane(table));
Then I added a JFrame and added the Panel to show in the frame
JFrame frame = new JFrame();
// frame Title
frame.setTitle("DMC");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// adding panel inside frame
frame.add(panel);
// displaying frame
frame.show()
Note:
I have added code for auto Adjustment of column width of JTable.
Output can be seen from attached Image
All components added to the BoxLayout need the same alignmentX, otherwise you can get some weird layouts:
//JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
JLabel labelSemester = new JLabel("Semester 1: ");
label.semester.setAlignmentX(JLabel.LEFT_ALIGNMENT);
panel.add(labelSemester);
...
JTable table = new JTable(table_rows, col_names);
//panel.add(new JScrollPane(table));
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
panel.add( scrollPane );
Read the section from the Swing BoxLayout tutorial on Fixing Alignment Problems for more information. Keep a link to the tutorial handy for all Swing basics.

Inserting a JList into an existing JFrame

I have a JFrame that looks like:
The user enters some command into JTextArea2 and the result is shown into JTextArea1.
Now, for a specific command entered I want to create and "insert" the JList somewhere inside the JTextArea1.
It would look like:
Here is a little bit of code to show how I initially place them. How could I do that?
setLayout(new FlowLayout());
textArea_1 = new JTextArea(25,112);
textArea_1.setEditable(false);
scroll_1 = new JScrollPane(textArea_1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll);
textArea_2 = new JTextArea(1,111);
scroll_2 = new JScrollPane(textArea_2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll_2);

JScrollPane in Java

I wrote a little code to see how the scroll Pane functions but my code never worked.
here's the code,
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
scrollPane.add(textArea);
scrollPane.add(imad);
content.add(textArea);
content.add(imad);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
When I run it, I get a little window with the textArea and next to the text area a very little white square, which is the scrollpane i suppose because when I remove it from the code, this square disappears. When I write in the text area and exceed the window's dimension, I can't scroll vertically using the mouse wheel, and not horizontally at all. I saw many examples on internet and I can't understand why my code doesn't work??
Any help explaining how scrollpane works?
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
Only one component can be added to the viewport of the scroll pane, so the label replaces the text area.
content.add(textArea);
content.add(imad);
A component can only have a single parent. The above code removes the label from the scrollpane, so nothing is now in the scrollpane.
Try something like:
JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
For a better solution, start with the working example found in the Swing tutorial on How to Use Text Areas and then modify the code. This way you will start with a better structured program that follows Swing standards.

Using Boxlayout is causing my Panel to only be half the size of the parent panel

I am using a JPanel as a Main panel to display information to my users.
I have created 3 other Jpanels using methods. titlePanel, verbiagePanel, closeButtonPanel. Each of these methods are assigned as a component and added to the main panel. I am using BoxLayout in the main panel as well as the other panels.
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
this.setTitle("EDI - Help Page");
//this.setResizable( false );
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(700, 300));
centerPanel.add(titlePanel, BorderLayout.NORTH);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel, BorderLayout.CENTER);
centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
centerPanel.add(closeButtonPanel, BorderLayout.SOUTH);
getContentPane().add(centerPanel);
this.pack();
Above it the main panel Method. When I compile and run the dialog, everything looks right except the verbiagePanel. It is only half the size of the parent panel and the other 2 panels.
Here is the code for my verbiagePanel
private JPanel verbiagePanel() {
String titleText = "<html><font size=4><b><center>How To Use This Application</b></center></font></html>";
String text = "<html>" +
" <P align=left><font size=3>" +
" 1. Add a data to the list of Selected Datasets.<br/>" +
" (see below for detailed instructions on adding datasets)<br/>" +
" 2. Select the project to send data to.<br/>" +
" 3. Adjust the list of selected 3D or 2D translators if desired.<br/>" +
" 4. Use the Send button to continue the EDI transaction." +
" </font></P>" +
" </html>";
JLabel titleLabel = new JLabel(titleText, JLabel.CENTER);
titleLabel.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel bodyLabel = new JLabel(text, JLabel.LEFT);
bodyLabel.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(titleLabel);
p.add(bodyLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
The funny thing is, if I remove the BoxLayout from the Panel. The Panel will expand to match the other 2 panels. But the spacing of the labels are crazy. I will end up having at least 5 labels in the panel. Right now I am only showing 2 to make it more simple.
I ended up using GrigBagLayout model. It worked as needed.

Categories