im having a little issue with my code. I have created a gridlayout of 5,1,0,0. I have a textfield, 3 buttons and a label where the result analysis of whatever the user had input is displayed at the bottom. Now the results can come on multiple lines depending on how big words are in the sentence, my problem is when multiple lines of results are displayed, the layout of my program changes and i dont know how to keep it the same but just the label or Applet window itself resize if need be?
public class assignment_tauqeer_abbasi extends JApplet implements ActionListener {
JTextArea textInput; // User Input.
JLabel wordCountLabel; // To display number of words.
public void init() {
// This code from here is the customisation of the Applet, this includes background colour, text colour, text back ground colour, labels and buttons
setBackground(Color.black);
getContentPane().setBackground(Color.black);
textInput = new JTextArea();
textInput.setBackground(Color.white);
JPanel south = new JPanel();
south.setBackground(Color.darkGray);
south.setLayout( new GridLayout(5,1,0,0) );
/* Creating Analyze and Reset buttons */
JButton countButton = new JButton("Analyze");
countButton.addActionListener(this);
south.add(countButton);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(this);
south.add(resetButton);
JButton fileButton = new JButton("Analyze Text File");
fileButton.addActionListener(this);
south.add(fileButton);
/* Labels telling the user what to do or what the program is outputting */
wordCountLabel = new JLabel(" No. of words:");
wordCountLabel.setBackground(Color.black);
wordCountLabel.setForeground(Color.red);
wordCountLabel.setOpaque(true);
south.add(wordCountLabel);
/* Border for Applet. */
getContentPane().setLayout( new BorderLayout(2,2) );
/* Scroll bar for the text area where the user will input the text they wish to analyse. */
JScrollPane scroller = new JScrollPane( textInput );
getContentPane().add(scroller, BorderLayout.CENTER);
getContentPane().add(south, BorderLayout.SOUTH);
} // end init();
public Insets getInsets() {
// Border size around edges.
return new Insets(2,2,2,2);
}
// end of Applet customisation
This is my code for the layout. Any help would be apprecited!
A GridLayout will size every cell according to the content of the largest cell. Consider using a different layout, or a combination of layouts instead.
The gridLayout that you have used would possibly complicate the five contents that you have used. Try using flow Layout instead this would automatically make space for the new contents that are being entered.
Related
I have two files that I need to display in a program. I need to use JTabbedPane and each file should be displayed in its own tab. I can make the text appear in the tab, but the scroll bar won't appear, so I can't see all of the information in the file. How do I add the scroll bar to the text area?
I made one method that creates a panel with the text in it (this is for one file). Then, I made another method that has JTabbedPane and I added the panel to a tab.
Panel method:
private void makeTextPanel() throws IOException
{
textPanel = new JPanel();
textArea = new JTextArea();
textArea.setEditable(false);
//width: 770 height: 1000
textAreaDimensions = new Dimension(TEXT_AREA_WIDTH, TEXT_AREA_HEIGHT);
textArea.setPreferredSize(textAreaDimensions);
BufferedReader inputFile = new BufferedReader(new FileReader(FILE_ONE));
String lineOfText = inputFile.readLine();
while(lineOfText != null)
{
textArea.append("\n" + lineOfText);
lineOfText = inputFile.readLine();
}
// Add a scroll bar
scrollPane = new JScrollPane(textArea);
// Add the text area and scroll bar to the panel
textPanel.add(textArea);
textPanel.add(scrollPane);
}
Tabbed pane method:
private void makeTabbedPane() throws IOException
{
frame = new JFrame("Project");
tabbedPane = new JTabbedPane();
frame.add(tabbedPane, BorderLayout.PAGE_START);
// add panel to the tab
makeTextPanel();
tabbedPane.addTab("Tab 1", textPanel);
// dimensions
frameDimensions = new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
frame.setPreferredSize(frameDimensions);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
To reiterate:
How do I make the scroll bar visible?
I set the height of the text area to 1000. Will the scroll bar be able to scroll through everything? If not, how do I set the height of the text area to fit everything in the file?
The component you want scroll bars for should always a child of the JScrollPane. Adding the textArea and then the scrollPane to that tabbedPane probably isn't doing what you think it is. Make sure that textArea is the child of scrollPane, and add just the scrollPane to the tabbedPane, ensuring you've specified a layout that dictates how the scrollPane is to take up the space you want within tabbedPane.
The scrollpane will automatically add scrollbars only when it decides the textArea is bigger than it can render in the space it's been given.
Question 1) The JScrollPane methods setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy() will allow you to force the scrollbars to be always visible.
Question 2) The "preferred" height of the textArea is what your scrollPane will use to determine the scrollbar behaviour (see this example). It's all taken care of for you. If not, you'd be forced yourself to consider font rendering height, how much text you put in the textArea etc.
Generally speaking, just throwing a JTextArea into a JScrollPane will see the desired behaviour you're seeking without you having to do anything "special" with JTextArea size.
I have 1 JFrame while 3 JPanel inside it. In JPanel 1, it contains some JTextArea (Linkedlist of TextArea). JPanel 2 contains JTree. JPanel 3 contains JLabel.
Each time i press enter key in JTextArea, JPanel 1 will add a new JTextArea. Each new Text Area add, JPanel 2 (JTree) will add new node. Every TextArea have unique name. If we focus to any text area, JPanel 3 will display name of that text area with JLabel.
My question is, how can i change other componenet outside of JPanel 1? Most of my code inside in JPanel 1. I've already done with adding new text area. I'm using JPanel just only for make my work easier to look.
I've try to search other post and other resource. But, i still dont get what i want.
I'm using AbstractAction method. Here my state now:
Action enter = new AbstractAction() {
//add new text area
};
I want be like this:
Action enter = new AbstractAction() {
//add new text area
//add node in JTree in JPanel 2
//change text of JLabel in JPanel 3
};
I hope this is clear for all of you
I would like to create something like the following in Swing:
The top part is relatively easy: I can just create a table and display it. What I'm having trouble with is the square plus and minus buttons at the bottom, which are designed to add a new item or remove the selected item respectively. In particular, I haven't been able to make the square shape because on Mac OS X and some other platforms, JButtons are rectangles with rounded corners and I can't find a way to change that. Also, I'm wanting to make sure it's a perfect square and without any space in between buttons.
How can this be accomplished in a cross-platform way on Swing?
JButtons are rectangles with rounded corners and I can't find a way to change that.
Change the Border:
button.setBorder( new LineBorder(Color.BLACK) );
Edit.
Another approach is to create your own icon from an existing button. Something like the following:
JButton button = new JButton("+");
Dimension size = button.getPreferredSize();
size.x += 6;
size.y += 6;
button.setPreferredSize(size);
Rectangle rectangle = new Rectangle(3, 3, size.x - 3, size.y - 3);
ScreenImage buttonImage = ScreenImage(button, rectangle);
ImageIcon icon = new ImageIcon(buttonImage);
JButton plus = new JButton(icon);
plus.setBorder( ... );
The above code should create an image of your button on any platform. I increased the preferred size to avoid taking an image of the border.
You will need to use the Screen Image class.
This is most easily achieved by returning a preferred size that is NxN - where N is the larger of preferred width or height.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class SquareButton extends JButton {
SquareButton(String s) {
super(s);
}
#Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int s = (int)(d.getWidth()<d.getHeight() ? d.getHeight() : d.getWidth());
return new Dimension (s,s);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
JComponent gui = new JPanel(new FlowLayout());
for (int ii=0; ii<5; ii++) {
gui.add(new SquareButton("" + ii));
}
gui.setBorder(new EmptyBorder(4, 8, 4, 8));
JFrame f = new JFrame("Square Buttons");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
You can set the size of a button using using setPreferredSize():
JButton button = new JButton("+");
button.setPreferredSize(new Dimension(10, 10));
You might be able to remove the rounded corners using:
button.setBorder(BorderFactory.createEmptyBorder());
If this does not work then you can override the paintComponent() method on JButton.
Well in order to make them square, you have 2 options: 1. Make the button hold an icon image of just a square image of a transparent image. 2. you could set the button dimensions on your own. I am not sure how to set the dimensions, but that is a an option you could choose. You can just create a JToolBar that is set on the BorderLayout.SOUTH end of the window whenever you add, and whatever buttons are added onto that, will be right next to each other. To add the buttons do this:
JButton button1 = new JButton("+");
JButton button2 = new JButton("-");
JToolBar toolbar = new JToolBar();
<JPanel,JFrame,Whatever>.add(toolbar, BorderLayout.SOUTH);
toolbar.add(button1);
toolbar.add(button2);
This will add the toolbar onto the JFrame, JPanel, or whatever you're adding it onto, and it will set it to the bottom of the screen as you see above.
Hope this helps!
I am in the process of making my own java socket game. My game is painting alright to the full screen (where it says "paint graphics here", but im painting to the whole jframe at the moment). I want to add a textbox with a scroll bar for displaying only text, not taking any input and another textbox to take text inputs from the user and then a button to send the text, for chat purposes. But onto my question, how do I even start to lay this out? I understand I need a layout, but can someone help me on this? Here is my code at the moment (this code only sets up painting to the whole screen at the moment, need to divide the screen up now like I have in the picture above):
public class Setup extends JFrame implements Runnable{
JPanel panel;
JFrame window;
public Setup(Starter start, JFrame window){
window.setSize(600,500);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
panel = new Display(start);
this.window = window;
}
public void run(){
window.getContentPane().add(panel);
window.setBackground(Color.BLACK);
window.setVisible(true);
}
}
"new Display(start)" - this extends jpanel, its basically where I paint everything graphics wise.
In addition, I've seen people add in different panels but I cant have them be the same size. Like in the picture, the "paint graphics here" panel is the biggest one, and so on.
The JPanel is actually only a container where you can put different elements in it (even other JPanels). So in your case I would suggest one big JPanel as some sort of main container for your window. That main panel you assign a Layout that suits your needs ( here is an introduction to the layouts).
After you set the layout to your main panel you can add the paint panel and the other JPanels you want (like those with the text in it..).
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel paintPanel = new JPanel();
JPanel textPanel = new JPanel();
mainPanel.add(paintPanel);
mainPanel.add(textPanel);
This is just an example that sorts all sub panels vertically (Y-Axis). So if you want some other stuff at the bottom of your mainPanel (maybe some icons or buttons) that should be organized with another layout (like a horizontal layout), just create again a new JPanel as a container for all the other stuff and set setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS).
As you will find out, the layouts are quite rigid and it may be difficult to find the best layout for your panels. So don't give up, read the introduction (the link above) and look at the pictures – this is how I do it :)
Or you can just use NetBeans to write your program. There you have a pretty easy visual editor (drag and drop) to create all sorts of Windows and Frames. (only understanding the code afterwards is ... tricky sometimes.)
EDIT
Since there are some many people interested in this question, I wanted to provide a complete example of how to layout a JFrame to make it look like OP wants it to.
The class is called MyFrame and extends swings JFrame
public class MyFrame extends javax.swing.JFrame{
// these are the components we need.
private final JSplitPane splitPane; // split the window in top and bottom
private final JPanel topPanel; // container panel for the top
private final JPanel bottomPanel; // container panel for the bottom
private final JScrollPane scrollPane; // makes the text scrollable
private final JTextArea textArea; // the text
private final JPanel inputPanel; // under the text a container for all the input elements
private final JTextField textField; // a textField for the text the user inputs
private final JButton button; // and a "send" button
public MyFrame(){
// first, lets create the containers:
// the splitPane devides the window in two components (here: top and bottom)
// users can then move the devider and decide how much of the top component
// and how much of the bottom component they want to see.
splitPane = new JSplitPane();
topPanel = new JPanel(); // our top component
bottomPanel = new JPanel(); // our bottom component
// in our bottom panel we want the text area and the input components
scrollPane = new JScrollPane(); // this scrollPane is used to make the text area scrollable
textArea = new JTextArea(); // this text area will be put inside the scrollPane
// the input components will be put in a separate panel
inputPanel = new JPanel();
textField = new JTextField(); // first the input field where the user can type his text
button = new JButton("send"); // and a button at the right, to send the text
// now lets define the default size of our window and its layout:
setPreferredSize(new Dimension(400, 400)); // let's open the window with a default size of 400x400 pixels
// the contentPane is the container that holds all our components
getContentPane().setLayout(new GridLayout()); // the default GridLayout is like a grid with 1 column and 1 row,
// we only add one element to the window itself
getContentPane().add(splitPane); // due to the GridLayout, our splitPane will now fill the whole window
// let's configure our splitPane:
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); // we want it to split the window verticaly
splitPane.setDividerLocation(200); // the initial position of the divider is 200 (our window is 400 pixels high)
splitPane.setTopComponent(topPanel); // at the top we want our "topPanel"
splitPane.setBottomComponent(bottomPanel); // and at the bottom we want our "bottomPanel"
// our topPanel doesn't need anymore for this example. Whatever you want it to contain, you can add it here
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); // BoxLayout.Y_AXIS will arrange the content vertically
bottomPanel.add(scrollPane); // first we add the scrollPane to the bottomPanel, so it is at the top
scrollPane.setViewportView(textArea); // the scrollPane should make the textArea scrollable, so we define the viewport
bottomPanel.add(inputPanel); // then we add the inputPanel to the bottomPanel, so it under the scrollPane / textArea
// let's set the maximum size of the inputPanel, so it doesn't get too big when the user resizes the window
inputPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75)); // we set the max height to 75 and the max width to (almost) unlimited
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS)); // X_Axis will arrange the content horizontally
inputPanel.add(textField); // left will be the textField
inputPanel.add(button); // and right the "send" button
pack(); // calling pack() at the end, will ensure that every layout and size we just defined gets applied before the stuff becomes visible
}
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
#Override
public void run(){
new MyFrame().setVisible(true);
}
});
}
}
Please be aware that this is only an example and there are multiple approaches to layout a window. It all depends on your needs and if you want the content to be resizable / responsive. Another really good approach would be the GridBagLayout which can handle quite complex layouting, but which is also quite complex to learn.
You'll want to use a number of layout managers to help you achieve the basic results you want.
Check out A Visual Guide to Layout Managers for a comparision.
You could use a GridBagLayout but that's one of the most complex (and powerful) layout managers available in the JDK.
You could use a series of compound layout managers instead.
I'd place the graphics component and text area on a single JPanel, using a BorderLayout, with the graphics component in the CENTER and the text area in the SOUTH position.
I'd place the text field and button on a separate JPanel using a GridBagLayout (because it's the simplest I can think of to achieve the over result you want)
I'd place these two panels onto a third, master, panel, using a BorderLayout, with the first panel in the CENTER and the second at the SOUTH position.
But that's me
hi I have tried to implement group layout fro my assignment requirement below is my code snippet but I am having one problem within this code snippet when i do pack(); it throws me exception also I don't know how to make it visible please guide me where I am wrong code suggestion would be helpful
thanks in advance
public class AMS_GUI extends JFrame
{
private JFrame frame;
public AMS_GUI()
{
makeFrame();
}
public void makeFrame()
{
JLabel unitLabel = new JLabel("Units"); // units label
JComboBox unitCombo = new JComboBox(); // units empty combo box
JButton addUnit = new JButton("Add"); // add units button for adding units
JLabel AssessmentLabel = new JLabel("Assessments"); // assessments Label
JComboBox AssessmentCombo = new JComboBox(); // assessments empty combo box
JButton addAssessment = new JButton("Add"); // assessments add button
JLabel TasksLabel = new JLabel("Tasks"); // tasks Label
JComboBox TasksCombo = new JComboBox(); // tasks empty combo box
JButton addTasks = new JButton("Add"); // tasks add button
JButton editTasks = new JButton("Edit");// tasks Edit button
JLabel planLabel = new JLabel("Plans");
JButton makePlan = new JButton("MakePlan");
JButton showPlan = new JButton("ShowPlan");
JButton savePlan = new JButton("SavePlan");
//Set up the content pane.
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(unitLabel)
.addComponent(AssessmentLabel)
.addComponent(TasksLabel)
.addComponent(planLabel)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(unitCombo)
.addComponent(AssessmentCombo)
.addComponent(TasksCombo)
.addComponent(makePlan)
.addComponent(showPlan)
.addComponent(savePlan))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(addUnit)
.addComponent(addAssessment)
.addComponent(addTasks)
.addComponent(editTasks)
)
);
setTitle("AMS_GUI");
pack();
Exception
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,
border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource#bb6ab6,flags=296,
maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,
margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,
paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,
selectedIcon=,text=Edit,defaultCapable=true]
is not attached to a vertical group
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
[..]
is not attached to a vertical group
Add a vertical group and add the components to it.
From the JavaDocs:
GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. The horizontal group is responsible for determining the minimum, preferred and maximum size along the horizontal axis as well as setting the x and width of the components contained in it. The vertical group is responsible for determining the minimum, preferred and maximum size along the vertical axis as well as setting the y and height of the components contained in it. Each Component must exist in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.