Currently my JTextField component is sizing to what appears to be 0. I have attempted to setPreferredSize, setSize(new Dimension), added columns & removed the anchor with GridBagConstraints. Nothing has worked thus far. Here is my code:
private void createPayFrame() {
JFrame payFrame = new JFrame();
payFrame.setSize(new Dimension(450, 300));
payFrame.setLocationRelativeTo(null);
payFrame.setLayout(new GridBagLayout());
JLabel payment = new JLabel("<html>Your bill has been generated. It is located in your "
+ System.getProperty("user.home") + "\\eclipse-workspace\\Turbo Team directory</html>");
GridBagConstraints gbc = new GridBagConstraints();
JTextField payField = new JTextField(10);
payField.setToolTipText("Enter in the amount due here");
gbc.anchor = GridBagConstraints.ABOVE_BASELINE;
gbc.insets = new Insets(0,0,20,0);
payFrame.add(payment, gbc);
gbc.gridy = 1;
gbc.insets = new Insets(10,0,0,0);
payFrame.add(payField, gbc);
payFrame.setVisible(true);
}
This is what it looks like when I run the code:
Any idea what is going on here?
Set the fill for the textfield
gbc.fill = GridBagConstraints.HORIZONTAL;
I've had this problem with GridBagLayout when the JTextField was too large to display. Have you tried with less columns? Maybe try to start at new JTextField(1) and work your way up to see if it works at smaller lengths.
Related
I have a simple GUI with multiple tabs like that:
GUI
The problem is that, after filling the text area up to the bottom (filled console) and switching the tab - it completely breaks the markup (bottom-broken)
Here is the code example of the textArea and Constraints
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
tabbedPanelOne.add(textArea);
CustomAppender.setTextArea(textArea);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 5;
gbc.weightx = 1.0;
gbc.weighty = 10.0;
Font font = textArea.getFont();
float size = font.getSize() - 4.0f;
textArea.setFont(font.deriveFont(size));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
//textArea.setPreferredSize(new Dimension(500, 300));
layout.setConstraints(textArea, gbc);
/*
* Scrolls for TextArea
*/
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tabbedPanelOne.add(scroll);
layout.setConstraints(scroll, gbc);
//textArea.setPreferredSize(new Dimension(500, 300)); works fine, but it disables vertical scroller.
layout.setConstraints(textArea, gbc);
First of all when add a component to a scrollpane you would never attempt to set constraints on the component. A JScrollPane uses its own custom layout manager so you don't do anything special when you add the component to the viewport of the scroll pane.
However, the simplest solution would be to use a BorderLayout on the panel you add to the tab. Something like:
JPanel top = new JPanel();
top.add(...);
JTextArea textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel main = new JPanel( new BorderLayout() );
main.add(top, BorderLayout.PAGE_START);
main.add(scrollPane, BorderLayout.CENTER);
Now the scroll pane will take up all the available space in the tab and scrollbars will appear when needed.
I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them.
what element/object/class should i use? i want it to be able to present messages in different lines.
here is my code for creating the GUI:
public ssGUI() {
setLayout(new BorderLayout());
bRunNoAdj = new JButton("Run no adjustment");
bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunNoAdj.setMnemonic(KeyEvent.VK_E);
bRunNoAdj.addActionListener(this);
bRunNoAdj.setEnabled(false);
bRunNoAdj.setBackground(Color.white);
bRunAdj = new JButton("Run adjustment");
bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunAdj.setMnemonic(KeyEvent.VK_E);
bRunAdj.addActionListener(this);
bRunAdj.setEnabled(false);
bRunAdj.setBackground(Color.white);
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
bConnect.setBackground(Color.white);
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
bDisconnect.setBackground(Color.white);
bStationary = new JButton("Show Stationary");
bStationary.setMnemonic(KeyEvent.VK_E);
bStationary.addActionListener(this);
bStationary.setEnabled(false);
bStationary.setBackground(Color.white);
bMoving = new JButton("Show Moving");
bMoving.setMnemonic(KeyEvent.VK_E);
bMoving.addActionListener(this);
bMoving.setEnabled(false);
bMoving.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(bConnect);
topPanel.add(bDisconnect);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.add(bRunNoAdj);
centerPanel.add(bRunAdj);
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(bStationary);
bottomPanel.add(bMoving);
add(bottomPanel, BorderLayout.SOUTH);
}
any help would be appreciated, thank you.
The easiest would probably be to use a JTextArea.
You would ofcourse prevent the user from editing the area, this can be done like this:
JTextArea area = new JTextArea();
area.setEditable(false);
And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:
JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
And lastly you can add a line to the area by doing:
area.setText(area.getText() + "\n" + newLine);
Or preferably:
area.append("\n" + newLine);
I hope this helps :)
Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.
I think what you mean is JTextArea or JTextField
I'm trying to create a simple UI for a program that can read a file, write to a file and search for text within a file. I have most of my Components created, the problem is that they're all being "drawn" in the same (center) cell. I've tried applying weights, widths, etc. all to no avail.
Here's my base code for the UI:
public void GUI(){
//Create main window for Program
JFrame mainWindow = new JFrame("Simple Data Base"); //Init frame
mainWindow.setSize(500, 400); //Set frame size
mainWindow.setVisible(true); //Make frame visible
//Create panel for the main window of the GUI
JPanel simpleGUI = new JPanel( new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
simpleGUI.setBackground(Color.cyan);
//Create button linking to read function
JButton readButton = new JButton("Read"); //Init button, and give text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
//Create button linking to the search function
JButton searchButton = new JButton("Search");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 1;
//Create label prompting user to specify desired function
JLabel promptText = new JLabel("Click 'Read' to read a file, 'Search' to search within a file, 'Write' to write to a file:");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 0;
//Add components to Main window
mainWindow.getContentPane().add(simpleGUI);
simpleGUI.add(promptText, gbCons);
simpleGUI.add(readButton, gbCons);
simpleGUI.add(searchButton, gbCons);
}
the problem is that they're all being "drawn" in the same (center) cell.
simpleGUI.add(promptText, gbCons);
simpleGUI.add(readButton, gbCons);
simpleGUI.add(searchButton, gbCons);
You are using the same GridBagConstraints for each component so the contraints are identical for each component.
You neeed to:
set the constraints
add the component to the panel using the constraints
repeat steps 1 and 2.
For example:
JButton readButton = new JButton("Read");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
simpleGUI.add(readButton, gbCons);
JButton searchButton = new JButton("Search");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 1;
simpleGUI.add(searchButton, gbCons);
I suggest you read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.
Download the demo code and use that example as your starting code. The demo code will show you how to better structure your class by:
NOT extending JFrame
Creating the GUI on the Event Dispatch Thread
Use the pack() method, NOT the setSize(...) method
Make the frame visible AFTER all components have been added to the frame
This is a piece of code generated from netbeans Swing gui designer.
I'm trying to do the following: jLabel1 and jLabel2 will contain only image icon with dimension 52x46 px without text, they should be fixed in the left and right side of the line, jTextField2 is expected to fill the gap between jlabels and autoresize to full screen/view width.
Problem is, that jTextField2 remains with same width no matter what size of window/view is... The initial width is dependent on the length of hard-coded text inside the field...
Do you have any idea, how to do this?
private void initComponents() {
javax.swing.JLabel jLabel1;
javax.swing.JLabel jLabel2;
javax.swing.JTextField jTextField2;
java.awt.GridBagConstraints gridBagConstraints;
jLabel1 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
jLabel1.setText("ABC");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
getContentPane().add(jLabel1, gridBagConstraints);
jTextField2.setText("some text field content");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
getContentPane().add(jTextField2, gridBagConstraints);
jLabel2.setText("ABC");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
getContentPane().add(jLabel2, gridBagConstraints);
pack();
}
See documentation of GridBagLayout
GridBagConstraints.weightx, GridBagConstraints.weighty
Used to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least
one component in a row (weightx) and column (weighty), all the
components clump together in the center of their container. This is
because when the weight is zero (the default), the GridBagLayout
object puts any extra space between its grid of cells and the edges of
the container.
jTextField2.setText("some text field content");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
getContentPane().add(jTextField2, gridBagConstraints);
I am try to create a text area that will display a list of scores. For some reason though, the text area only expands when someone types in it, and the user should not even be allowed to type in it. I thought i wrote in the code for it properly but for some reason it doesn't seem to work. The "hello" phrase I appended isn't even displaying in the text area. Can anyone provide some advice:
public HighScores() throws FileNotFoundException, IOException{
frame.setVisible(true);
frame.setSize(400,200);
frame.add(main);
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(10,10,10,10);
g.gridx = 0;
g.gridy = 0;
main.add(highscorespanel, g);
highscorespanel.add(highscores);
g.gridx = 0;
g.gridy = 1;
main.add(textareapanel, g);
Color c = textareapanel.getBackground();
textareapanel.setBackground(c);
textareapanel.add(ta);
ta = new JTextArea ();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
ta.append("hello");
JScrollPane sp = new JScrollPane(ta);
BufferedReader br = new BufferedReader(new FileReader("src/BattleShip/scores.txt"));
String namescore = br.readLine();
while(namescore!=null){
ta.append("\t"+namescore);
}
Im almost sure you don't need the answer anymore but you need to move your line "textareapanel.add(ta);" to some point after you initialize ta.
ta = new JTextArea();
textareapanel.add(ta);
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
EDIT:
At a second look you want a JScrollPane for your JTextArea so you code should be like this:
JTextArea ta = new JTextArea();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
JScrollPane sp = new JScrollPane(ta);
textareapanel.add(sp);