I have a Groovy app which uses a scrollPane built via swing builder:
BinsicWindow(def controller)
{
controlObject = controller
swinger = new SwingBuilder()
mainFrame = swinger.frame(
title: "Binsic is not Sinclair Instruction Code",
size:[640, 480],
show:true,
defaultCloseOperation: WindowConstants.DISPOSE_ON_CLOSE){
scrollPane(autoscrolls:true) {
screenZX = textArea(rows:24, columns:32) {visble:true}
}
screenZX.setFont(new Font("Monospaced", Font.PLAIN, 18))
}
}
I add text to the textArea programatically (i.e. no user input) and I would like the textArea to scroll down automatically as content is added. But the view remains fixed at the top and I can only see the bottom (once the screen is more than full) by dragging the mouse.
Can I fix this? I have been searching for an answer to this for a wee while now and getting nowhere. Apologies if it's a simple answer.
The following lines should scroll your textarea to the last text position:
rect = screenZX.modelToView(screenZX.getDocument().getLength() - 1);
screenZX.scrollRectToVisible(rect);
Related
I try to make a label to display "You Win" and add it to winStage, but I cannot manage to display the label correctly on winStage. Some words don't show on the screen, and if I downscale further than 0.044f, the String disappears. when I add font.setUseIntegerPositions(false) to my code, the font shows correct, but the wrong label also shows. Is there a way to get this work? (hide the wrong label or get the label shows correctly), code uses Kotlin, but very similar to java
YUW N is the wrong label I want to remove, the correct "You Win" only shows when I add font.setUseIntegerPositions(false)
here's the relevant code part
private val winViewport = StretchViewport(mainStage.width, mainStage.height)
val winStage = Stage(winViewport, winBatch)
var font = BitmapFont()
var labelStyle = Label.LabelStyle()
labelStyle.font = font
labelStyle.fontColor = Color.RED
var label2 = Label("YOU WIN", labelStyle)
label2.setBounds(0f, winStage.height*2/3, winStage.width*1, winStage.height/6)
label2.setFontScale(0.07F) // 0.07f
font.setUseIntegerPositions(false)
label2.setWrap(false)
winStage.addActor(label2)
winStage.act(delta)
winStage.draw()
so I have actually couple of issues where I could need some help.
1.) Actually I can display the SQL Result into the jList, that works so far.
What I am actually missing are the names of the Columns. It just shows me the MySQL Entries.
2.) The second issue is, that my Database actually includes more Information. I want to show that in the jList also but to prevent having a huge window I want to add a horizontal and vertical scrollbar.
I tried adding one in but whenever I did that the list suddenly disappeared. how would adding a scrollbar to the list actually look like ?
The next steps of my Java program would be MySQL Operations. I want to change Values of the received dataset and send it back. I think I will do that with some kind of getSelectedRow right ?
Thank you very much for your help
public class aPanel extends JFrame{
private JTable jt;
public aPanel() {
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 780, 450);
JPanel contentPane = new JPanel();
contentPane.setBackground(new Color (51,51,51));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel leftTitle = new JPanel();
leftTitle.setBackground(new Color (51,153,255));
leftTitle.setBounds(0, 0, 230, 450);
contentPane.add(leftTitle);
leftTitle.setLayout(null);
String[] columnNames = {"ID","First_Name","Last_Name"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
try {
// Establishment of JDBC Connection
String url = "jdbc:mysql://localhost:3306/eHealthDB?serverTimezone=UTC";
Connection con = DriverManager.getConnection(url,"root","root");
String sql = "SELECT ID,First_Name,Last_Name FROM patient;";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next() )
{
String pID = rs.getString("ID");
String vName = rs.getString("First_Name");
String nName = rs.getString("Last_Name");
String[] data = {pID,vName,nName};
tableModel.addRow(data);
}
jt = new JTable();
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setCellSelectionEnabled(false);
jt.setRowSelectionAllowed(true);
jt.setBorder(new LineBorder(Color.WHITE));
jt.setForeground(Color.WHITE);
jt.setBackground(new Color (59,59,59));
jt.setBounds(370, 52, 251,167);
contentPane.add(jt);
jt.setModel(tableModel);
} catch (Exception e1) {
System.out.println(e1);
}
}
}
Don't use a null layout (get rid of that code)
Don't use setBounds(...) (get rid of that code)
Swing was designed to be used with layout managers. The layout manager will give the component a size/location based on the rules of the layout manager.
What I am actually missing are the names of the Columns.
In order for the columns names to appear the table must be added to the viewport of a JScrollPane and the scroll pane added to the frame.
So the basic change would be:
//contentPane.setLayout(null);
contentPane.setLayout(new BorderLayout());
and
//contentPane.add(jt);
contentPane.add(new JScrollPane(jt), BorderLayout.CENTER);
This will allow the table to fill the available space in frame. Scrollbars will appear as required. Read the section from the Swing tutorial on Layout Managers for more information and working examples.
Your question states, "JList", but your code shows a JTable -- this is a bit confusing
Having said that, you've a major problem here:
jt.setBounds(370, 52, 251,167);
Never restrict the size of an expanding component such as a JTable or a JTextArea since this prevents it from displaying within the JScrollPane as it should since it artificially restricts its size. If you are going to restrict the size of anything, it should be the JScrollPane's viewport, and only do this very carefully.
Next, you're adding the JTable itself to the GUI and should be adding JTable to a JScrollPane, actually to the JScrollPane's viewport, and then add the containing JScrollPane to the GUI.
e.g.,
jt = new JTable();
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setCellSelectionEnabled(false);
jt.setRowSelectionAllowed(true);
jt.setBorder(new LineBorder(Color.WHITE));
jt.setForeground(Color.WHITE);
jt.setBackground(new Color (59,59,59));
// jt.setBounds(370, 52, 251,167);
JScrollPane scrollPane = new JScrollPane(jt);
// contentPane.add(jt);
jt.setModel(tableModel);
contentPane.add(scrollPane);
I've got a JPanel that has a BoxLayout (Page axis), and I want to lay out two components, one on top of the other.
My problem is the margin to the left of the large lipsum box, how can I get rid of this? If I don't add the top components, there is no margin.
Here's my code, the second image is created by not adding headerPanel:
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
this.add(headerPanel);
this.add(descLabel);
This class extends JPanel, and is added to a JFrame, which is simply pack()'d
Though I couldn't tell where the observed behaviour comes from, the expected display could be achieved by using an intermediate JPanel to contain your label, rather than adding the JLabel directly :
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
descPanel.add(descLabel);// added
this.add(headerPanel);
this.add(descPanel);// modified
My problem is the margin to the left of the large lipsum box, how can I get rid of this?
You need to make the alignments of your components consistent. That is the alignment "X" property of all the components should be left aligned.
I'm guessing the JLabel is center aligned so you need to use:
descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
See Fixing Alignment Problems section from the Swing tutorial on How to Use BoxLayout for more information and examples.
I'm trying to create a scrollpane that has a background. However, I want the background to take up more than the area of the scrollpane. For instance, Here is my background and colored in red is where I want the scrollpane to be:
However, when I add buttons I get the following result:
How can I limit the actual scrollpane part to just a section (colored in red in the above picture)?
Here is what I have so far. I toyed around with spacing/padding but that did not produce any good results:
Skin skin = Resources.getSkin(Resources.SKIN_JSON);
container = new Table();
container.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
this.addActor(container);
Table table = new Table();
// table.debug();
final ScrollPane scroll = new ScrollPane(table, skin);
table.pad(10).defaults().expandX().space(4);
table.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
container.add(scroll).expand().fill().colspan(1);
container.row().space(10).padBottom(10);
Image background = new Image(Resources.getAtlas(Resources.SKIN_ATLAS).findRegion("main-panel-horz"));
container.setBackground(background.getDrawable());
TextButton button1 = new TextButton("Button1", skin);
table.add(button1);
table.row();
TextButton button2 = new TextButton("button2", skin);
table.add(button2);
table.row();
TextButton button3 = new TextButton("button3", skin);
table.add(button3);
table.row();
TextButton button4 = new TextButton("button4", skin);
table.add(button4);
What you need to do is pad the outer table to get its contents to fit in the region of the background that you would like. (Or you could alternatively pad the cell that you put the scroll pane in.)
If you use a 9-patch drawable for the background, the padding of the outer table will be done for you automatically. You could also specify a TextureRegionDrawable in your skin Json and use that as the background. That would also allow you to pad the table automatically.
You can easily get texture region drawables from the skin with skin.getDrawable(drawableName);--no need to create an intermediate Image just to create a drawable out of a region.
Here's how you would create a TextureRegionDrawable with explicit padding in json:
com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable: {
main-panel-horz-padded: { region: main-panel-horz, leftWidth: 50, rightWidth: 50, topHeight: 50, bottomHeight: 50 }
},
Then in your code:
container.setBackground(skin.getDrawable("main-panel-horz-padded"));
As it says on the tin.. I cannot get a label's text to wrap. Essentially I am building like a comments panel, user enters a comment and its displayed with timestamp etc.
I want the labels to both display with ContentMode.PREFORMATTED but also wrap.
The layouts of which contain the label are fixed width (100%) as is the label obviously by default, from what I have readin the book of vaadin what I am doing should work?
here is my code snippet:
VerticalLayout container = new VerticalLayout();
rootLayout.addComponent(container);
VerticalLayout comment = new VerticalLayout();
container.addComponent(comment);
Label createdByLbl = new Label(entity.getManagedMetadata().getAttrValue("createdBy") + " said:");
createdByLbl.setId("conversation.comment.username." + repositoryUID);
createdByLbl.setStyleName("conversation-comment-username");
Label createdDateLbl = new Label(entity.getManagedMetadata().getAttrValue("createdDate"));
createdDateLbl.setId("conversation.comment.createddate." + repositoryUID);
createdDateLbl.setSizeUndefined();
String text = entity.getDataNode().getAttrValue("text");
Label textLbl = new Label(text, ContentMode.PREFORMATTED);
textLbl.setId("conversation.comment.text." + repositoryUID);
comment.addComponent(createdByLbl);
comment.addComponent(textLbl);
comment.addComponent(createdDateLbl);
comment.setExpandRatio(createdByLbl, 0.2f);
comment.setExpandRatio(textLbl, 0.7f);
comment.setExpandRatio(createdDateLbl, 0.1f);
Note the container is also wrapped by CssLayout (rootLayout), which is full sized.
I want the textLbl as seen above to display as formatted should the user enter text on separate lines themselves and wrap if they have entered a long paragraph comment.
Here is a picturing showing my dilemma.
Any help would be appreciated.
Thanks,
Try with css.
For example:
textLbl.addStyleName("wrapLine");
Css:
.wrapLine{
word-wrap: break-word; /* IE */
white-space: -moz-pre-wrap; /* Firefox */
}