I am trying to make a small program..When I execute it, I insert rows and columns and then when I click a button a matrix is appeared on the screen. My problem is that when the number of rows is bigger , I cant see all the matrix and other things that are below, and I can not move down in the JFrame. How can I do this? how can I add a lift, or elevator, to make possible to move up and down? I am not sure if it is called a lift ( the object at the right of the window that helps us to move up and down). Please help me...Thank you in advance.
"how can I add a lift, or elevator, to make possible to move up and down? I am not sure if it is called a lift ( the object at the right of the window that helps us to move up and down)."
Wrap the JTable in a JScrollPane
JTable table = new JTable();
JScrollPane scroll = new JScrollPane(table);
container.add(scroll);
Make sure you remove the table from the container, as a component can only have one parent container
See How to Use ScrollPanes
add(new JScrollPane(tableName));
you should put it in your constructor.
it worked for me, good luck :)
Related
I am in the process of creating my first JavaFX application. I use SceneBuilder to design the GUI. I am facing the following problem:
I have dragged a Pane inside the GridPane. The Pane seems to have to follow the constraints given by RowConstraints and ColumnConstraints. The problem is that these are not aligned with my pane:
I did not expect this to be a big problem, but this results in a gap that is visible when I run my code:
I am talking about the white-colored part in this figure - I want this to be blue. Also, I want to see the entire pane in my window.
I played around and tried to figure this out on my own, but I don't see what I am doing wrong. Any ideas?
What seems to fix the issue is the following:
Inside the Inspector, open the Layout-entry for ColumConstraints. Change HGrow and HAlignment from INHERIT to ALWAYS and LEFT, respectively.
But if anyone knows more about this, feel free to post a separate answer!
I've been working on a Pokemon-themed quiz game in Java (modeled after Sporcle, if you're familiar). Pretty much everything works how I want it to, except for the layout of the different components of the program.
I've never been very good with the different layout managers, and I don't know how to get them to do what I need.
Here's what the window looks like right now:
Now, I'll play around with font sizes later, but the tables themselves look exactly how I want them to. The problem is, I want them to be under the text fields and buttons and stuff. Here's the portion of the code where I add all the components to my JPanel:
panel.add(label,FlowLayout.LEFT); //adding the "big question text"
panel.add(answerfield); //adding the JTextField
panel.add(correctAnswerTracker); //adding the "x / 151" text
for(int x = sPanes.length-1; x >=0; x--) //as you keep adding to left, it gets pushed over, so doing it backwards results in the correct order
panel.add(sPanes[x],FlowLayout.LEFT);
//each table is in a scrollPane, and all my scrollPanes are in the array sPanes, so I'm looping through that to add the tables
panel.add(startStopButton); //button that says "Start"
panel.add(exit); //button that says "Exit
panel.add(timer); //the timer
As you can see, the statements to add the text field, and correct answer tracker are all written before the add statement for the tables, and yet the tables are at the top. Additionally, there's the issue of my tables in that loop being added in the backwards order, so I had to reverse the direction of the loop iterations to get the tables to appear in the correct order. I've tried using stuff like setLocation and setBounds to get my components more where I want them, but nothing happened. Also, everything just appears in a row below the tables (and I know that's what FlowLayout does), but how would I go about customizing exactly where things appear?
Wrap a panel with BorderLayout around ones with FlowLayout. Put all the content that should be above the tables in a panel and add it with BorderLayout.NORTH. Put all the content that should be below the tables in another panel and add it with BorderLayout.SOUTH. Then put the tables in their own panel just as your are now, and add it with BorderLayout.CENTER.
Either use a LayoutManager or setLayout(null). In the latter case, you can move your components around by calling setBounds on them. I've been doing that lately too (not using a LayoutManager), it's quite liberating.
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.
I just want to drag and drop them like in Netbeans.
is there any way to get functionality of Netbeans or Eclipse in IDEA?
I had the same problem and I have solved it. You need to create a new row before dragging any new elements. In the left side you will see small green boxes for each row. Right click on the last row's green box and then choose "Insert row after this". Then you can drag and drop any new elements into your newly created row.
You can drag and drop components and alter on which part of the layout they appear. You might need to split a column if you want the component on only the half of a parent.
See: https://www.jetbrains.com/idea/help/placing-gui-components-on-a-form.html
But to set some minimum size or set if a component should grow, you need to look into properties panel of a selected component (on the left).
See: https://www.jetbrains.com/idea/help/setting-component-properties.html
So I am creating a GUI and it has a JTable that is set inside of a JScrollpane. When the user opens the window it displays everything how I want it to. The user can add rows to the table to fill in data and then they can scroll through the information. I also have a JButton which I have set to print the whole window. Now my problem is the table prints but only shows the data in the viewing area. What i was thinking was if the user could drag the bottom corner of the JTable to re size it to show everything then everything would be ok, I'm just not sure if that's even possible or how to do it. Ive been searching and i haven't seem to come across anything like this yet.
Edit:
So for anyone else here is the link to the class I found. It allows you to use the mouse to resize any component.
http://tips4java.wordpress.com/2009/09/13/resizing-components/
To get an image of the entire table you can try using Screen Image, which allows you to take an image of a specific component.