I want to add table2 into the scrollpanel (called feedback) which already has table1 in there. But only one table shows up. If I use feedback.add(table2), only the 1st table shows (I guess the 2nd table is behind the first one, but I don't know how to make the second one below the first one). if I use feedback.getViewport().add(table2, null), only the 2nd table shows. Do I need to use some layout manager here? i tried to search online about scrollpanel layout but didn't get any solutions. Could anyone tell me what's the problem or give me some related example links? Thanks a lot. The relative code are:
content = getContentPane();
content.setLayout(new FlowLayout());
scrollPane = new JScrollPane(tree);
feedback = new JScrollPane(table1);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollPane, feedback);
content.add(splitPane);
.
.
.
.
feedback.add(table2);
//i add this, but still doesn't work
content.add(table2);
Only a single component can be added to the "viewport" of a JScrollPane. This is done by using:
JScrollPane scrollPane = new JScrollPane( table );
or
scrollPane.setViewportView( table );
If you want multiple component to appear in a scrollPane then add the component to a panel first and add the panel to the viewport.
Read the JScrollPane API for more information and follow the link to the Swing tutorial as well for examples.
Related
I have a Java connection with MySQL database. I inserted 30 variable to a MySQL table. I would like to create a graphical interface with Jframe where I can select the variables from the table using a checkbox.
I plan to do something similar as Java Swing Group of checkbox multiple selection handler
But I do not know how I can use it with NetBeans? I tried to add a newJFrame where I add a jScrollPane and I wanted to add multiple jCheckBoxes but I can add only one to jScrollPane.
Could someone let me know how I should add a group of checkboxes to jScrollPane?
You have to add a JPanel to your JScrollPane, then add checkboxes to that JPanel.
JPanel panel = new JPanel(layout);
panel.add(checkbox1);
panel.add(checkbox2);
JScrollPane scrollPane = new JScrollPane(panel);
I have the following JTable in my program which is placed inside a JScrollPane. I made it using the GUI builder provided by Netbeans.
The problem is some of the columns in the table is not visible and there is no horizontal scroll bar. I even set the horizontalScrollBarPolicy to ALWAYS and it didnt help. So how do I make the table scrollable?
As #Andrew Thompson mentiond you should provide some code that addresses the problem. Without seeing your code it's hard to find out what's the problem. But a common workaround about that is as the following:
If you have correctly added your JTable to the ViewPort of a JScrollPane like this:
JTable jtable = new JTable();
//...
JScrollPane sc = new JScrollPane(jtable);
//sc.setViewportView(jtable); <- This way is correct too
//
getContentPane().add(sc);
Then the most probable problem is about the AutoResizeMode of your JTable. Try this:
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Hope this would help you.
So, I'm wondering how I would go about colouring certain words in my scrollpane. After doing some research I have found almost nothing helpful. What is there to help with colouring certain words?
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
txtInfo = new JTextArea("Bot In Progress...\n");
txtInfo.setColumns(35);
txtInfo.setEnabled(false);
txtInfo.setDisabledTextColor(Color.black);
scrollPane.setViewportView(txtInfo);
So, here is my scrollpane. What I have it doing now is accepting outputs from part of my program and displaying them. Certain words that I want coloured are Win!(Green) and Loose(Red), and maybe some more later.
The reason why a scrollpane works the best for me is because I need the log to update constantly and the user is supposed to be able to look back up at the log at any time. Is there a better way of storing that information or what?
I don't think you can do it with JTextArea instead you can do it using JTextPane. You can then add the JTextPane to your JScrollPane. Have a look at this site http://javatechniques.com/blog/setting-jtextpane-font-and-color/ where it explains how to do this.
I have a problem with JScrollPane - it looks like it lays over my JTable.
I have made a JTable without JScrollPane and everything seems to be fine - everything gets loaded as I wish and so on - but when I try to add JScrollPane everything goes wrong - only the column names are showing.
This is my code...
projectTable = new javax.swing.JTable();
ProjectTableModel ptm = new ProjectTableModel();
projectTable.setModel(ptm);
jScrollPane1.setViewportView(projectTable);
Is it a problem with positioning components? Could someone please help me with this problem.
The Oracle JTable tutorial contains a section about 'Adding a table to a container', where the container in the example is a JScrollPane
How to implement a panel that would support line wrapping and line breaking?
I would only add textual labels and line breaks to this panel.
The labels should flow from left to right, wrapping to the next "line" if
needed. The line breaks would cause a jump to the next line.
I would also like to make the panel vertically scrollable.
The solution should work in Java 5. SwingX can be used.
Clarification: The textual labels are actually JXHyperlinks (from SwingX),
i.e. the panel contains clickable labels. This is the reason I cannot just use
JTextArea.
UPDATE: I missed the request for hyperlink support. Don't know how to do that w/o using the EditorPane.
JTextArea does exactly what you've described.
JTextArea textArea = new JTextArea();
JScrollPanel sPane = new JScrollPane(textArea);
alt text http://img187.imageshack.us/img187/3238/wraprn0.png
This sample is not from a panel, that is a container, but from a JLabel, that is intended to show content.
You could use HTML in your content and use a <br> on each break. You should programmatically calculate the breaks according with your rules on component resize.
Here's the code:
import javax.swing.*;
import java.awt.*;
public class Wrap {
public static void main( String [] args ) {
JFrame frame = new JFrame("Wrap test");
String text = "<html>This<br>is<br>a<br>multiline<br>label</html>";
frame.add( new JLabel( text ) );
frame.pack();
frame.setVisible( true );
}
}
I found JTextPane, which I had overlooked before for some reason. This class does what I need.
Thanks for your help though. :)
Although it may not be a solution you're in search of, but from the requirements you have, it seems like a custom LayoutManager may be able to achieve what you are after. By designing and assigning a custom Layout Manager which allows line breaks to a Container (such as Panel), it should be possible to have a Panel which allows line breaks.
The Laying Out Components Within a Container article from The Java Tutorials will provide general information on how Layout Managers work in Java, and in particular, the Creating a Custom Layout Manager will provide information on how to make a custom Layout Manager to apply to an Container.
The behavior of the FlowLayout (the default Layout Manager for Panel) seems fairly close to the behavior you may be after. Adding functionality to line break seems like the missing piece.
Suggestion: Perhaps the custom Layout Manager can have the ability to add a line break by having a Component that represents a line break, which can be added to a Container by using the add() method.
For example, have a class constant Component in the custom Layout Manager, such as (a hypothetical) LineBreakLayout.LINE_BREAK, and adding that to the Container can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:
Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));
The above hypothetical LineBreakLayout will then render the first Label in one line and the second Label in the second line.