Whenever I create a new TextArea, all the other textareas next to it seem to be shifted downward.
Can anyone show me the proper way to create two textareas side by side and have them each at a specific location? Right now, text area 2 goes below text area 1 even though i want them at the same level.
Code Now:
FlexTable flexTable = new FlexTable();
VerticalPanel mainPanel = new VerticalPanel();
TextArea t1 = new TextArea();
TextArea t2 = new TextArea();
t1.setCharacterWidth(30);
t1.setVisibleLines(25);
t2.setCharacterWidth(30);
t2.setVisibleLines(25);
flexTable.setWidget(74,10,t1);
flexTable.setWidget(74,70,t2);
mainPanel.add(flexTable);
RootPanel.get().add(mainPanel);
If you want both TextArea to show up in the same FlexTable cell you should wrap those two TextArea with a HorizontalPanel.
HorizontalPanel horizontalPanel = new HorizontalPanel();
FlexTable flexTable = new FlexTable();
...
TextArea t1 = new TextArea();
TextArea t2 = new TextArea();
...
horizontalPanel.add(t1);
horizontalPanel.add(t2);
flexTable.setWidget(74, 10, horizontalPanel);
RootPanel.get().add(mainPanel);
Related
I am trying to add a textfield next to some check boxes, but for some reason it looks only 1 char in size?
JButton addStaff = new JButton("Add Staff");
westPanel.add(addStaff);
// bind the button with the listener
addStaff.addActionListener(new StaffHandler());
c1 = new JCheckBox("Home Only?");
westPanel.add(c1);
c2 = new JCheckBox("Offer ShortHand?");
westPanel.add(c2);
t1 = new JTextField("");
westPanel.add(t1);
My output is the following, which looks a bit rubbish.
You can use the constructor new JTextField(int columns)
e.g. JTextField t1 = new JTextField(20);
I have been trying to customize the business theme in Codename One. So far I have added extra buttons. Right now I am trying to get those buttons to be constrained by a y- axis boxlayout, but I am currently getting a IllegalArgumentException. I have set the form to a border layout:
Form hi = new Form("Welcome", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(BoxLayout.Y_AXIS, Customer).
add(BoxLayout.Y_AXIS, learnMore).
add(BoxLayout.Y_AXIS, gpsProduct).
add(BoxLayout.Y_AXIS, Website);
hi.show();
Box layout Y isn't a constraint for border layout. It's unclear how you wanted this to look but I'm guessing you want something like this which will arrange the components one after the other:
Form hi = new Form("Welcome", BoxLayout.y());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(Customer).
add(learnMore).
add(gpsProduct).
add(Website);
hi.show();
Here are two nested examples that place the box in a border layout parent:
Form hi = new Form("Welcome", new BorderLayout());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
Container box = new Container(BoxLayout.y());
box.add(Customer).
add(learnMore).
add(gpsProduct).
add(Website);
hi.add(BorderLayout.CENTER, box);
hi.show();
This can be written in shorthand as:
Form hi = new Form("Welcome", new BorderLayout());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(BorderLayout.CENTER,
BoxLayout.encloseY(Customer, learnMore, gpsProduct, Website););
hi.show();
I am trying to make a variable size interface. However, currently, if the size of all components is greater than the dimension of the window, then those begin to
distort ! Therefore, I would like to add a JScrollPane to be displayed when the window size is no longer sufficient, in order to avoid this distortion.
I just started by adding a JScrollPane (as shown on the code bellow), but it does not seem to work, and I am not sure what to change now.
JScrollPane myScrollPane = new JScrollPane();
myWindow.getContentPane().add(myScrollPane, BorderLayout.CENTER);
EDIT 1: here is a simple example of what I have tried:
static void one(String[] ari, JLabel sess_nb, Box boxy, long[] res){
int p;
int length = ari.length;
Border cadre = BorderFactory.createLineBorder(Color.black);
Font police = new Font("Monospaced 13", Font.BOLD, 18);
Font police1 = new Font("Monospaced 13", Font.BOLD, 15);
Font font = new Font("Monospaced 13", Font.ITALIC, 13);
for (p=0;p<length;p++){
Box boxz = Box.createVerticalBox();
JLabel Rate = new JLabel("Rating Group "+r);
JLabel rg_reser = new JLabel();
JLabel switsh = new JLabel("1");
JLabel reser = new JLabel(); //in this label the resrvation for each update and each rating group will be stocked
JLabel consump = new JLabel(); //in this label will be stocked the consumption
//----------------------------------------------------
//choice of MB or kB
JComboBox combo = new JComboBox();
combo.addItem("Megabytes");
combo.addItem("Bytes");
combo.addItem("kilobytes");
combo.addItem("Gigabytes");
JLabel upload = new JLabel("Upload consumption:");
JTextField upload_entry = new JTextField("7.5");
JLabel download = new JLabel("Download consumption:");
JTextField download_entry = new JTextField("7.5");
JTextField total_entry = new JTextField();
JLabel total = new JLabel("Total consumption:");
JButton rg = new JButton("Next");
JLabel update = new JLabel("Result here");
boxz.add(Rate);
boxz.add(total);
boxz.add(total_entry);
boxz.add(upload);
boxz.add(upload_entry);
boxz.add(download);
boxz.add(download_entry);
boxz.add(combo);
boxz.add(rg);
boxz.add(update);
boxy.add(boxz);
scrollPane1.add(boxy);
}
EDIT 2: here is a new test code, but it also does not work:
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
content.add(imad);
content.add(textArea);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
A small white scare appears (I think it is the scroll bar or scroll panel?) and the components in the window exceeds the windows size but nothing happens when I try to scroll (using the mouse or by clinking on the white scare). I don't know what is wrong.
Don't use scrollPanel.add(boxy) - you need to add boxy to the scrollPanel's viewport, not to the scrollPanel itself. Use scrollPanel.setViewportView(boxy) instead, or better yet create the ScrollPanel AFTER you create boxy and use the constructor that takes a Component argument.
I'm having issues getting libgdxs scrollpane control to work. The code below shows control setup for a simple layout with a label, a List of items inside a scrollpane, and a button. The problem is my scroll pane doesn't show anything other than the vScroll/vScrollKnob ninepatch (that tiny white square)
it just looks like this:
screenshot.
private void setupLayout()
{
String[] listEntries = {"1","2","3","4","5"};
ListStyle listStyle = new ListStyle();
NinePatch example = new NinePatch(new Texture(Gdx.files.internal("data/example.9.png")));
listStyle.selectedPatch = example;
listStyle.font = new BitmapFont();
mList = new List(listEntries,listStyle);
ScrollPaneStyle paneStyle = new ScrollPaneStyle();
paneStyle.vScroll = example;
paneStyle.vScrollKnob = example;
mListScroll = new ScrollPane(mList,paneStyle);
mListScroll.setScrollingDisabled(true, false);
mListScroll.width = 500;
mListScroll.height = 500;
LabelStyle ls = new LabelStyle();
ls.font = new BitmapFont();
ls.fontColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
mLabel = new Label("Label", ls);
TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
mButton = new TextButton("Button",buttonStyle);
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mListScroll);
mStage.addActor(table);
}
It works as expected if i don't user the scroll pane and add the list directly to the table like this:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mList); //changed mListScroll(scrollpane class) to mList(List class)
mStage.addActor(table);
screenshot
But then it will extend out the bottom of my screen when there are too many items. What am I doing wrong here? Is there another parameter i need to set on the scrollpane?
I believe the issue you are having is how you are adding things to the table. I would suggest the following code instead of how you are doing it:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
// Changing the table layout size itself.
table.add(mListScroll).size(500, 500);
mStage.addActor(table);
For more a more detailed explanation refer to TableLayout the quick start here shows you more how using tables for laying out objects.
I have a JPanel and for example, if I click on the button "INSERT", I can add a JButton and a JLabel. My problem is I need to insert the JLabel under the JButton. The JLabel text must centred respect the JButton text. After that, I want a space around 10 pixels to use again my "INSERT" button and add horizontally a new pair on JButton and JLabel with the same orientation.
Thanks!
PD: Please, complement your question with an attempt.
Here is a quick example that shows a dynamic (which is what I assume you wanted) setup to allow insertion of an undefined number of panels:
public class AwesomeAnswer {
public static void main(String[] args) {
// please not that this is only an example and not a
// Swing thread safe way of starting a JFrame
JFrame frame = new JFrame();
JPanel content = (JPanel)frame.getContentPane();
// create our top panel that will hold all of the inserted panels
JPanel page = new JPanel();
page.setLayout( new BoxLayout( page, BoxLayout.Y_AXIS ) );
// add our page to the frame content pane
content.add( page );
// add two button/label panels
page.add( insert( "This is an awesome answer", "Accept" ) );
page.add( insert( "Say thank you", "Thank" ) );
frame.pack();
frame.setVisible( true );
}
public static final JPanel insert( String labelText, String buttonText ) {
// create the label and the button
JLabel lbl = new JLabel( labelText );
JButton btn = new JButton( buttonText );
// create the panel that will hold the label and the button
JPanel wrapPanel = new JPanel( new GridBagLayout() );
wrapPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// tell the grid bag how to behave
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 0;
gbc.gridheight = 2;
// make the button centered
JPanel buttonPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
buttonPanel.add( btn );
// make the label centered
JPanel labelPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
labelPanel.add( lbl );
// add our button and label to the grid bag with our constraints
wrapPanel.add( buttonPanel, gbc );
wrapPanel.add( labelPanel, gbc );
return wrapPanel;
}
}
I think that you have something like that
rootPane
+-----panelButton
| +------JButton
|
+-----panelPanels
+-----panel
+---JButton
+---JLabel
The SpringLayout can help you
SpringUtilities.makeGrid(panel,
2, 1, //rows, cols
0, 0, //initialX, initialY
5, 5);//xPad, yPad