I have a problem with displaying the components of a JScrollPane. Let me first explain the context. I've got one big splitpane:
center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1);
center.setDividerLocation(0.9);
center.setDividerSize(3);
center.setResizeWeight(1);
center.setContinuousLayout(true);
The p pane is shown the right way, no problem here. But the p1 pane won't be displayed, i can see the empty bottom-part of the splitPane, but that's all.
JPanel p = new JPanel();
p.add(canvas);
JPanel p1 = new JPanel();
p1.add(canvasPropPane);
The canvasPropPane is a scrollPane that i initialize like this:
VolumeSizeAndPosition volum = new VolumeSizeAndPosition();
canvasPropPane = new JScrollPane(volum);
volume was tested on an independent frame and have been shown the right way.
I tried showing on the canvasPropPane a simple button canvasPropPane.add(wildButton); and it has a strange behavior: it paints the button only after i hover the mouse over it's location; at repaint (after resizing the scrollpane) it disappears.
I've solved similar problems by calling invalidate() on all of the underlying nested Swing objects. So for your particular question p.invalidate() and p1.invalidate() may help. I believe this strange behavior to be a bug in Swing.
Related
I am new to GWT and have made 3 textarea objects and have added them to a vertical panel, which is also added to my rootpanel. However, I cannot seem to input any text in these textareas. Any suggestions?
VerticalPanel panel = new VerticalPanel();
TextArea tb = new TextArea();
TextArea tb1 = new TextArea();
TextArea tb2 = new TextArea();
panel.add(tb);
panel.add(tb1);
panel.add(tb2);
RootPanel.get().add(panel);
I would try enabling them:
tb.setEnabled(true)
tb1.setEnabled(true)
tb2.setEnabled(true)
But I don't think that should be necessary.
There might be something small you are missing, I would compare all of your code to this. It seems to be a good working example that you could compare your code to and see if you missed a small step.
It seems you may need to add the TextArea objects to horizontal panels and then add those horizontal panels to the vertical panel.
The problem you describe maybe caused by adding another widget on top of your TextArea widgets. In this case TextArea widget may remain visible, but it will be unusable.
I don't see it in the code snippet that you provided, but maybe it's not all of your code.
Try this. It is the example straight from the GWT Javadoc.
Maybe you need to use setCharacterWidth(int size) and setVisibleLines(int size) before adding it.
public class TextBoxExample implements EntryPoint {
public void onModuleLoad() {
//Make an 80 x 50 TextArea
TextArea ta = new TextArea();
ta.setCharacterWidth(80);
ta.setVisibleLines(50);
// Add them to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.add(ta);
RootPanel.get().add(panel);
}
}
I'm developing a Java GUI and I need:
A label in first row(only one label).
Starting 2nd row need to add say 100 buttons which extends to multiple lines(width shouldn't go beyond the visible screen)
In a new line one more Label
From next line say 100 buttons which extends to multiple lines(width shouldn't go beyond the visible screen)...
[OPTIONAL] If the components exceeds JFrame height then need a scroll facility to the main window (only vertical)
I have a strange results with flow layout, sometimes it stick to visible width, sometimes it sets even 500 buttons in a single row.
I have tried every layout and also multipanes. Still no luck.
Please guide.. just need an idea, No need of code
Updated with code: Sorry guys, that was my first question to stackoverflow
Thanks for prompt response
Infact i tried many, here is a simple one.
setLayout(new FlowLayout());
setTitle("JAVA GUI");
setSize(500,500);
setVisible(true);
add(new JLabel("row 1"));
JPanel panel1 = new JPanel(new FlowLayout());
for(int i=0;i<200;i++){
panel1.add(new JButton("b"+i));
}
add(panel1);
Here the panel1 is appearing in a sigle row which goes beyond the visible part of the screen.
I think this can be solved by setting maximumsize to Jframe, but no idea how to set its size to FULL SCREEN.
You can try MigLayout.
http://www.miglayout.com/
Also this question is not really a question for stack overflow. A good way to ask your question would be to post your code and tell us what is wrong with it and what it is supposed to do.
While this is not the norm for 'good' stackOverflow questions, I don't have any problem with it myself. Some people cannot deal with anything except code. I would suggest that, if you're going to post code, that you take the trouble to post code that will compile, run, and demonstrate your situation. It really helps those of us out here understand what you're seeing and what you're trying to do.
You talk about "rows"; be aware that rows and columns are terms used with things like GridLayout and GridBagLayout, but I don't think they're appropriate for what you describe.
In your description, you don't say what you want scrolled. It would appear you want the entirety of the UI scrolled, I'll assume that for now.
I would try a JPanel with BoxLayout, oriented vertically, for the overall main UI. You will put some things into that:
The first JPanel.
Another JPanel, set with FlowLayout, holding the first bunch of buttons.
Another JPanel with the next JLabel
And a fourth JPanel, set with FlowLayout, holding the second bunch of buttons.
Now, I would put the top-level panel into a JScrollPane, and then put that into the CENTER section of a Frame (with its default BorderLayout), and see what happens. To tell the truth, I'm not sure, but these are the things I would start with.
I cannot tell, without running code, why you get odd behavior sometimes.
As said in a previous comment, using a ContentPane is the way to go. Here is a working example of what you want:
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JAVA GUI");
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
int nbLines = 10;
for (int i = 0; i < nbLines; i++) {
JPanel linePanel = new JPanel(new FlowLayout());
linePanel.add(new JLabel("row " + i));
for(int j = 0; j < 50; j++) {
linePanel.add(new JButton("b" + j));
}
panel1.add(linePanel);
}
frame.setContentPane(panel1);
//frame.setSize(500, 500);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
}
And here is what I get:
If you want to have left-aligned buttons you can use:
JPanel linePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
This question already has an answer here:
How can I use BoxLayout to do this?
(1 answer)
Closed 9 years ago.
I have a JPanel, and I want to add JRadioButtons to it, this is the code I tried :
private void populateQuestionnaire(Question question){
buttonGroup = new ButtonGroup();
for(Choix c : question.getListChoix()) {
radioButton = new JRadioButton(c.getChoixLibelle());
buttonGroup.add(radioButton);
jPanel1.add(radioButton);
}
jPanel1.revalidate();
jPanel1.repaint();
}
And I have the layout of JPanel is FlowLayout.
This is how the JRadioButtons displayed :
I want JRadioButtons to be added one below the other and to be centered in the JPanel.
Instead of using a FlowLayout, which lays out items left to right and wraps appropriately, you can use a BoxLayout, which allows you to specify laying out items either horizontally or vertically.
You can set the LayoutManager for your JPanel at construction:
JPanel jpanel1 = new JPanel(new BoxLayout(parentComponent, BoxLayout.Y_AXIS));
BoxLayout is great for stacking elements on top of each other. Consider this code:
public class MyFrame extends JFrame {
public MyFrame() {
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("My Button 1");
JRadioButton b2 = new JRadioButton("My Button 2");
bg.add(b1);
bg.add(b2);
BoxLayout bl = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(bl);
b1.setAlignmentX(CENTER_ALIGNMENT);
b2.setAlignmentX(CENTER_ALIGNMENT);
this.add(b1);
this.add(b2);
}
}
Which makes, when instantiated and shown, the following window:
Now let's look at how this code works:
Consider this code:
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("My Button 1");
JRadioButton b2 = new JRadioButton("My Button 2");
bg.add(b1);
bg.add(b2);
This code does the same thing you were doing before, only a little simpler for example's sake. It creates a button group and two JRadioButtons, then adds the buttons to the button group. Now here is when it gets interesting.
Next, consider this code:
BoxLayout bl = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(bl);
The first line creates a new BoxLayout with the following parameters:
1 The container which it is laying out. (It needs this because it can't be shared.)
2 The axis which it should be laying out components. (You want Y-AXIS for your case.)
The second line set's the JFrame's contentPane's layout to the BoxLayout you just created.
Finally, consider this code:
b1.setAlignmentX(CENTER_ALIGNMENT);
b2.setAlignmentX(CENTER_ALIGNMENT);
this.add(b1);
this.add(b2);
This sets the alignment of the two radio buttons so that their centers will be aligned to each other and to the center of the frame. Then it adds them to the frame's content pane.
Hope this helped!
Note: The reason I used this.getContentPane() while constructing the BoxLayout instead of just using this is because when working with JFrames commands like add() and setLayout() get redirected to the frame's content pane. So if we were to use this in the constructor, when we called this.setLayout(bl) we really would be calling this.getContentPane().setLayout(bl). But, we just told the BoxLayout it'll be laying out the frame, not it's content pane, so you'll get an exception saying that the BoxLayout can't be shared. To correct the error, we just need to realise that we are actually working with the content pane through the frame's methods, and update the BoxLayout's constructor accordingly to let it know what it really is laying out.
I've a big problem with Swing in Java, I used BoxLayout for this but still it looks bad.
Any suggestions about my usage of layouts, or how to change it to look like in assumptions? (here are assumptions)
Container main = new Container();
Container left = new Container();// here goin buttons
Container right = new Container(); // here goin tabs + more buttons, textfields and other stuff
BoxLayout lewyL = new BoxLayout(left, BoxLayout.Y_AXIS);
left.setLayout(lewyL);
left.add(rastrowa); //radiobutton
left.add(wektorowa);//radiobutton
left.add(apDwuliniowa);//checkbox
left.add(wczytaj);//button
left.add(zapisz);//obutton
left.add(wyczysc);//button
BoxLayout prawyL = new BoxLayout(right, BoxLayout.Y_AXIS);
right.setLayout(prawyL);
right.add(zakladki);// tabs (mostly i use BoxLayout but for last one i need something more "complicated")
EDIT: I almost solve this problem, I need to move all elements to left (how it look like)but I have no idea how ;/ Here is constructor of this class.
JLabel label = new JLabel("O wektor");
JLabel labelA = new JLabel("a:");
JLabel labelB = new JLabel("b:");
JButton wykonaj = new JButton("Wykonaj");
JTextField a = new JTextField(5);
JTextField b = new JTextField(5);
add(label);
add(labelA);
add(a);
add(labelB);
add(b);
add(wykonaj);
There's nothing wrong with the way it looks (in my opinion), but if you want it to look a little better, why don't you convert the left panel (which is 6x1) into a 3x2 panel, with the checkboxes/radiobuttons on the left, and buttons on the right? Sounds like a job for GridLayout - one of my favorite classes...
JPanel leftPanel = new JPanel(new GridLayout(3,2));
leftPanel.add(rastrowa); //radiobutton
leftPanel.add(wczytaj); //button
leftPanel.add(wektorowa); //radiobutton
leftPanel.add(zapisz); //obutton
leftPanel.add(apDwuliniowa); //checkbox
leftPanel.add(wyczysc); //button
Note that the 3,2 defines the number of rows,columns. When adding panels, they are added to the grid from left-to-right, and top-to-bottom. GridLayout also auto-sizes the components, so all the buttons etc will be the same width and height, making it look more consistent.
The GridLayout documentation might be useful, and the Visual Guide to Layout Managers is a great place to see other layout managers that might work better for your different situations. I personally find BorderLayout and GridLayout to be the most useful, and cover about 95% of the situations I ever need for my GUIs.
I'm trying to create a very simple window using Java Layouts. I have got three elements to arrange: a button, a progress bar and a label. The button has to be vertically centered, the progress bar has to take full width, and the label has to be left aligned.
Here's some code (just assume pane is the content pane of a JFrame, and button, progressBar and label have been created before):
BoxLayout layout = new BoxLayout(pane, BoxLayout.Y_AXIS);
pane.setLayout(layout);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(button);
progressBar.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(progressBar);
label.setAlignmentX(Component.LEFT_ALIGNMENT);
pane.add(label);
When I test the application I see everything misaligned and screwed up: the button and the label are randomly indented, and if I resize the window the indentation amount changes in a strange way.
The progress bar looks good (full width).
I just don't understand what's happening. Can you give me a clue?
BoxLayout cannot handle different alignments: see http://download.oracle.com/javase/tutorial/uiswing/layout/box.html
quoting from that article: "In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment. Similarly, all the components controlled by a left-to-right Boxlayout should generally have the same Y alignment."
Sometimes you need to get a little creative and use nested panels. But I like this approach better then trying to learn and memorize all the constraints required when using other layout managers (GridBagLayout, GroupLayout) there where designed to be used by IDE's that generate code for you.
import java.awt.*;
import javax.swing.*;
public class BoxLayoutVertical extends JFrame
{
public BoxLayoutVertical()
{
Box box = Box.createVerticalBox();
JButton button = new JButton("A button");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
box.add(button);
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setAlignmentX(Component.CENTER_ALIGNMENT);
box.add(progressBar);
JPanel panel = new JPanel( new BorderLayout() );
JLabel label = new JLabel("A label");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(label);
box.add(panel);
add(box, BorderLayout.NORTH);
}
public static void main(String[] args)
{
BoxLayoutVertical frame = new BoxLayoutVertical();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setSize(300, 200);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
To complement my comment to the original question, here is a snippet that uses DesignGridLayout:
JButton button = new JButton("Button");
JProgressBar progressBar = new JProgressBar();
JLabel label = new JLabel("Label");
// The interesting stuff is in the next 4 lines
DesignGridLayout layout = new DesignGridLayout(getContentPane());
layout.row().center().add(button).withOwnRowWidth();
layout.row().center().fill().add(progressBar);
layout.row().left().add(label);
pack();
It does exactly what wou describe in your question and doesn't require any specific call of any of the components.
Maybe your code is just a snippet, but I'm missing a call to pack().
Coding swing layout by hand can be very frustrating with the standard Layout managers. I use MiG Layout for that purpose. It is straight forward and you have a nice layout with just a few lines of code. If you're not forced to use BoxLayout I would suggest you give it a try.
Don't use BoxLayout. It works only for very simple cases.
For your case, I would recommend either GridBagLayout or (my favorite) GroupLayout.
For GroupLayout, I created a subclass (LayoutHelper) with some utility methods and useful constructors, which makes writing the Layout much easier.
Of course, usually I align all components in a group the same way, so it is not as short in your case as it would be in the simple case.
LayoutHelper h = new LayoutHelper(pane);
h.setVerticalGroup
( h.sequential( button, progressBar, label));
h.setHorizontalGroup
( ((ParallelGroup)h.parallel())
.addComponent(button, Alignment.CENTER)
.addComponent(progressBar)
.addComponent(label, Alignment.TRAILING));
Here is a screenshot:
For a simple "everything aligned the same way", the horizontal group would look like this:
h.setHorizontalGroup
( h.parallel (button, progressBar, label));
(optionally with a first argument indicating the alignment).